如何使用Microsoft Bot SDK向Teams频道发送主动消息
向Teams静态频道发送主动消息问题
需求说明
- 使用JavaScript/TypeScript版Microsoft Bot SDK,实现向Teams静态频道发送主动消息
- 需创建REST端点,供第三方服务器调用后触发Bot发送消息到指定频道:其他服务器执行操作后给Bot服务器发送事件,Bot负责将事件通知到对应Teams频道
- 现有示例均为响应用户消息的场景,无法满足当前主动触发的需求
现有实现代码
触发端点(index.js)
server.post('/api/proactiveMessages', async (req, res) => { // Route received a request to adapter for processing console.log('Inside proactive"') await myBot.teamsCreateConversation(adapter,"http://localhost:54956", "Hello"); res.send(200) });
该端点用于接收第三方请求并触发Bot发送消息,目前仅在Bot Framework Emulator中测试,未部署到真实Teams环境。
Bot消息处理方法(bot.js)
async teamsCreateConversation(adapter, serviceUrl, message) { const teamsChannelId = "emulator" const conversationParameters = { isGroup: true, channelData: { channel: { id: teamsChannelId } }, activity: { text: message }, bot: { id: 'd1c86990-1146-11ef-b537-a7ce13724f54', name: 'Bot', role: 'bot' } }; let conversationReference let newActivityId console.log('Right before sending the message') await adapter.createConversationAsync( 'd1c86990-1146-11ef-b537-a7ce13724f54', teamsChannelId, serviceUrl, null, conversationParameters, async (turnContext) => { console.log('Inside the callback') conversationReference = TurnContext.getConversationReference(turnContext.activity); newActivityId = turnContext.activity.id; } ) console.log('After sending the message') return [conversationReference, newActivityId] }
当前问题
调用createConversationAsync时程序卡住,Bot Framework Emulator未收到消息,但通过nc监听端口能捕获到请求:
[chatops-sample] nc -l 54956 POST /v3/conversations HTTP/1.1 Content-Type: application/json; charset=utf-8 accept: */* User-Agent: Microsoft-BotFramework/3.1 botframework-connector/4.22.2 core-http/3.0.4 Node/v21.7.3 OS/(arm64-Darwin-23.4.0) Content-Length: 152 Accept-Encoding: gzip,deflate Host: localhost:54956 Connection: keep-alive {"isGroup":true,"bot":{"id":"d1c86990-1146-11ef-b537-a7ce13724f54","name":"Bot","role":"bot"},"activity":{},"channelData":{"channel":{"id":"emulator"}}}
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

