2017-08-25 150 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); 
})