Teams通过Webhook实现mention某人及踩坑记录

背景

我们在使用MS Teams进行沟通的时候,肯定会用到webhook进行消息的自动化发送,但是现在有一个需求,有一些消息需要指定@某些人,而不是单纯的发送消息

实现

前面我们说了怎么通过python代码进行通知发送,这次只需要改一些代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import urllib3
import json

http = urllib3.PoolManager()


def lambda_handler(event, context):
# 这是webhook地址
url = "https://xxx.webhook.office.com/webhookb2/xxxxxx"
msg = {
"type":"message",
"attachments":[
{
"contentType":"application/vnd.microsoft.card.adaptive",
"contentUrl":'',
"content":{
"$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
"type":"AdaptiveCard",
"version":"1.2",
"body": [
{
"type": "TextBlock",
"text": "Hi <at>Adele UPN</at>, <at>Adele Microsoft Entra ID</at>"
}
],
"msteams": {
"entities": [
{
"type": "mention",
"text": "<at>Adele UPN</at>",
"mentioned": {
"id": "AdeleV@contoso.onmicrosoft.com",
"name": "Adele Vance"
}
},
{
"type": "mention",
"text": "<at>Adele Microsoft Entra ID</at>",
"mentioned": {
"id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd",
"name": "Adele Vance"
}
}
]
}
}
}
]
}
encoded_msg = json.dumps(msg).encode("utf-8")
resp = http.request("POST", url, body=encoded_msg)
print(
{
"message": event["Records"][0]["Sns"]["Message"],
"status_code": resp.status,
"response": resp.data,
}
)

可以看到一下变更点

  1. body中的TextBlock添加了Adele UPN 字段,其中Adele UPN可以为用户名
  2. 添加msteams块,其中指定type为mention, id可以为UserId,也可以为用户邮箱,也可以为MS Entra Object ID,key都是id
  3. name字段,可以把变更点1中的用户名改为别的字段,但是不影响@用户的效果

坑点

切记!@mention功能不支持Guest账户,也就是说只有在这个Teams中的用户才能被@!