3

我使用 Microsoft Bot Framework 开发了一个聊天机器人,并通过 DirectLine 将其包含在我的网站中:

<div id="chatbot_body"></div>
    <script src="https://unpkg.com/botframework-webchat/botchat.js"></script>
    <script>
      BotChat.App({
        directLine: { secret: 'here-is-my-key' },
        user: { id: 'Sie' },
        bot: { id: 'botid' },
        resize: 'detect'
      }, document.getElementById("chatbot_body"));
    </script>

默认情况下,聊天机器人窗口是隐藏的,只有在用户单击“与聊天机器人聊天”链接时才会出现。

但我也希望通过单击此链接,聊天机器人立即开始对话。我正在尝试通过填写聊天输入并在单击链接时将其发送到聊天机器人来使用 Jquery 执行此操作。

$("#chatbot_link").on("click", function(){
    $("#chatbot_body").show(); // show chatbot window
    $("input.wc-shellinput").val("start"); // fill input field with 'start'
    $(".wc-console").addClass("has-text"); // add has-text class (necessary?)
    $(".wc-send").click(); // submit form by clicking wc-send
}

但这不起作用。输入不会发送到聊天机器人,因此聊天机器人不会说任何话。

有什么想法我在这里做错了吗?

非常感谢 :)

4

2 回答 2

1

@Nils W 的回答很棒。毕竟我最终使用了反向通道,因为我也需要它来完成其他任务。 https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-backchannel有一个非常适合我的问题的示例,单击按钮并将此事件发送到机器人通过反向渠道。

bot.on("event", function (event) {
    var msg = new builder.Message().address(event.address);
    msg.data.textLocale = "en-us";
    if (event.name === "buttonClicked") {
        msg.data.text = "I see that you clicked a button.";
    }
    bot.send(msg);
})
于 2017-08-28T14:04:11.383 回答
1

听起来您正在寻找“欢迎消息” - 这是机器人首次加入聊天时发送给用户的消息。示例:“欢迎使用购物机器人!我将帮助您跟踪您的购物清单”或描述您的机器人一般功能的内容。您可以在 Node.js 中通过将以下代码添加到您的机器人来执行此操作:

// Welcome message for Node.js bot
bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id == message.address.bot.id) {
                // Bot is joining conversation
                // - For WebChat channel you'll get this on page load.
                var reply = new builder.Message()
                        .address(message.address)
                        .text("Welcome to my page");
                bot.send(reply);
            } else {
                // User is joining conversation
                // - For WebChat channel this will be sent when user sends first message.
                // - When a user joins a conversation the address.user field is often for
                //   essentially a system account so to ensure we're targeting the right 
                //   user we can tweek the address object to reference the joining user.
                // - If we wanted to send a private message to teh joining user we could
                //   delete the address.conversation field from the cloned address.
                var address = Object.create(message.address);
                address.user = identity;
                var reply = new builder.Message()
                        .address(address)
                        .text("Hello %s", identity.name);
                bot.send(reply);
            }
        });
    }
});

来源:https ://gist.github.com/nwhitmont/d9910fcf7ab4048ee37bd5c789cfc375

于 2017-08-25T23:04:08.837 回答