-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (109 loc) · 4.44 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const restify = __importStar(require("restify"));
const moment = __importStar(require("moment-timezone"));
const botbuilder_1 = require("botbuilder");
let loop = true;
// Create bot adapter, which defines how the bot sends and receives messages.
let adapter = new botbuilder_1.BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
let name, greeting, time;
let num;
// Create HTTP server.
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log(`\n${server.name} listening to ${server.url}`);
});
// Listen for incoming requests at /api/messages.
server.post('/api/messages', (req, res) => __awaiter(this, void 0, void 0, function* () {
// Use the adapter to process the incoming web request into a TurnContext object.
yield adapter.processActivity(req, res, (turnContext) => __awaiter(this, void 0, void 0, function* () {
if (turnContext.activity.type === botbuilder_1.ActivityTypes.ConversationUpdate &&
turnContext.activity.membersAdded !== undefined &&
turnContext.activity.membersAdded[0].name === 'Bot') {
name = turnContext.activity.from.name;
greeting = greeter();
yield turnContext.sendActivity(`${greeting} ${name}!`);
yield chabaao(turnContext);
}
// Do something with this incoming activity!
if (turnContext.activity.type === botbuilder_1.ActivityTypes.Message) {
// Get the user's text
const utterance = turnContext.activity.text;
switch (utterance) {
case 'thamb':
case 'tham':
loop = false;
break;
case 'kha':
case 'khao':
loop = true;
name = turnContext.activity.from.name;
greeting = greeter();
yield turnContext.sendActivity(`${greeting} ${name}!`);
yield chabaao(turnContext);
break;
default:
yield turnContext.sendActivity(`You said ${utterance}`);
}
}
}));
}));
function chabaao(turnContext) {
return __awaiter(this, void 0, void 0, function* () {
while (loop) {
num = getRandomInt(4);
if (num === 0) {
num++;
}
let d = moment.tz('Asia/Kolkata');
time = d.format('hh:kk');
let dialogs = [
`Did you updated the work log?`,
`What is the status?`,
`${name}, please come to MR${num} for scrum`,
`Hi team, let's meet in MR${num} for a discussion with ajitem`,
`${name}, we can sit at ${time} for the same`
];
let index = getRandomInt(dialogs.length);
yield turnContext.sendActivity(dialogs[index]);
let n = getRandomInt(1000000);
console.log(`Sleep for ${n}`);
yield sleep(getRandomInt(n));
dialogs = [];
}
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function greeter() {
let d = moment.tz('Asia/Kolkata');
let t = parseInt(d.format('HH'), 10);
if (t < 12)
return 'Good Morning';
else if (t < 17)
return 'Good Afternoon';
else
return 'Good Evening';
}