|
import requests |
|
import json |
|
|
|
def convert_newlines_to_google_chat_format(text): |
|
|
|
return text.replace('\\n', '\\\n') |
|
|
|
def send_google_chat_card(webhook_url, title, subtitle, link_text, link_url): |
|
headers = { |
|
'Content-Type': 'application/json; charset=UTF-8' |
|
} |
|
|
|
subtitle = convert_newlines_to_google_chat_format(subtitle) |
|
|
|
card_message = { |
|
"cards": [ |
|
{ |
|
"header": { |
|
"title": title, |
|
}, |
|
"sections": [ |
|
{ |
|
"widgets": [ |
|
{ |
|
"textParagraph": { |
|
"text": subtitle |
|
} |
|
}, |
|
{ |
|
"textParagraph": { |
|
"text": "<b>{}</b>".format(link_text) |
|
} |
|
}, |
|
{ |
|
"buttons": [ |
|
{ |
|
"textButton": { |
|
"text": "Open Link", |
|
"onClick": { |
|
"openLink": { |
|
"url": link_url |
|
} |
|
} |
|
} |
|
} |
|
] |
|
}, |
|
] |
|
} |
|
] |
|
} |
|
] |
|
} |
|
|
|
|
|
response = requests.post(webhook_url, headers=headers, data=json.dumps(card_message)) |
|
|
|
if response.status_code == 200: |
|
print("Message posted successfully.") |
|
else: |
|
print(f"Failed to post message: {response.status_code}, {response.text}") |
|
|