forked from BeepBoopHQ/starter-node-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (132 loc) · 4.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// TEDxBOT
// version 0.1
// by dligthart <dligthart@gmail.com>
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var Botkit = require('botkit');
var stormpath = require('stormpath');
var slackToken = process.env.SLACK_TOKEN;
var token = slackToken;
var controller = Botkit.slackbot({ debug: false });
var apiKey = {
id: process.env.STORMPATH_ID,
secret: process.env.STORMPATH_SECRET
};
var appId = process.env.STORMPATH_APPID;
if(!apiKey.id || !apiKey.secret) {
apiKey.id = 'NF0P0OR4JZ0BYI1MMHFCH9Z4X';
apiKey.secret = 'jjpXpjDjBF8o21VzltHygmYhAkbEozbk7NPsyAPogfQ';
appId = '2dOFY5UnOtoCGznZKlA5ax';
//console.warn('Stormpath API key and secret are required');
// process.exit();
}
var client = new stormpath.Client({ apiKey: apiKey });
var application = null;
client.getApplication('https://api.stormpath.com/v1/applications/' + appId, function(err, resource) {
if(err) console.log('Could not retrieve stormpath application', appId);
application = resource;
});
controller.spawn({ token: token }).startRTM(function (err, bot, payload) {
if (err) throw new Error('Error connecting to Slack: ', err);
console.log('Connected to Slack');
});
controller.hears(['hi'], ['direct_message', 'direct_mention'], function (bot, message) {
startRegistrationConversation(bot, message);
});
function startRegistrationConversation(bot, message) {
var account = {
givenName: '',
surname: '',
username: '',
email: '',
password: ''
};
function configAccount() {
return account;
}
bot.startConversation(message, function(err, convo) {
convo.say('Hello! Human!');
convo.ask('Would you like to register? ', function(response, convo) {
if('yes' == response.text) {
convo.next();
inputName(response, convo, configAccount);
}
});
});
}
function createAccount(account, convo) {
application.createAccount(account, function(err, createdAccount) {
if(err) {
// TODO: add the correct error handling.
/**
] name: 'ResourceError',
[ 2016-06-17T13:40:13Z ] status: 409,
[ 2016-06-17T13:40:13Z ] code: 2001,
[ 2016-06-17T13:40:13Z ] userMessage: 'Account with that email already exists. Please choose another email.',
[ 2016-06-17T13:40:13Z ]
**/
console.log(err);
convo.say('Something went wrong during registration..');
convo.next();
} else {
console.log(createdAccount);
convo.say('Ok then - you are now registered!');
convo.next();
convo.say('One more thing; you can use this password to log in: ' + account.password);
convo.next();
convo.say('Master, I bid you farewell. Thank you for activating my circuits. ');
convo.next();
convo.say('And please check your email - I have sent you a message..bye bye');
}
});
}
function inputName(response, convo, account) {
convo.ask('Pleased to get acquainted, meatbag - now what is your designation?', function(response, convo) {
account().givenName = response.text;
convo.say('I am here to serve you, Master ' + account().givenName +' !');
convo.next();
convo.ask('Master '+ account().givenName + ', if you don\'t mind me asking; what is your last name? ', function(response, convo) {
account().surname = response.text;
convo.say(account().givenName + ' ' + account().surname + ', Master, what a beautiful name, splendid! I have stored your full name in my memory banks..');
convo.next();
inputEmail(response, convo, account);
});
});
}
function inputEmail(response, convo, account) {
convo.ask('Now please enter your email address so I can send you lots of spam - wink wink ;)', function(response, convo) {
account().email = extractEmail(response.text);
convo.say('Thanks you entered: ' + account().email);
convo.next();
convo.ask(account().givenName + ', did you enter the correct email address?', function(response, convo) {
if('yes' == response.text) {
account().username = account().email;
account().password = makePassword(13, 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890');
createAccount(account(), convo);
} else {
convo.say('Ok let\'s go through it again.. ');
convo.next();
inputEmail(response, convo, account);
}
});
});
}
function makePassword(n, a) {
var index = (Math.random() * (a.length - 1)).toFixed(0);
return n > 0 ? a[index] + makePassword(n - 1, a) : '';
}
function extractEmail(text){
var r = text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if(r) {
return r[0];
}
return null;
}
server.listen(80);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});