forked from Koredotcom/BotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessTheNumber.js
59 lines (51 loc) · 1.92 KB
/
GuessTheNumber.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
var botId = "st-guess-the-number-bot";
var botName = "Guess the number";
var sdk = require("./lib/sdk");
function generateRandomNumber() {
return Math.ceil((Math.random()*100));
}
module.exports = {
botId : botId,
botName : botName,
on_user_message : function(requestId, data, callback) {
sdk.sendBotMessage(data, callback);
},
on_bot_message : function(requestId, data, callback) {
var message;
if (data.context.guess) {
if (data.context.guess.isMore) {
message = "Lesser than that";
} else if (data.context.guess.isLess) {
message = "More than that";
} else if (data.context.guess.isCorrect) {
message = "That is correct! You just took " + data.context.guessCount + " guesses!" ;
}
} else if (data.context.theNumber) {
message = "I have a number between 1 & 100. Try and guess it!";
}
if (message) {
data.message = message;
}
sdk.sendUserMessage(data, callback);
},
on_webhook : function(requestId, data, componentName, callback) {
var context = data.context;
var entities = context.entities || {};
if (componentName === 'GenerateNewNumber') {
context.theNumber = generateRandomNumber();
context.guessCount = 0;
} else if (componentName === 'GuessingTheNumber') {
var guessedNumber = Number(entities.guessedNumber);
context.guess = {};
if (context.theNumber > guessedNumber) {
context.guess.isLess = "true";
} else if (context.theNumber < guessedNumber) {
context.guess.isMore = "true";
} else if (context.theNumber === guessedNumber) {
context.guess.isCorrect = "true";
}
context.guessCount++;
}
callback(null, data);
}
};