我正在使用针对NodeJS的Bot Framework SDK v4为Microsoft Teams开发机器人。有没有办法机器人可以自动发起通道中的对话,而不是用户发起对话?当用户发起对话时,我的机器人工作正常。关于我如何处理此事的任何建议?
MS团队称之为“主动消息”(注意:Bot框架通常将“主动消息”定义为向用户发送与当前会话无关的消息,您可以参考该消息。团队将一些内容归入此类别)。您可以从官方团队文档中了解有关how to use proactive messaging的更多信息。或者,更具体地说,creating a channel conversation。
它的要点是你需要capture a conversationUpdate
and check for a new member added to the conversation或fetch the team roster,然后你send the proactive message。
注意:对于MS团队,用户或团队必须首先添加机器人:
只要您的机器人具有通过以前添加在个人或团队范围内获得的用户信息,机器人就可以与单独的Microsoft团队用户创建新的对话。此信息使您的机器人能够主动通知他们。例如,如果您的机器人被添加到团队中,它可以查询团队名单并在个人聊天中向用户发送单独的消息,或者用户可以@mention另一个用户触发机器人向该用户发送直接消息。
一些开发人员在使用主动消息传递时遇到401: Unauthorized
错误,特别是如果机器人由于某种原因重新启动并且机器人试图重新发起主动消息。您可以阅读更多有关阻止by using trustServiceUrl
from this Sample的信息(这是我的分支,用于提交Pull Request以使用trustServiceUrl
信息更新主动样本)。
您可以使用Botframework V4和Teams Extensions V4中的连接器客户端发起全新的对话。在nodejs中,您将在this Github Issue的其中一条注释中找到解决方案。对于那些在C#中寻找解决方案的人来说,here is a detailed blog post关于在僵尸网络框架的C#版本中实现这一点。
在nodejs中:
var conversationReference = TurnContext.getConversationReference(context.activity)
connectorClient = await createConnectorClient(context)
var conversationParameters = {
isGroup: true,
bot: conversationReference.bot,
channelData: (await teamsCtx.getTeamsChannelData()),
tenantId: teamsCtx.tenant.id,
activity: MessageFactory.text("Queue Summary Placeholder") as Activity
} as ConversationParameters
await connectorClient.conversations.createConversation(conversationParameters)
在C#中
ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient());
var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();
var conversationParameter = new ConversationParameters
{
Bot = turnContext.Activity.Recipient,
IsGroup = true,
ChannelData = channelData,
TenantId = channelData.Tenant.Id,
Activity = MessageFactory.Text(message)
};
var response = await _client.Conversations.CreateConversationAsync(conversationParameter);