Bot Framework v4 - Bot 发起对话

Bot Framework v4 - Bot Initiate Conversation

我正在使用适用于 NodeJS 的 Bot Framework SDK v4 为 Microsoft Teams 开发一个机器人。有没有一种方法可以让机器人自动在频道中发起对话,而不是用户发起对话?当用户发起对话时,我的机器人工作正常。对我如何进行此操作有什么建议吗?

MS Teams 称其为 "Proactive Message"(注意:Bot Framework 通常将 "proactive message" 定义为向用户发送与当前对话无关的消息,您可以参考该消息。Teams 将 a很少有东西属于这一类)。您可以阅读有关 how to use proactive messaging from the official Teams docs. Or, more specifically, creating a channel conversation.

的更多信息

要点是你需要capture a conversationUpdate and check for a new member added to the conversation or fetch the team roster, then you send the proactive message

注意:对于 MS Teams,用户或团队必须先添加机器人:

Bots can create new conversations with an individual Microsoft Teams user as long as your bot has user information obtained through previous addition in a personal or team scope. This information enables your bot to proactively notify them. For instance, if your bot was added to a team, it could query the team roster and send users individual messages in personal chats, or a user could @mention another user to trigger the bot to send that user a direct message.

一些开发人员在使用主动消息传递时遇到 401: Unauthorized 错误,尤其是当机器人出于某种原因重新启动并且机器人试图重新启动主动消息时。您可以阅读有关防止 by using trustServiceUrl from this Sample 的更多信息(这是我的分支,用于提交拉取请求以使用 trustServiceUrl 信息更新主动示例)。

您可以使用 Botframework V4 和 Teams Extensions V4 中的连接器客户端发起全新对话。在 nodejs 中,您将在 this Github Issue. For anyone looking for a solution in C#, here is a detailed blog post 的评论之一中找到关于在 botframework 的 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);

我们确实需要知道您希望机器人何时发送消息,机器人框架 TeamsActivityHandler class 提供了多个 methods 供您使用,例如:

  1. onMembersAdded(BotHandler):为成员添加事件注册一个 activity 事件处理程序,为任何传入的对话更新发出 activity,其中包括添加到对话的成员。

详细了解您可以使用的事件/方法 here

我最终弄明白了,我写了一个机器人控制器,我可以用下面的代码按需调用它。

var conversationParameters = new ConversationParameters
            {
                IsGroup = true,
                ChannelData = new TeamsChannelData
                {
                    // this needs to come from the teams context.
                    Channel = new ChannelInfo(channelId),
                },
                Activity = (Activity)MessageFactory.Attachment(attachment)
            };
            // your service url may differ.
            MicrosoftAppCredentials.TrustServiceUrl(String.IsNullOrEmpty(serviceUrl) ? constantServiceUrl : serviceUrl, DateTime.MaxValue);
            var response = connectorClient.Conversations.CreateConversationAsync(conversationParameters).GetAwaiter().GetResult();