I need my bot to send messaged to everybody, who connected to it, with triggeting by http request. Here are 2 issues: how to send proactive message and how to manage it with http. This is what I have so far:
var restify = require('restify');
var builder = require('botbuilder');
var server = restify.createServer();
var globalreply;
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) {
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();
}
server.get('/api/messages/:name', respond);
This should catch 1 message of a user, remember it and reply whenever I go to localhost:3978/api/messages/any
But it always has error "TypeError: Cannot read property 'conversation' of undefined"
I tried to use also
bot.add('/notify', function (session) {
session.endDialog("I'm sending you a proactive message!");
});
bot.beginDialog({
to: { address: "User1", channelId: "emulator" , id: "2c1c7fa3"},
from: { address: "stuffbot", channelId: "emulator", id: "stuffbot" }
}, '/notify');
But it didn't help too. Help is needed, is there any way to manage bot sending with http to all connected users?
Best regards
It seems that conversation object in globalreply is removed after bot.send. Clone globalreply instead of referencing it. I used JSON.stringify/parse for cloning the object.
It works for me though I'm looking for a better way. Please, refer following code snippet.
bot.dialog('/',function (session) {
globalreply = JSON.stringify( session.message; ) // address: reply.address
var reply = JSON.parse(globalreply);
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.beginDialog;
var reply = JSON.parse(globalreply);
reply.text = 'hello ' + req.params.name
bot.send(reply);
next();
}
Most helpful comment
It seems that
conversationobject inglobalreplyis removed afterbot.send. Cloneglobalreplyinstead of referencing it. I usedJSON.stringify/parsefor cloning the object.It works for me though I'm looking for a better way. Please, refer following code snippet.