สร้าง Webhook Message ส่งข้อความเข้า MS Team แบบ Advance

Phanwasin P.
SET-IT-TEAM
Published in
3 min readSep 1, 2023

เริ่มต้น เรามาทำความรู้จักกับ ms team webhook กันก่อน สำหรับ microsoft team ที่เราใช้ส่งข้อความติดต่อกันนั้น เราสามารถให้ server ส่งข้อความต่างๆเข้าหาห้อง channel ต่างๆใน ms team แทนตัวบุคคลได้ โดยผ่านเครื่องมือที่ชื่อว่า Incoming Webhook

Incoming Webhook

เราสามารถสร้างได้โดยการเลือก option ที่ channel ที่ต้อง และเลือก Connectors (ยังใช้ได้บน MS Team Old Version เท่านั้น)

เลือก Add หรือ Configure ที่ Incoming Webhook

ใส่ชื่อ และ upload รูป avatar ให้เรียบร้อย และ copy URL ของ webhook เก็บไว้เพื่อใช้ต่อไป

หลังจากที่ได้ URL webhook ที่ต้องการมาแล้ว ต่อไปเราก็ต้อง build ตัว message ที่ต้องการส่งไปในรูปแบบของ Message Card

Message Card

ตัวอย่าง simple message card ที่ใช้ส่งข้อความเช่น

{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": "Teams Webhook Example",
"sections": [{
"activityTitle": "New Message",
"text": "Hello World!"
}]
}

เมื่อต้องส่ง simple message card ผ่าน python script ก็จะสร้าง code ประมาณนี้

import json
import requests

def send_teams_message(webhook_url, message):
headers = {
"Content-Type": "application/json"
}

payload = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": "Python Teams Webhook Example",
"sections": [{
"activityTitle": "New Message",
"text": message
}]
}

response = requests.post(
url=webhook_url,
headers=headers,
data=json.dumps(payload)
)

if response.status_code == 200:
print("Message sent successfully.")
else:
print("Failed to send message.")

# Replace YOUR_WEBHOOK_URL with your actual Teams Webhook URL
webhook_url = "YOUR_WEBHOOK_URL"
message = "Hello from Python!"

send_teams_message(webhook_url, message)

ทั้งนี้ Message Card ที่สามารถส่งเข้า Webhook ได้นั้นจะมีหลายประเภท แต่จะไม่อธิบายในที่นี้ หรือเข้าดูกันเอง [card type]

Adaptive Card

ในที่นี้ เพื่อความ Advance จะขอเน้นที่ AdaptiveCard ซึ่งสามารถปรับแต่งลูกเล่นต่างๆได้มากมาย เช่นการสร้าง card ตามตัวอย่างนี้

โดยเราต้องเริ่มต้นด้วย template ของ AdaptiveCard สำหรับ Incoming Webhook ดังนี้

{
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": None,
"content": << JSON_PAYLOAD_PLACE_HERE >>
}
]
}

สำหรับ content ส่วน << JSON_PAYLOAD_PLACE_HERE >> ที่ระบุใน template จะสามารถใช้เครื่องมือ amdesigner ช่วยสร้างขึ้นมาได้ โดยเข้าไปสร้างผ่าน [Link] (https://amdesigner.azurewebsites.net/)

เช่น ในภาพตัวอย่าง เราออกแบบ adaptive card แล้วได้ JSON Payload มา ก็นำมาประกอบกับ template และใส่ใน code python ตามนี้

import json
import requests

def send_teams_adaptive_card(webhook_url, message):
headers = {
"Content-Type": "application/json"
}

payload = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": None,
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": [
{
"type": "TextBlock",
"text": message,
"wrap": True,
"weight": "bolder",
"size": "medium"
}
]
}
}
]
}

response = requests.post(
url=webhook_url,
headers=headers,
data=json.dumps(payload)
)

if response.status_code == 200:
print("Adaptive Card sent successfully.")
else:
print("Failed to send Adaptive Card.")

# Replace YOUR_WEBHOOK_URL with your actual Teams Webhook URL
webhook_url = "YOUR_WEBHOOK_URL"
message = "Hello from Python with Adaptive Card!"

send_teams_adaptive_card(webhook_url, message)

เมื่อ run code ดังกล่าว เราก็จะสามารถส่ง message เข้าไปยัง MS Team channel ที่เราสร้าง Incoming Webhook ได้แล้ว

Hooray!!!

--

--