2016-09-13 41 views
1

我想用http請求觸發我的機器人(例如剛剛輸入http://localhost:3978/api/messages/http),所以在觸發它之後,它會發送每個連接到此機器人的用戶一些消息。 我已經看到了這個話題:How to send message later in bot framework? 這是我到目前爲止有:微軟botframework(node.js):如何從HTTP請求觸發機器人

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: process.env.MICROSOFT_APP_ID, 
appPassword: process.env.MICROSOFT_APP_PASSWORD 
}); 

server.post('/api/messages', connector.listen()); 
var bot = new builder.UniversalBot(connector); 

bot.dialog('/',function (session) { 
var reply = session.message; // address: reply.address 
reply.text = 'Wake up!' 
console.log(reply.text); 
bot.send(reply); 
}); 

// Create response function 
function respond(req, res, next) { 
res.send('hello ' + req.params.name); 
bot.send(reply); 
next(); 
} 
server.get('/api/messages/:name', respond); 

不幸的是,當我acessing我http://localhost:3978/api/messages/http它不發送任何消息。我還試圖用

connector.send('message'); 

但它總是得來我「ERROR:ChatConnector:發送 - 郵件丟失地址或的serviceUrl」

UPDATE: 我已經宣佈了答覆全局變量與

var globalreply; 
bot.dialog('/',function (session) { 
globalreply = session.message; // address: reply.address 
globalreply.text = 'Wake up!' 
console.log(globalreply.text); 
bot.send(globalreply); 
}); 

// Create response function 
function respond(req, res, next) { 
res.send('hello ' + req.params.name); 
bot.beginDialog; 
bot.send(globalreply); 
next(); 
} 

但現在得來我一個錯誤: 類型錯誤:無法讀取的不確定屬性「對話」。 在我的bot.send(globalreply);一行。 期待您的幫助。

此致敬禮。

回答

2

如果你想建立一個正常的HTTP API路由,我建議使用Restify API風格路由,而不是機器人的/api/messages路由處理程序。

例如:

function apiResponseHandler(req, res, next) { 
    // trigger botbuilder actions/dialogs here 
    next(); 
} 

server.get('/hello/:name', apiResponseHandler);