当机器人在 Teams 中发送消息时,如何获得通知?
How do I get notifications when a bot sends a message in Teams?
我使用 Microsoft Bot Framework v4 Nodejs SDK(botbuilder-sdk for nodejs)为 Microsoft Teams 开发了一个机器人。我们以这样一种方式实施了机器人,当我们使用来自我们的一个 CRM 的 REST API 调用接收数据时,数据将发布到 Microsoft Teams 上的频道。但是,当我这样做时,我们不会在设备上收到通知。有人遇到过这样的问题吗?
我最初是在保存上下文状态。每次我们从 CRM 接收数据时,我都会增加消息的 activity id(将其作为新消息发送而不是回复)并使用 context.sendActivity() 将其发送到 Microsoft Teams。
当我们收到自适应卡片时,我们不会在 activity 提要或任何设备上收到通知。
我已经完成了您上面描述的所有步骤。我还完成了故障排除步骤。但是,它仍然没有给我卡片的通知。但是,当我发起与机器人的对话时,当机器人响应时我会收到通知。
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
在此图中,当我启动 TMS Bot 时会收到通知!信息。但是,我没有收到接下来两条消息的通知。
编辑:OP 和我交换了几封电子邮件以得到答复。总的来说,这个答案是完成团队主动消息传递的好信息,一般来说,但主要答案在最后一部分,简化代码中。
这是一个涵盖很多领域的长答案,只是因为我不能 100% 确定我知道您没有收到哪种通知。
疑难解答
- Troubleshooting Guide
- 特别注意需要启用通知的许多区域
- 特别是,用户可能需要"Follow"and/or"Favorite"接收通知的频道
- 如果用户打开了桌面应用程序,他们将在那里收到通知,并且不会在 phone 上收到通知,除非他们在桌面应用程序上处于非活动状态超过 3 分钟。否则,这可能是 Teams 中的错误。
聊天通知
如果您按照上面链接的故障排除指南进行操作,您的用户应该会收到聊天通知。如果没有,您可以尝试更新您的 MS Teams 桌面或移动客户端。正如@KyleDelaney 提到的,@
提及用户 and/or 频道
可能会有所帮助
Activity 订阅源通知
你也可以create Activity Feed Notifications。其要点是您需要:
- 在消息中包含
text
和 summary
- 包括将
notifications.alert
设置为 true 的 channelData
这段代码将完成:
const msg = MessageFactory.text('my message');
msg.summary = 'my summary';
msg.channelData = {
notification: {
alert: true,
},
};
return await dc.context.sendActivity(msg);
结果:
注意:如果您的机器人 仅 创建通知并且没有对话,您可能会受益于 creating a notifications-only bot。
完整的实现代码
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
await turnContext.sendActivity(activity);
结果:
使用 Teams Extension
BobBuilder V4 的 Teams 扩展目前处于 Beta 阶段,但似乎可以满足您的需求。我相信您在使用上述内容时没有收到通知的原因是因为您的机器人正在频道中创建一个新的回复链而不是直接回复用户。我相信你可以在没有扩展的情况下完成所有这些(通过手动编辑 activity/context 属性),但扩展应该更容易。
这是我用来在频道内获取工作通知的代码:
在index.js
(或app.js
)中:
import * as teams from 'botbuilder-teams';
[...]
// Change existing to use "new teams.TeamsAdapter..."
const adapter = new teams.TeamsAdapter({
appId: endpointConfig.appId || process.env.microsoftAppID,
appPassword: endpointConfig.appPassword || process.env.microsoftAppPassword,
});
无论您将消息发送到哪里:
import * as teams from 'botbuilder-teams';
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
const adapter = context.adapter as teams.TeamsAdapter;
await adapter.createReplyChain(context, [activity]);
简化代码
OP 和我来回发送了一些电子邮件,关键问题是他需要从下面添加 trustServiceUrl
代码。通常,这会显示 500 错误,但在这种情况下,它似乎不会创建通知。经过大量测试后,您真正需要做的就是将不同的通知发送到不同的渠道。它基本上相当于设置 turncontext.activity 的几个属性并信任 serviceUrl。根本没有接触 activity ID 或使用 Teams Extension。我下面的代码是我如何从模拟器发送消息,然后可以将卡片发送到不同的团队频道:
public onTurn = async (turnContext: TurnContext) => {
const dc = await this.dialogs.createContext(turnContext);
const dialogResult = await dc.continueDialog();
// Route message from Emulator to Teams Channel - I can send "1", "2", or "3" in emulator and bot will create message for Channel
let teamsChannel;
switch (turnContext.activity.text) {
// You can get teamsChannel IDs from turnContext.activity.channelData.channel.id
case '1':
teamsChannel = '19:8d60061c3d104exxxxxxxxxxxxxxxxxx@thread.skype';
break;
case '2':
teamsChannel = '19:0e477430ebad4exxxxxxxxxxxxxxxxxx@thread.skype';
break;
case '3':
teamsChannel = '19:55c1c5fb0d304exxxxxxxxxxxxxxxxx0@thread.skype';
break;
default:
break;
}
if (teamsChannel) {
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
summary: 'my summary',
text: 'Test Card',
};
const serviceUrl = 'https://smba.trafficmanager.net/amer/';
turnContext.activity.conversation.id = teamsChannel;
turnContext.activity.serviceUrl = serviceUrl;
// This ensures that your bot can send to Teams
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
await turnContext.sendActivity(activity);
} else {
[...Normal onTurn Code...]
await this.conversationState.saveChanges(turnContext);
}
注意:要接收通知,您和您的用户必须关注频道。
我已经完成了您上面描述的所有步骤。我还完成了故障排除步骤。但是,它仍然没有给我卡片的通知。但是,当我发起与机器人的对话时,当机器人响应时我会收到通知。
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
在此图中,当我启动 TMS 机器人时会收到通知!信息。但是,我没有收到接下来两条消息的通知。
我使用 Microsoft Bot Framework v4 Nodejs SDK(botbuilder-sdk for nodejs)为 Microsoft Teams 开发了一个机器人。我们以这样一种方式实施了机器人,当我们使用来自我们的一个 CRM 的 REST API 调用接收数据时,数据将发布到 Microsoft Teams 上的频道。但是,当我这样做时,我们不会在设备上收到通知。有人遇到过这样的问题吗?
我最初是在保存上下文状态。每次我们从 CRM 接收数据时,我都会增加消息的 activity id(将其作为新消息发送而不是回复)并使用 context.sendActivity() 将其发送到 Microsoft Teams。
当我们收到自适应卡片时,我们不会在 activity 提要或任何设备上收到通知。
我已经完成了您上面描述的所有步骤。我还完成了故障排除步骤。但是,它仍然没有给我卡片的通知。但是,当我发起与机器人的对话时,当机器人响应时我会收到通知。
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
在此图中,当我启动 TMS Bot 时会收到通知!信息。但是,我没有收到接下来两条消息的通知。
编辑:OP 和我交换了几封电子邮件以得到答复。总的来说,这个答案是完成团队主动消息传递的好信息,一般来说,但主要答案在最后一部分,简化代码中。
这是一个涵盖很多领域的长答案,只是因为我不能 100% 确定我知道您没有收到哪种通知。
疑难解答
- Troubleshooting Guide
- 特别注意需要启用通知的许多区域
- 特别是,用户可能需要"Follow"and/or"Favorite"接收通知的频道
- 特别注意需要启用通知的许多区域
- 如果用户打开了桌面应用程序,他们将在那里收到通知,并且不会在 phone 上收到通知,除非他们在桌面应用程序上处于非活动状态超过 3 分钟。否则,这可能是 Teams 中的错误。
聊天通知
如果您按照上面链接的故障排除指南进行操作,您的用户应该会收到聊天通知。如果没有,您可以尝试更新您的 MS Teams 桌面或移动客户端。正如@KyleDelaney 提到的,@
提及用户 and/or 频道
Activity 订阅源通知
你也可以create Activity Feed Notifications。其要点是您需要:
- 在消息中包含
text
和summary
- 包括将
notifications.alert
设置为 true 的
channelData
这段代码将完成:
const msg = MessageFactory.text('my message');
msg.summary = 'my summary';
msg.channelData = {
notification: {
alert: true,
},
};
return await dc.context.sendActivity(msg);
结果:
注意:如果您的机器人 仅 创建通知并且没有对话,您可能会受益于 creating a notifications-only bot。
完整的实现代码
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
await turnContext.sendActivity(activity);
结果:
使用 Teams Extension
BobBuilder V4 的 Teams 扩展目前处于 Beta 阶段,但似乎可以满足您的需求。我相信您在使用上述内容时没有收到通知的原因是因为您的机器人正在频道中创建一个新的回复链而不是直接回复用户。我相信你可以在没有扩展的情况下完成所有这些(通过手动编辑 activity/context 属性),但扩展应该更容易。
这是我用来在频道内获取工作通知的代码:
在index.js
(或app.js
)中:
import * as teams from 'botbuilder-teams';
[...]
// Change existing to use "new teams.TeamsAdapter..."
const adapter = new teams.TeamsAdapter({
appId: endpointConfig.appId || process.env.microsoftAppID,
appPassword: endpointConfig.appPassword || process.env.microsoftAppPassword,
});
无论您将消息发送到哪里:
import * as teams from 'botbuilder-teams';
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
const adapter = context.adapter as teams.TeamsAdapter;
await adapter.createReplyChain(context, [activity]);
简化代码
OP 和我来回发送了一些电子邮件,关键问题是他需要从下面添加 trustServiceUrl
代码。通常,这会显示 500 错误,但在这种情况下,它似乎不会创建通知。经过大量测试后,您真正需要做的就是将不同的通知发送到不同的渠道。它基本上相当于设置 turncontext.activity 的几个属性并信任 serviceUrl。根本没有接触 activity ID 或使用 Teams Extension。我下面的代码是我如何从模拟器发送消息,然后可以将卡片发送到不同的团队频道:
public onTurn = async (turnContext: TurnContext) => {
const dc = await this.dialogs.createContext(turnContext);
const dialogResult = await dc.continueDialog();
// Route message from Emulator to Teams Channel - I can send "1", "2", or "3" in emulator and bot will create message for Channel
let teamsChannel;
switch (turnContext.activity.text) {
// You can get teamsChannel IDs from turnContext.activity.channelData.channel.id
case '1':
teamsChannel = '19:8d60061c3d104exxxxxxxxxxxxxxxxxx@thread.skype';
break;
case '2':
teamsChannel = '19:0e477430ebad4exxxxxxxxxxxxxxxxxx@thread.skype';
break;
case '3':
teamsChannel = '19:55c1c5fb0d304exxxxxxxxxxxxxxxxx0@thread.skype';
break;
default:
break;
}
if (teamsChannel) {
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
summary: 'my summary',
text: 'Test Card',
};
const serviceUrl = 'https://smba.trafficmanager.net/amer/';
turnContext.activity.conversation.id = teamsChannel;
turnContext.activity.serviceUrl = serviceUrl;
// This ensures that your bot can send to Teams
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
await turnContext.sendActivity(activity);
} else {
[...Normal onTurn Code...]
await this.conversationState.saveChanges(turnContext);
}
注意:要接收通知,您和您的用户必须关注频道。
我已经完成了您上面描述的所有步骤。我还完成了故障排除步骤。但是,它仍然没有给我卡片的通知。但是,当我发起与机器人的对话时,当机器人响应时我会收到通知。
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
在此图中,当我启动 TMS 机器人时会收到通知!信息。但是,我没有收到接下来两条消息的通知。