繁体   English   中英

从群聊到 MS Teams 中的个人聊天与 Bot 框架的主动对话

[英]Proactive Dialog with the Bot Framework from Group chat to Personal chat in MS Teams

可以使用下一部分代码向团队群组聊天中的消息发送者发送主动消息(=私人):

if (stepContext.Context.Activity.ChannelId == Channels.Msteams &&
    stepContext.Context.Activity.Conversation.IsGroup.HasValue &&
    stepContext.Context.Activity.Conversation.IsGroup.Value) {
        var teamConversationData = stepContext.Context.Activity.GetChannelData<TeamsChannelData>();
        var connectorClient = new ConnectorClient(new Uri(stepContext.Context.Activity.ServiceUrl), _credentialOptions.MicrosoftAppId, _credentialOptions.MicrosoftAppPassword);
        var userId = stepContext.Context.Activity.From.Id;
        var tenantId = teamConversationData.Tenant.Id;
        var parameters = new ConversationParameters
            {
                  Members = new[] { new ChannelAccount(userId) },
                  ChannelData = new TeamsChannelData
                  {
                       Tenant = new TenantInfo(tenantId),
                  },
            };
        var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters, cancellationToken: cancellationToken);
        var message = Activity.CreateMessageActivity();
        message.Text = "This is a proactive message. I've sent it from a group conversation.";
        await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)message, cancellationToken: cancellationToken);
}

但问题是我还希望能够为该调用用户触发一个对话框,而不是发送一个活动。 搜索 web 后看起来不可能? 我玩了一下代码,发现我可以替换

await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)message, cancellationToken: cancellationToken);

stepContext.Context.Activity.Conversation.Id = conversationResource.Id;
stepContext.Context.Activity.Conversation.IsGroup = false;

这样就可以将对话发送到发送用户的私人聊天中。

当我查看数据库时,我可以看到已保存的用户对话框堆栈填充了对话框(等待用户交互)。 我还可以看到仍然为空的群组对话对话框堆栈,因此该机器人在群组聊天和其他用户中仍然响应。

当对话结束时,我将 Conversation.Id 和.IsGroup 放回我保存在上下文中的先前群聊值,并且它能够像我希望的那样在群聊中传递最终答案.

基本上我的代码可以切换到在私人中间对话中发送内容,并且在对话中的任何其他点它可以切换回群组对话。

问题是关于我在对话中更改对话 ID。 这是正常的事情吗? 这会破坏我还没有想到的东西吗?

我认为这种方法没有任何问题,但我认为通常你会开始一个新的对话,而不是继续使用同一个对话。

我认为当 state 被保存时,在黑客攻击之前访问 state 的任何代码都将保留在新 ID 下。 在回合之前运行的中间件与在回合完成之后运行的中间件将有两个单独的对话 ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM