单击链接将文本提交给BotFramework WebChat

时间:2017-08-25 11:47:02

标签: javascript jquery botframework direct-line-botframework

我与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>

默认情况下,聊天机器人窗口是隐藏的,只有当用户点击&#34;与聊天机器人聊天时才会显示。链接。

但我也希望点击此链接聊天机会立即启动对话。我想通过填写聊天输入并在点击链接时将其发送到聊天机器人来与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
}

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

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

非常感谢:)

2 个答案:

答案 0 :(得分:0)

这听起来像是在寻找“欢迎信息” - 这是在他们第一次加入聊天时从机器人发送给用户的消息。示例:“欢迎来到购物机器人!我会帮助您跟踪您的购物清单”或描述机器人一般功能的内容。您可以通过向机器人添加以下代码在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

答案 1 :(得分:0)

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