Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c# - How do I send chat transcript? SendConversationHistoryAsync() not working

There is a sample for Conversation History: https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/22.conversation-history

Actually sending the transcript doesn't work in my attempts to run it. Specifically this line:

await connectorClient.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, cancellationToken: cancellationToken);

I get the following exception:

ConversationHistory> fail: Microsoft.BotBuilderSamples.ConversationHistoryBot[0] ConversationHistory> Exception caught : System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. ---> System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request ConversationHistory> --- End of inner exception stack trace --- ConversationHistory> at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error) ConversationHistory> at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token) ConversationHistory> at System.Net.Http.HttpConnection.FillAsync() ConversationHistory>
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed) ConversationHistory> at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) ConversationHistory>
--- End of inner exception stack trace --- ConversationHistory> at Microsoft.BotBuilderSamples.ConversationHistoryBot.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken) in C:UsersOyensource eposBotBuilder-Samplessamplescsharp_dotnetcore22.conversation-historyConversationHistoryBot.cs:line 99 ConversationHistory> at Microsoft.Bot.Builder.TranscriptLoggerMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate nextTurn, CancellationToken cancellationToken) in D:a1slibrariesMicrosoft.Bot.BuilderTranscriptLoggerMiddleware.cs:line 105 ConversationHistory> at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) in D:a1slibrariesMicrosoft.Bot.BuilderMiddlewareSet.cs:line 55 ConversationHistory> at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) in D:a1slibrariesMicrosoft.Bot.BuilderBotAdapter.cs:line 167

I can confirm that the transcript files are saved in my blob storage, and I can iterate through the activities retrieved from the blob.

(1) What am I missing to get SendConversationHistoryAsync() to work?

(2) What does the actual transcript look like when sent? (Is it worth it to just iterate through my activities and handle each activity type and make my own conversation history message?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ConversationHistory sample functions as expected. But, when using the WebChat or Emulator channels, the Activity.Ids must be updated. Otherwise, the WebChat control will filter them out if already present:

bool updateActivities = new[] { Channels.Webchat, Channels.Emulator, Channels.Directline, }
                             .Contains(activity.ChannelId);
var incrementId = 0;
//get current id to ensure we do not overlap
if (updateActivities && activity.Id.Contains("|"))
{
     int.TryParse(activity.Id.Split('|')[1], out incrementId);
}

/* get activities */

foreach (var a in activities)
{
  incrementId++;
  a.Id = string.Concat(activity.Conversation.Id, "|", incrementId.ToString().PadLeft(7, '0'));
  a.Timestamp = DateTimeOffset.UtcNow;
  a.ChannelData = string.Empty; // WebChat uses ChannelData for id comparisons, so clear it
}

var transcript = new Transcript(activities);

await connectorClient.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, 
                                transcript, cancellationToken: cancellationToken);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...