简体   繁体   中英

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.

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.

 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.

I used Microsoft.Bot.Connector.ConnectorClient but StartconversationAsync and PostActivityAsync methods are not available. I tried the methods available in Microsoft.Bot.Connector.ConnectorClient

  • connectorClient.Conversations.CreateConversationAsync(conversationparameters)
  • connectorClient.ConversationsCreateDirectConversationAsync(botAccount, userAccount, (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"}}

The newActivity is created as below:

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.

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. 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
  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 .

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. you can check if the activity id is the same then return the request else go with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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