2017-02-10 80 views
1

我正在嘗試使用LUIS創建一個Slack機器人,當機器人看到它添加到的頻道中的問候語時,它會向發送問候語的用戶發送直接消息。Node.js:如何使用MS Bot Framework在Slack中發送直接消息?

我看過問題#431並且寫了一個bot。這裏是我的代碼:

var builder = require('botbuilder'); 
var restify = require('restify'); 
// Setup Restify Server 
var server = restify.createServer(); 
server.listen(process.env.port || process.env.PORT || 3978, function() { 
    console.log("%s listening to %s", server.name, server.url); 
}); 
server.get(/.*/, restify.serveStatic({ 
    'directory': '.', 
    'default': 'index.html' 
})); 

// Create Chat Bot 
var connector = new builder.ChatConnector({ 
    appId: process.env.MICROSOFT_APP_ID, 
    appPassword: process.env.MICROSOFT_APP_PASSWORD 
}); 
var bot = new builder.UniversalBot(connector, { 
    persistConversationData: true // need persistent data for dictionary 
}); 
server.post('/api/messages', connector.listen()); 

// Create LUIS recognizer that points at our model and add it as the root '/' dialog 
var model = (omitted); 
var recognizer = new builder.LuisRecognizer(model); 
var dialog = new builder.IntentDialog({ recognizers: [recognizer] }); 
bot.dialog('/', dialog); 

// Add intent handlers 
dialog.matches('Greeting', [ 
    function(session, args, next) { 
     var language = builder.EntityRecognizer.findEntity(args.entities, 'Language'); 
     next({ response: language }); 
    }, 
    function(session, results) { 

     bot.beginDialog({ 
      text: 'Hello', 
      to: {channelId: "emulator", address:"User1", id:"(omitted)", isBot:false}, 
      from: { channelId:"emulator", address:"Bot1", id:"(omitted)", isBot:true} 
     }, '/'); 
    } 
]); 

然而,目前在機器人收到一個問候,它提供了以下錯誤信息:

ERROR: ChatConnector: startConversation - address is invalid. 
Error: Invalid address. 
    at ChatConnector.startConversation (C:\..\node_modules\botbuilder\lib\bots\ChatConnector.js:173:18) 
    at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:308:27 
    at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13) 
    at UniversalBot.ensureConversation (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:302:14) 
    at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:163:19 
    at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:53 
    at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13) 
    at C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:337:23 
    at UniversalBot.tryCatch (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:381:13) 
    at UniversalBot.lookupUser (C:\..\node_modules\botbuilder\lib\bots\UniversalBot.js:324:14) 

(我省略了目錄的一部分)

我看了問題#687但我仍然無法弄清楚問題所在。我怎樣才能讓機器人工作?

我正在使用Botbuilder v3.4.4和Node v4.6.0。

回答

1

走在這裏,我想的方法是:

  1. 保存session.message.address的地方,你將不得不在bot.beginDialog你正在做以後使用它。
  2. 在開始新對話之前,您需要刪除會話對象以創建新對話
  3. 使用該地址開始對話。

它會像

// consider making this an array insted 
var address 

// probably in the function that matches the greeting 
address = session.message.address; 

// in the step where you want to send the direct messsage 
var newConversationAddress = Object.assign({}, address); 
delete newConversationAddress.conversation; 

// begin dialog with address without conversation 
bot.beginDialog(newConversationAddress,... 

看看到CreateNewConversation樣品。你會看到一些非常相似的事情正在完成。