83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
var restify = require('restify');
|
|
var builder = require('botbuilder');
|
|
|
|
//=========================================================
|
|
// Bot Setup
|
|
//=========================================================
|
|
|
|
// 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);
|
|
});
|
|
|
|
// 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);
|
|
server.post('/api/messages', connector.listen());
|
|
|
|
//=========================================================
|
|
// Bots Dialogs
|
|
//=========================================================
|
|
|
|
var addresses = new Set()
|
|
|
|
var intents = new builder.IntentDialog();
|
|
bot.dialog('/', intents);
|
|
|
|
intents.matches(/register/i, [
|
|
function (session) {
|
|
var address = JSON.stringify(session.message.address);
|
|
if (addresses.has(address)){
|
|
session.send("Already registered");
|
|
} else {
|
|
addresses.add(address);
|
|
session.send("Chat registered");
|
|
}
|
|
},
|
|
|
|
]);
|
|
|
|
/*intents.matches(/./, [
|
|
function(session, args, next) {
|
|
console.log("Matched all commands: %j", args);
|
|
session.send("Matched all commands:");
|
|
}
|
|
]);*/
|
|
|
|
intents.onDefault([
|
|
function (session, args, next) {
|
|
session.send("Hi, this command is invalid, maybe try: register command")
|
|
}
|
|
]);
|
|
|
|
server.use(restify.bodyParser());
|
|
server.post('/api/notify', function (req, res) {
|
|
// Process posted notification
|
|
var notification = req.body.notification;
|
|
console.log("sending message %j", notification)
|
|
console.log(req.getContentType())
|
|
for (var address of addresses) {
|
|
var address = JSON.parse(address);
|
|
// Send notification as a proactive message
|
|
var msg = new builder.Message()
|
|
.address(address)
|
|
.text(notification);
|
|
bot.send(msg);
|
|
}
|
|
res.end();
|
|
});
|
|
|
|
/*
|
|
bot.dialog('/register', function (session) {
|
|
var address = JSON.stringify(session.message.address);
|
|
if addresses.has(address){
|
|
session.send("Already registered")
|
|
}
|
|
addresses.add()
|
|
session.send("Hello World");
|
|
});*/
|