Skip to the content.

使用 GraphAPI 来发送消息

我们今天继续来讲讲 Graph API,看一下如何发送消息到 Teams。

Teams 里有好几个地方可以发送消息:

  • 团队的频道 Channel
  • 聊天chat

往频道里发送消息

import json
import requests

client_id = 'your-app-id'
client_secret = 'your-app-secret'
team_id = 'your-team-id'
channel_id = 'your-channel-id'

token_url = 'https://login.microsoftonline.com/your-tenant-id/oauth2/token'
data = {
    'client_id': client_id,
    'client_secret': client_secret,
    'grant_type': 'client_credentials',
    'resource': 'https://graph.microsoft.com'
}
response = requests.post(token_url, data=data)
token = response.json()['access_token']

# Send a message to the channel
message_url = f'https://graph.microsoft.com/v1.0/teams/{team_id}/channels/{channel_id}/messages'
headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
}
message = {
    'body': {
        'content': 'Hello, this is a test message.'
    }
}
requests.post(message_url, headers=headers, json=message)

这段代码主要分为两个部分:获取 AAD 令牌,发送消息

首先, 使用 Azure AD 应用程序 ID 和秘密向 Azure AD 发送请求,以获取 AAD 令牌。这需要向 Azure AD 的 token_url 发送一个请求,并在请求数据中提供应用程序 ID 和秘密、授权类型和资源。响应将包含 AAD 令牌,该令牌可用于向 Graph API 发送请求。

接下来, 使用 AAD 令牌和团队 ID 和频道 ID 向 Graph API 发送一个 HTTP POST 请求,以向该频道发送消息。这需要在请求头中包含 AAD 令牌,并在请求正文中包含消息内容。

可能大家会问那如何可以回复一条消息呢?回复消息实际上也很简单,python代码如下:

# Retrieve the latest message in the channel
messages_url = f'https://graph.microsoft.com/v1.0/teams/{team_id}/channels/{channel_id}/messages'
response = requests.get(messages_url, headers=headers)
latest_message_id = response.json()['value'][0]['id']

# Reply to the latest message
reply_url = f'https://graph.microsoft.com/v1.0/teams/{team_id}/channels/{channel_id}/messages/{latest_message_id}/replies'
reply = {
    'body': {
        'content': 'This is a reply to the test message.'
    }
}
requests.post(reply_url, headers=headers, json=reply)

这段代码用于获取最新的消息并回复该消息。首先,使用 Graph API 获取频道中的所有消息。这需要发送一个 HTTP GET 请求到 messages_url,其中包含团队 ID 和频道 ID。请求头中还需要包含 AAD 令牌,以进行鉴权。然后,从响应中提取最新消息的 ID。响应是一个 JSON 对象,其中包含名为“value”的数组,最后,使用 requests.post() 函数发送一个 HTTP POST 请求到 reply_url,以在频道中发送回复消息。请求头中需要包含 AAD 令牌,以进行鉴权。请求正文中需要包含回复消息的 JSON 对象。

往聊天chat里发送消息

发送消息到 Microsoft Teams 中的聊天室可以使用下面的代码:

# Set the chat_id
chat_id = '19:<chat_id>@thread.skype'
message = {
    "body": {
        "content": "Hello, this is a test message."
    }
}

response = requests.post(f'https://graph.microsoft.com/v1.0/chats/{chat_id}/messages', headers=headers, json=message)

在上面的代码中, 替换 为自己的值。chat id是一个聊天线的id号,然后调用和之前类似的url,差别在于这里使用了 `chats/{chat_id}` 而不是 `teams/{team_id}/channels/{channel_id}`

看到这里,可能大家已经猜到了,回复消息的写法:

messages_url = f'https://graph.microsoft.com/v1.0/chats/{chat_id}/messages'
response = requests.get(messages_url, headers=headers)
latest_message_id = response.json()['value'][0]['id']

# Reply to the latest message
reply_url = f'https://graph.microsoft.com/v1.0/chats/{chat_id}/messages/{latest_message_id}/replies'
reply = {
    'body': {
        'content': 'This is a reply to the test message.'
    }
}
requests.post(reply_url, headers=headers, json=reply)

和前面的基本类似,差别还是在于 chats,而不是 teams 和 channels。

看到这里,大家是不是已经迫不及待要试试看了?赶快动手吧!

Written on June 3, 2022