简体   繁体   English

在 MS Teams Bot 中管理长期运行的操作

[英]Manage a long-running operation in MS Teams Bot

I am using the following sample / article to Manage a Long-running operation in MS Teams Bot.我正在使用以下示例/文章来管理 MS Teams Bot 中的长时间运行的操作。

https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-long-operations-guidance?view=azure-bot-service-4.0 https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-long-operations-guidance?view=azure-bot-service-4.0

In step 5, a DirectLineClient is being created and an Event Activity is sent to Bot using PostActivityAsync.在第 5 步中,正在创建 DirectLineClient,并使用 PostActivityAsync 将 Event Activity 发送到 Bot。

 var responseActivity =  new Activity("event");
 responseActivity.Value = originalActivity;
 responseActivity.Name = "LongOperationResponse";
 responseActivity.From = new ChannelAccount("GenerateReport", "AzureFunction");
 var directLineSecret = Environment.GetEnvironmentVariable("DirectLineSecret");
 using(DirectLineClient client = new DirectLineClient(directLineSecret))
 {
    var conversation = await client.Conversations.StartConversationAsync();
    await client.Conversations.PostActivityAsync(conversation.ConversationId, responseActivity);
 }

However, I need the above sample to work for MS Teams Bot and not the DirectLineClient.但是,我需要上面的示例来为 MS Teams Bot 而不是 DirectLineClient 工作。

I used Microsoft.Bot.Connector.ConnectorClient but StartconversationAsync and PostActivityAsync methods are not available.我使用了 Microsoft.Bot.Connector.ConnectorClient 但 StartconversationAsync 和 PostActivityAsync 方法不可用。 I tried the methods available in Microsoft.Bot.Connector.ConnectorClient我尝试了 Microsoft.Bot.Connector.ConnectorClient 中可用的方法

  • connectorClient.Conversations.CreateConversationAsync(conversationparameters) connectorClient.Conversations.CreateConversationAsync(conversationparameters)
  • connectorClient.ConversationsCreateDirectConversationAsync(botAccount, userAccount, (Activity)newActivity); connectorClient.ConversationsCreateDirectConversationAsync(botAccount, userAccount, (Activity)newActivity);
  • connectorClient.Conversations.SendToConversationAsync(conversationid, (Activity)newActivity); connectorClient.Conversations.SendToConversationAsync(conversationid, (Activity)newActivity);

But all the methods failed with Bad Requestwith the error as seen in the Response: {"error":{"code":"BadArgument","message":"Unknown activity type"}}但是所有方法都因错误请求而失败,错误如响应中所示:{"error":{"code":"BadArgument","message":"Unknown activity type"}}

The newActivity is created as below: newActivity 创建如下:

var messagnewActivity = new Activity("event");
newActivity.Value = originalActivity;
newActivity.From = new ChannelAccount("GenerateReport", "AzureFunction");
newActivity.Type = "event";
newActivity.Conversation = new ConversationAccount {  Id = originalActivity.Conversation.Id  };
newActivity.ChannelId = originalActivity.ChannelId;

Can someone please suggest how do I pass the Activity (Event Activity type) to MS Teams Bot.有人可以建议我如何将活动(事件活动类型)传递给 MS Teams Bot。

Thanks Gagan谢谢加根

I'm not really familiar with Direct Line, but I think it's effectively an -alternative- type of bot to Teams, so if you're trying to do this inside Teams, it explains the issue.我对 Direct Line 不是很熟悉,但我认为它实际上是 Teams 的一种替代类型的机器人,因此如果您尝试在 Teams 内部执行此操作,它可以解释问题。 In principle, the basic idea is quite simple though:原则上,基本思想很简单:

  1. you store state somehow (eg in memory or in a database) to indicate that the long running operation is in progress for the user您以某种方式存储 state(例如在 memory 或数据库中)以指示用户正在进行长时间运行的操作
  2. when the long-running process is complete, your code (which could live OUTSIDE your bot, eg in an Azure Function) can send the user a message AS IF IT WAS the bot - this is called Pro-Active Messaging and you can read more about it at https://docs.microsoft.com/en-us/graph/teams-proactive-messaging .当长时间运行的过程完成后,您的代码(可能位于您的机器人之外,例如在 Azure 函数中)可以向用户发送一条消息,就像它是机器人一样 - 这称为主动消息传递,您可以阅读更多在https://docs.microsoft.com/en-us/graph/teams-proactive-messaging关于它。

This is to inform you that I was facing the same issue sometime before then I found a tweak in the code while debugging.这是为了通知您,在此之前的某个时候我遇到了同样的问题,然后我在调试时发现了代码中的一个调整。 when it calls twice recursively then the Activity Id is the same as the previous one.当它递归调用两次时,Activity Id 与前一个相同。 you can check if the activity id is the same then return the request else go with it.您可以检查活动 id 是否相同,然后返回请求,否则返回请求 go 。

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

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