-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (85 loc) · 3.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const restify = require('restify');
const builder = require('botbuilder');
const menu = require('./menu');
String.prototype.format = String.prototype.format || function () {
a = this;
for (let k in arguments) {
a = a.replace("{" + k + "}", arguments[k]);
}
return a;
}
// Setup Restify Server
const server = restify.createServer();
server.listen(process.env.port || 8080, function () {
console.log('%s listening to %s', server.name, server.url);
});
// TODO: Remover chaves
// Create chat connector for communicating with the Bot Framework Service
const connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId || "347604bd-62b6-40fa-ac1d-7d8a1028d0ee",
appPassword: process.env.MicrosoftAppPassword || "hgCB035~[cfdipKULMM96}}"
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Receive messages from the user and responds
const bot = new builder.UniversalBot(connector, function (session) {
if (session.message.text.toLowerCase().includes('hello')) {
const sender = getSender(session.message);
session.send(`Hey! Whenever you need I can 'help' you...`);
} else if (session.message.text.toLowerCase().includes('help')) {
showOptions(session);
} else if (session.message.text.toLowerCase().includes('menu')) {
menu.getTodayMenu().then(ementa => session.send(ementa));
} else {
session.send(`Sorry I didn't understand you...
Try help for all the available options`);
}
});
bot.on('conversationUpdate', function (message) {
if (message.membersAdded && message.membersAdded.length > 0) {
// Say hello when joining conversation
if (message.membersAdded.find(member => member.id === message.address.bot.id)) {
// const isGroup
const reply = new builder.Message()
.address(message.address)
.text('Hello');
bot.send(reply);
}
} else if (message.membersRemoved) {
// See if bot was removed
var botId = message.address.bot.id;
for (var i = 0; i < message.membersRemoved.length; i++) {
if (message.membersRemoved[i].id === botId) {
// Say goodbye
var reply = new builder.Message()
.address(message.address)
.text("Goodbye");
bot.send(reply);
break;
}
}
}
});
//Bot on
bot.on('contactRelationUpdate', function (message) {
if (message.action === 'add') {
const sender = getSender(message);
var reply = new builder.Message()
.address(message.address)
.text(`Hello ${sender}... Thanks for adding me.`);
bot.send(reply);
} else {
// delete their data
}
});
function showOptions(session) {
session.send(`Available options:
help: Currently available options
menu: Current day menu`);
// menu : Show today menu
// weekMenu: Show current week menu
}
function getSender(message) {
const name = message.user ? message.user.name : null;
return name || 'there';
}