【问题标题】:Azure Bot Framework: Bot responds with “undefined” (Node.js)Azure Bot Framework:Bot 响应“未定义”(Node.js)
【发布时间】:2018-04-12 10:04:01
【问题描述】:

我的机器人的使用方式是,当用户键入问题时,它会重定向对话框并根据问题返回答案。目前,我正在尝试存储对话历史记录。添加中间件是存储过程的一部分,我遇到了一个问题,即“undefined”意外出现在消息日志中。

我的代码 sn-p

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");
        }
    }
]);

输出

(用户到机器人)消息:嗨

(机器人到用户)消息:Test1

(机器人到用户)消息:未定义

(用户到机器人)消息:Card1

(机器人到用户)消息:未定义

(机器人到用户)消息:请选择您的选择。 1.卡2 2.卡片3

预期

(用户到机器人)消息:嗨

(机器人到用户)消息:Test1

(用户到机器人)消息:Card1

(机器人到用户)消息:请选择您的选择。 1.卡2 2.卡片3

【问题讨论】:

  • 您在哪个频道看到了这种行为,这是在 Azure 上的测试 WebChat 窗格中吗?
  • 虽然由于路由的一些问题我无法完成预期的步骤,但复制粘贴您的代码成功地在模拟器中呈现了一个轮播。
  • 抱歉,我刚刚看到了 Facebook 标签。我不确定 Facebook 是否支持 Rich Cards/Templates 的 XML 格式
  • @StevenG。我可以在 FB 上展示丰富的卡片。有没有办法省略“未定义”这个词?
  • “未定义”这个词是什么时候产生的?就在向用户显示任何卡片之前?

标签: node.js facebook azure botframework middleware


【解决方案1】:

您尚未在 recievesend 中间件中提供代码 sn-p。假设您正在使用以下代码记录历史记录:

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

【讨论】:

  • @GuessssMe 加里的回答有帮助吗?我没有机会对此进行测试,但我也假设不存在的 text 属性是根本原因。您是否尝试将传出消息的属性设置为空字符串:''
【解决方案2】:

您可以为此使用同义词和 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.'
        }
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2021-12-26
    • 2018-03-11
    • 2020-09-15
    相关资源
    最近更新 更多