我的机器人以一种方式使用,当用户键入问题时,它将重定向对话框并根据问题返回答案。目前,我正在尝试存储对话历史记录。添加中间件是存储过程的一部分,我遇到了一个问题,即“未定义”意外地出现在消息日志中。
我的代码段
bot.dialog('cards', [
function (session) {
session.send('Test1');
var msg = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel)
.textFormat(builder.TextFormat.xml)
.attachments([
new builder.HeroCard(session)
.title('Test1')
.images([
builder.CardImage.create(session, 'imgURL')
])
.buttons([builder.CardAction.dialogAction(session, 'Card1', null, 'Here')
])
]);
msg.addAttachment (
new builder.HeroCard(session)
.title("Test2")
.images([
builder.CardImage.create(session, "imgUrl")
])
.buttons([
builder.CardAction.dialogAction(session, "Card2", null, "Here")
])
);
session.endDialog(msg)
}
]).triggerAction({
matches: 'Greetings'})
//Adding of new card
bot.dialog('Card1', [
function (session) {
var msg1 = new builder.Message(session).sourceEvent({
//specify the channel
facebook: {
text:"Card1"
}
});
session.endDialog(msg1)
session.beginDialog('card1link');
}
]).triggerAction({
matches: 'card1'})
bot.dialog('card1link', [
function (session, args, next) {
builder.Prompts.choice(session, '1. Card2\n2. Card3\n', ['1', '2'], {
retryPrompt: "Please pick your choice.\n1. Card2\n2. Card3\n",
maxRetries: 1
});
},
function (session, args, next) {
if (args.response) {
var choice = args.response.entity;
switch (choice) {
case '1':
session.replaceDialog('Card2');
break;
case '2':
session.replaceDialog('Card3');
break;
}
}
else{
session.send("Sorry");
}
}
]);
输出
(User-to-Bot)消息:嗨
(Bot-to-User)消息:Test1
(Bot-to-User)消息:未定义
(User-to-Bot)消息:Card1
(Bot-to-User)消息:未定义
(Bot-to-User)消息:请选择您的选择。 1.卡2 2. Card3
预期
(User-to-Bot)消息:嗨
(Bot-to-User)消息:Test1
(User-to-Bot)消息:Card1
(Bot-to-User)消息:请选择您的选择。 1.卡2 2. Card3
答案 0 :(得分:1)
您尚未在recieve
或send
中间件中提供代码段。假设您使用以下代码记录历史记录:
bot.use({
receive: (evt, next) => {
//handle evt
//what you save should be `evt.text` as the message
next();
},
send: (evt, next) => {
//handle evt
//what you save should be `evt.text` as the message
next();
}
})
当您的机器人获得(Bot-to-User) message: undefined
时,它应该触发endOfConversation
事件。此事件的结构如下:
{
address:Object {id: "9n1h124ddkb2", channelId: "emulator", user: Object, …}
code:"completedSuccessfully"
textLocale:"en-US"
type:"endOfConversation"
}
其中不包含text
属性。
这应该是罪犯。
您可以添加条件以避免这种情况:
receive: (evt, next) => {
console.log(evt);
if (evt.type == 'message') {
your logic here
}
next();
}
答案 1 :(得分:-1)
您可以使用同义词和retryPrompt。
builder.Prompts.choice(
session,
"Selection one option",
[
{
value: "1",
synonyms: ["Card1", "Card 1"]
},
{
value: "2",
synonyms: ["Card2", "Card 2"]
}
],
{
listStyle: builder.ListStyle.button,
retryPrompt: 'Selection one option.'
}
)