2017-03-27 144 views
7

我是微軟Bot框架的新成員。現在我正在模擬器上測試我的代碼。我想在您連接後立即發送Hello消息。以下是我的代碼。Microsoft Bot框架:連接發送消息

var restify = require('restify'); 
var builder = require('botbuilder'); 

var server = restify.createServer(); 
server.listen(process.env.port || process.env.PORT || 3978, function() { 
    console.log('%s listening to %s', server.name, server.url); 
}); 

var connector = new builder.ChatConnector({ 
    appId: "-- APP ID --", 
    appPassword: "-- APP PASS --" 
}); 
var bot = new builder.UniversalBot(connector); 
server.post('/api/message/',connector.listen()); 

bot.dialog('/', function (session) { 
    session.send("Hello"); 
    session.beginDialog('/createSubscription'); 
}); 

以上代碼在用戶發起會話時發送Hello消息。我想在用戶連接後立即發送此消息。

回答

13

鉤入conversationUpdate事件並檢查何時添加機器人。之後,您可以發佈消息或開始一個新的對話框(如下面的代碼中我從ContosoFlowers Node.js sample中提取的代碼,儘管有許多others也這樣做)。

// Send welcome when conversation with bot is started, by initiating the root dialog 
bot.on('conversationUpdate', function (message) { 
    if (message.membersAdded) { 
     message.membersAdded.forEach(function (identity) { 
      if (identity.id === message.address.bot.id) { 
       bot.beginDialog(message.address, '/'); 
      } 
     }); 
    } 
}); 
+0

謝謝...正是我在找什麼。 –

+0

還有一件事我可以得到連接到機器人的所有用戶的列表? –

+0

嗯,我不知道有什麼要做的。您應該手動執行此操作,存儲與機器人開始對話的用戶。 –