如何发送聊天记录? SendConversationHistoryAsync()不起作用

时间:2018-11-06 23:53:19

标签: c# botframework

有一个“对话历史记录”示例:https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/22.conversation-history

实际上,在我尝试运行成绩单时,无法发送成绩单。具体来说这行:

TextField

我收到以下异常:

  

ConversationHistory>失败:   Microsoft.BotBuilderSamples.ConversationHistoryBot [0]   ConversationHistory>捕获到异常:   System.Threading.Tasks.TaskCanceledException:该操作是   取消。 ---> System.IO.IOException:无法从   传输连接:由于以下原因,I / O操作已中止   线程退出或应用程序请求。 ->   System.Net.Sockets.SocketException:I / O操作已中止   由于线程退出或应用程序请求   ConversationHistory>-内部异常堆栈跟踪结束   --- ConversationHistory>,位于System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError   错误)ConversationHistory>在   System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16   令牌)ConversationHistory>在   System.Net.Http.HttpConnection.FillAsync()对话历史记录>
  在   System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(布尔   折叠后的标题),会话历史记录>,位于   System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage   请求,CancellationToken取消Token)ConversationHistory>
  ---内部异常堆栈跟踪的结尾--- ConversationHistory>在   Microsoft.BotBuilderSamples.ConversationHistoryBot.OnTurnAsync(ITurnContext   turnContext,CancellationToken cancelToken)   C:\ Users \ Oyen \ source \ repos \ BotBuilder-Samples \ samples \ csharp_dotnetcore \ 22.conversation-history \ ConversationHistoryBot.cs:line   99会话历史>   Microsoft.Bot.Builder.TranscriptLoggerMiddleware.OnTurnAsync(ITurnContext   turnContext,NextDelegate nextTurn,CancellationToken   cancelToken)   D:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ TranscriptLoggerMiddleware.cs:line   105会话历史>   Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext   turnContext,BotCallbackHandler回调,CancellationToken   cancelToken)在   D:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ MiddlewareSet.cs:第55行   会话历史>   Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext   turnContext,BotCallbackHandler回调,CancellationToken   cancelToken)在   D:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ BotAdapter.cs:第167行

我可以确认脚本文件已保存在我的Blob存储中,并且可以遍历从Blob中检索到的活动。

(1)要使SendConversationHistoryAsync()正常工作,我缺少什么?

(2)发送时的实际成绩单看起来像什么? (仅遍历我的活动并处理每种活动类型并发出自己的对话历史记录消息是否值得?)

1 个答案:

答案 0 :(得分:2)

对话历史示例功能正常。但是,使用 WebChat Emulator 通道时,必须更新Activity.Id。否则,WebChat控件将过滤掉它们(如果已经存在):

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);