2017-08-25 151 views
2

提交文本BotFramework网上聊天我已经在我的网站通过DirectLine开发与微软博特Framework中的一个聊天机器人,并包括它:通过点击链接

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

默认情况下是隐藏的聊天机器人窗口,它才出现用户点击“与chatbot聊天”链接。

但我也希望通过点击此链接一个对话立即开始chatbot。我试图通过填写聊天输入并将它发送到chatbot单击链接时与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 
} 

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

任何想法我在做什么错在这里?

感谢很多:)

+0

一个好地方,问这可能是这里https://github.com/Microsoft/BotFramework-WebChat – JasonSowers

+0

的可能的复制[机器人框架得到嵌入式聊天控制页面的serviceUrl(https://开头计算器.COM /问题/ 42825048/BOT框架,得到最serviceURL中-的嵌入式聊控制页) –

回答

0

这听起来像你正在寻找一个“欢迎信息” - 这是来自僵尸当他们第一次参加聊天的用户发送的消息。例如:“欢迎来到Shopping Bot!我会帮助你跟踪你的购物清单”或描述你的机器人的一般功能的东西。您可以通过添加以下代码到你的机器人在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

0

通过@Nils W上的答案是真棒。毕竟我最终使用了反向通道,因为我还需要它来完成其他任务。 https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-backchannel有一个适合我的问题很好的例子,点击一个按钮,这个事件通过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); 
})