【发布时间】:2020-02-08 22:41:40
【问题描述】:
我正在尝试显示用户与我的机器人的聊天历史记录,以便当他们回来时可以看到他们之前的对话。当我尝试“SendConversationHistory(conversationId, transcripts)”功能时,我不断收到 Bad Request (400) 并且聊天机器人中不显示历史记录。有人知道如何解决这个问题吗?
我在 C# 中使用 BotFramework SDK v.3。
我所做的是: 1. 检查是否存储了任何对话 ID,以便能够使用该对话 ID 重新连接 2.我向机器人发送一个事件来恢复历史 3. 我能够获取以前的活动并创建成绩单
设置我的成绩单后,我尝试了这个不起作用的 SendConversationHistory 函数。
即使使用旧对话 ID 失败,我也可以与机器人聊天。我还尝试让机器人做一个回复活动,这很有效。
我关注了a js sample (v4)、this stackoverflow question 和this thread about history in github,但这些都没有帮助解决这个问题。
MessageController.cs(在 HandleSystemMessage 中)
else if (message.Name.Equals("restoreHistory"))
{
string convId = message.Conversation.Id;
List<Activity> activities = JsonConvert.DeserializeObject<List<Activity>>((string)message.Value);
var incrementId = 0;
if (message.Id.Contains("|"))
int.TryParse(message.Id.Split('|')[1], out incrementId);
foreach(var a in activities)
{
incrementId++;
a.Id = string.Concat(convId, "|", incrementId.ToString().PadLeft(7, '0'));
a.ChannelData = string.Empty;
}
if (activities != null && activities.Count > 0)
{
var connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
var transcripts = new Transcript(activities);
connector.Conversations.SendConversationHistory(convId, transcripts);
}
}
default.htm
var botConnection = new BotChat.DirectLine({
secret: ...,
conversationId: getPersistedConversationId(),
token: params['t'],
domain: params['domain'],
webSocket: params['webSocket'] && params['webSocket'] === "true"
});
BotChat.App({
botConnection: botConnection,
user: { id: ... },
bot: { id: ... },
speechOptions: speechOptions
}, document.getElementById("BotChatGoesHere"));
botConnection.connectionStatus$
.subscribe(function (connectionStatus) {
switch (connectionStatus) {
case 2:
if (botConnection.conversationId === localStorage.getItem('conversationId')) {
botConnection.postActivity({
from: user,
type: 'event',
name: 'restoreHistory',
text: '',
value: localStorage.getItem('conversations')
}).subscribe(function (activityId) {
console.log('sending chat history.');
});
} else {
saveConversationId(botConnection.conversationId);
}
}
});
function saveConversationId(conversationId) {
localStorage.setItem('conversationId', conversationId);
}
function getPersistedConversationId() {
return localStorage.getItem('conversationId');
}
我得到的错误是那些:
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in Microsoft.Rest.ClientRuntime.dll
Response status code indicates server error: 400 (BadRequest).
活动的 Json
[
{
"type":"message",
"id":"6Tklwkie30H7oCI1eyWx9i-h|0000001",
"timestamp":"2019-06-19T10:37:40.1175096Z",
"channelId":"directline",
"from":{
"id":"MyBotName",
"name":"MyBotName"
},
"conversation":{
"id":"6Tklwkie30H7oCI1eyWx9i-h"
},
"text":"Hello, I'm MyBotName. How can I help you? To get started ask me a question.",
"attachments":[
],
"entities":[
],
"replyToId":"7E6AtBm9iXL"
},
{
"type":"message",
"id":"6Tklwkie30H7oCI1eyWx9i-h|0000003",
"timestamp":"2019-06-19T10:37:43.4034223Z",
"localTimestamp":"2019-06-19T10:37:43.2484146+00:00",
"channelId":"directline",
"from":{
"id":"MyBotName",
"name":"MyBotName"
},
"conversation":{
"id":"6Tklwkie30H7oCI1eyWx9i-h"
},
"attachmentLayout":"carousel",
"locale":"en-US",
"text":"",
"attachments":[
{
"contentType":"application/vnd.microsoft.card.hero",
"content":{
"title":"Title1",
"subtitle":"",
"text":"",
"images":[
{
"url":"http://..."
}
],
"buttons":[
{
"type":"openUrl",
"title":"Button1",
"value":"https://..."
},
{
"type":"openUrl",
"title":"Button2",
"value":"https://..."
}
]
}
},
{
"contentType":"application/vnd.microsoft.card.hero",
"content":{
"title":"Title2",
"subtitle":"",
"text":"",
"images":[
{
"url":"http://..."
}
],
"buttons":[
{
"type":"openUrl",
"title":"Button1",
"value":"https://..."
},
{
"type":"openUrl",
"title":"Button2",
"value":"https://..."
}
]
}
},
{
"contentType":"application/vnd.microsoft.card.hero",
"content":{
"title":"Title3",
"subtitle":"",
"text":"",
"images":[
{
"url":"http://..."
}
],
"buttons":[
{
"type":"openUrl",
"title":"Button1",
"value":"https://..."
},
{
"type":"openUrl",
"title":"Button2",
"value":"https://..."
}
]
}
}
],
"entities":[
],
"replyToId":"6Tklwkie30H7oCI1eyWx9i-h|0000000"
},
{
"type":"message",
"id":"6Tklwkie30H7oCI1eyWx9i-h|0000000",
"timestamp":"2019-06-19T10:37:33.8526334Z",
"serviceUrl":"https://directline.botframework.com/",
"channelId":"directline",
"from":{
"id":"anonymous",
"name":"Anonymous"
},
"conversation":{
"id":"6Tklwkie30H7oCI1eyWx9i-h"
},
"recipient":{
"id":"MyBotName@WI6mwV4z0jY",
"name":"MyBotName"
},
"textFormat":"plain",
"locale":"en-US",
"text":"i'm looking for this",
"entities":[
{
"type":"ClientCapabilities",
"requiresBotState":true,
"supportsTts":true,
"supportsListening":true
}
],
"channelData":{
"clientActivityId":"1560940638882.6146258363791712.0"
}
}
]
【问题讨论】:
-
你用的是什么渠道?
-
@KyleDelaney 我正在使用 DirectLine 频道
-
您使用的是网络聊天还是自定义 Direct Line 客户端?
-
我们正在使用网络聊天。我们连接到类似于this 的机器人
-
您使用的是网络聊天 v3 还是 v4?
标签: c# botframework