|
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,image_url=None): |
|
headers = { |
|
'Content-Type': 'application/json; charset=UTF-8' |
|
} |
|
|
|
subtitle = convert_newlines_to_google_chat_format(subtitle) |
|
|
|
card_message = { |
|
"cards": [ |
|
{ |
|
"header": { |
|
"title": title, |
|
"imageUrl": image_url, |
|
"imageStyle": "IMAGE" |
|
}, |
|
"sections": [ |
|
{ |
|
"widgets": [ |
|
{ |
|
"textParagraph": { |
|
"text": subtitle |
|
} |
|
}, |
|
{ |
|
"buttons": [ |
|
{ |
|
"textButton": { |
|
"text": "ใใฃใใใใใ่จญๅฎใทใผใใ้ใ ", |
|
"onClick": { |
|
"openLink": { |
|
"url": link_url |
|
} |
|
} |
|
} |
|
} |
|
] |
|
}, |
|
{ |
|
"buttons": [ |
|
{ |
|
"textButton": { |
|
"text": "ใฉใคใณใใฃใใใ้ใ", |
|
"onClick": { |
|
"openLink": { |
|
"url": "https://chat.line.biz/U2df77fd63804c72346b4e50c0096572e/chat/Ua13ef47afc077917275658a44019e072" |
|
} |
|
} |
|
} |
|
} |
|
] |
|
}, |
|
] |
|
} |
|
] |
|
} |
|
] |
|
} |
|
|
|
|
|
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}") |
|
|
|
response_data = response.json() |
|
thread_name = response_data['thread']['name'] |
|
return thread_name |
|
|
|
def send_google_chat_card_thread(webhook_url, title, subtitle, link_text, link_url,thread_name): |
|
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 |
|
} |
|
} |
|
} |
|
} |
|
] |
|
}, |
|
] |
|
} |
|
] |
|
} |
|
], |
|
"thread": { |
|
"name":thread_name |
|
} |
|
} |
|
|
|
|
|
response = requests.post(webhook_url+"&messageReplyOption=REPLY_MESSAGE_OR_FAIL", 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}") |
|
|
|
response_data = response.json() |
|
thread_name = response_data['thread']['name'] |
|
return thread_name |