Teams 中的主动消息传递机器人,无需事先提及该机器人 [英] Proactive messaging bot in Teams without mentioning the bot beforehand
问题描述
我正在使用 Microsoft 机器人框架来创建机器人并将其集成到团队中.该机器人的部分要求包括每天主动向用户发送一次消息.据我了解,我只能在机器人之后向已添加到团队/群聊的用户发送消息,或者直接向机器人发送消息的用户.我的问题是——我能以某种方式绕过这个限制吗?作为新测试版的一部分,我的一个朋友向我推荐了 graphAPI 的一个新功能 - https://docs.microsoft.com/en-us/graph/api/user-add-teamsappinstallation?view=graph-rest-beta&;tabs=http.
I'm using the Microsoft bot-framework to create a bot and integrate it into teams. Part of the bot's requirements include proactively messaging users once per day. From what I understand, I can only message users that has been added to the team/groupChat after the bot, or that have messaged the bot directly. My question is - can I somehow bypass this limitation? A friend of my referred me to a new feature of graphAPI, as part of the new beta version - https://docs.microsoft.com/en-us/graph/api/user-add-teamsappinstallation?view=graph-rest-beta&tabs=http.
对我来说,这似乎与解决方案无关,因为我没有在响应中返回任何数据,所以如果我没有 conversationReference 对象,我仍然无法向用户发送消息.
To me it doesn't seem like it could be related to the solution since I'm not getting any data back in the response, so if I have no conversationReference object I still can't message the user.
目前,我的解决方案是在添加频道时简单地在频道中广播一条消息,要求用户通过发送消息来注册"它.有人有其他建议吗?
At the moment my solution is to simply broadcast a message in the channel when it's added, asking users to "register" with it by messaging it. Anyone has any other suggestion?
推荐答案
最简单的方法是:
- 为团队安装机器人
- 查询团队名单 -- 步骤 3 中的链接有另一种方法可以在底部执行此操作
- 与用户建立对话并发送主动消息
- Install the bot for the team
- Query the Team Roster -- The link in Step 3 has an alternative way to do this towards the bottom
- Create a conversation with the user and send a proactive message
这些链接中有很多代码,最好只是访问它们而不是在此处复制/粘贴.
There's a lot of code in those links and it's better to just visit them than to copy/paste it here.
第 3 步的末尾还提到了 trustServiceUrl
,如果您在尝试发送主动消息时遇到权限/身份验证问题,您可能会发现它很方便.
The end of Step 3 also mentions trustServiceUrl
, which you may find handy if you run into permissions/auth issues when trying to send a proactive message.
npm i -S npm install botbuilder-teams@4.0.0-beta1 botframework-connector
注意:@<version>
很重要!
在 index.js
const teams = require('botbuilder-teams');
adapter.use(new teams.TeamsMiddleware());
获取名册
// Get Team Roster
const credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
const connector = new ConnectorClient(credentials, { baseUri: context.activity.serviceUrl });
const roster = await connector.conversations.getConversationMembers(context.activity.conversation.id);
发送主动消息
const { TeamsContext } = require('botbuilder-teams');
// Send Proactive Message
const teamsCtx = TeamsContext.from(context);
const parameters = {
members: [
roster[0] // Replace with appropriate user
],
channelData: {
tenant: {
id: teamsCtx.tenant.id
}
}
};
const conversationResource = await connector.conversations.createConversation(parameters);
const message = MessageFactory.text('This is a proactive message');
await connector.conversations.sendToConversation(conversationResource.id, message);
必要时信任 ServiceUrl
阅读它.在发送消息之前你会想要这个.
Trust the ServiceUrl, as Necessary
Read about it. You'd want this before the message is sent.
MicrosoftAppCredentials.trustServiceUrl(context.activity.serviceUrl);
这篇关于Teams 中的主动消息传递机器人,无需事先提及该机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!