-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.js
269 lines (255 loc) · 10 KB
/
Bot.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Bot.js
* Demo chat bot.
*/
// bot settings
const
plugin = require('./plugin_manager.js'),
base = require('./base.js'),
server = base.server,
bot = new base(8080);
// game
let gacha_time = undefined;
let gacha_result = undefined;
// reply
let reply = undefined;
// one time conversation label and the thing what to do
let con_label = 'one';
let con_action = '';
// skip the received_message
let skip = 0;
// group and nickname_lists
let group_list = '';
let nickname_list = '';
let join_group = '';
let invite_nickname = '';
let friend_nickname = '';
server.post('/', async function(req, res, next){
bot.lineVerify(req, res);
//get the receive information
let platform = bot.getPlatform(req);
let received_message = bot.getReceivedMessage(req);
let user_id = bot.getUserId(req);
let payload = bot.getPayload(req);
//console.log(skip);
/*
if (skip >= 1){
skip = 0;
}
*/
bot.connect(platform, req, res);
if(con_label === 'one'){
try{
console.log(payload);
// button mode
if(typeof payload !== 'undefined'){
if(payload.match(/^抽卡/)){
//gacha_time = received_message.match(/^!抽\*[1-9]+[0-9]*/)[0].substring(3);
gacha_result = plugin.run('plugin_game', parseInt(10, 10));
reply = await bot.messageHandler(platform ,gacha_result);
bot.sendAPI(platform, 'reply', req, reply);
gacha_result = undefined;
} else if(payload.match(/^傳送跨平台訊息/)){
group_list = await plugin.run('plugin_sql', 'getGroupName', user_id);
if(group_list.length === 0){
reply = await bot.messageHandler(platform , '你沒有所屬的群組');
bot.sendAPI(platform, 'reply', req, reply);
} else {
reply = await bot.messageHandler(platform ,group_list + '\n請選擇要傳送的群組');
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'two';
con_action = '選擇群組';
}
} else if(payload.match(/^建立群組/)){
reply = await bot.messageHandler(platform, '請輸入群組名稱');
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'two';
con_action = payload;
} else if(payload.match(/^群組邀請/)){
group_list = await plugin.run('plugin_sql', 'getGroupName', user_id);
reply = await bot.messageHandler(platform, '你想邀請誰進入哪個群組?\n' + group_list + '\n格式:<群組名稱>,<暱稱>');
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'two';
con_action = payload;
// add friend
} else if(payload.match(/^好友邀請/)){
reply = await bot.messageHandler(platform, '你想要邀請誰成為好友呢?');
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'two';
con_action = payload;
// add nickname
} else if(payload.match(/^好/)){
reply = await bot.messageHandler(platform, '請輸入您要取的綽號');
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'two';
con_action = payload;
} else if(payload.match(/^不要/)){
reply = await bot.messageHandler(platform, '好ㄛ');
bot.sendAPI(platform, 'reply', req, reply);
// invite group
} else if(payload.match(/^接受/)){
await plugin.run('plugin_sql', 'joinGroup', join_group, invite_nickname);
reply = await bot.messageHandler(platform, '已加入群組');
bot.sendAPI(platform, 'reply', req, reply);
} else if(payload.match(/^拒絕/)){
reply = await bot.messageHandler(platform, '已拒絕加入群組');
bot.sendAPI(platform, 'reply', req, reply);
} else if(payload.match(/^接受邀請/)){
await plugin.run('plugin_sql', 'AddFriend', friend_nickname, user_nickname);
reply = await bot.messageHandler(platform, '您已經與' + user_nickname + "成為好友");
bot.sendAPI(platform, 'reply', req, reply);
platform = await plugin.run('plugin_sql', 'getPlatform', user_nickname);
let invite_user_id = await plugin.run('plugin_sql', 'getUserId', user_nickname);
let push = await bot.messageHandler(platform , friend_nickname + '已接受您的好友邀請');
bot.sendAPI(platform, 'push', invite_user_id, push);
} else if(payload.match(/^拒絕邀請/)){
reply = await bot.messageHandler(platform, '已拒絕成為好友');
bot.sendAPI(platform, 'reply', req, reply);
} else {
reply = bot.messageHandler(platform ,'unknown command');
bot.sendAPI(platform, 'reply', req, reply);
}
if(platform === 'line'){
skip++;
}
// text mode
} else if(received_message === '功能表'){
if(await plugin.run('plugin_sql', 'getUser_nickname', user_id) === null){
reply = bot.messageHandler(platform, received_message, '第一次使用');
bot.sendAPI(platform, 'reply', req, reply);
} else {
reply = bot.messageHandler(platform, received_message, '功能表');
bot.sendAPI(platform, 'reply', req, reply);
}
// nlp
} else if(typeof payload === 'undefined' && skip < 1){
reply = await plugin.run('plugin_reply', received_message);
reply = bot.messageHandler(platform, reply);
bot.sendAPI(platform, 'reply', req, reply);
}
if(typeof payload === 'undefined' && skip === 1){
skip = 0;
}
} catch(err){
console.log(err);
}
// talk two times
} else if(con_label === 'two'){
// create nickname
if(con_action === '好' && skip < 1){
let nickname = received_message;
let message = await plugin.run('plugin_sql', 'createNickname', platform, user_id, nickname);
reply = await bot.messageHandler(platform , message);
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'one';
con_action = '';
// create group
} else if(con_action === '建立群組' && skip < 1){
let group_name = received_message;
let message = await plugin.run('plugin_sql', 'createGroup', user_id, group_name);
reply = await bot.messageHandler(platform , message);
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'one';
con_action = '';
// select the group to send message
} else if(con_action === '選擇群組' && skip < 1){
let group_name = received_message;
nickname_list = await plugin.run('plugin_sql', 'getGroupMember', user_id, group_name);
reply = await bot.messageHandler(platform , nickname_list);
bot.sendAPI(platform, 'reply', req, reply);
if(nickname_list.length === 6){
con_label = 'one';
con_action = '';
} else {
con_label = 'three';
con_action = '傳送跨平台訊息';
}
// invite someone
} else if(con_action === '群組邀請' && skip < 1){
join_group = received_message.substring(0, received_message.indexOf(','));
invite_nickname = received_message.substring(received_message.indexOf(',')+1);
let reply_message;
//console.log(invite_nickname);
if(await plugin.run('plugin_sql', 'UserIsExist', invite_nickname)){
reply_message = "您已邀請" + invite_nickname + "進入" + join_group + "群組";
}
else{
reply_message = "查無此人或群組";
}
reply = await bot.messageHandler(platform ,reply_message);
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'one';
con_action = '';
if(reply_message.length >= 8){
let user_nickname = await plugin.run('plugin_sql', 'getUser_nickname', user_id);
let invite_user_id = await plugin.run('plugin_sql', 'getUserId', invite_nickname);
let platform = await plugin.run('plugin_sql', 'getPlatform', invite_nickname);
//console.log(platform);
let push = await bot.messageHandler(platform , user_nickname + '邀請你進入' + join_group);
bot.sendAPI(platform, 'push', invite_user_id, push);
push = await bot.messageHandler(platform , '功能表', '邀請進入群組');
bot.sendAPI(platform, 'push', invite_user_id, push);
con_label = 'one';
con_action = '';
}
} else if(con_action === '好友邀請' && skip < 1){
friend_nickname = received_message;
let reply_message;
let isExist = await plugin.run('plugin_sql', 'FriendIsExist', user_id, friend_nickname);
if(isExist){
reply_message = "您與" + friend_nickname + "已經是好友了";
}
else{
reply_message = "已向" + friend_nickname + "發出好友邀請";
}
reply = await bot.messageHandler(platform ,reply_message);
bot.sendAPI(platform, 'reply', req, reply);
con_label = 'one';
con_action = '';
if(!isExist){
let user_nickname = await plugin.run('plugin_sql', 'getUser_nickname', user_id);
let invite_user_id = await plugin.run('plugin_sql', 'getUserId', friend_nickname);
let platform = await plugin.run('plugin_sql', 'getPlatform', friend_nickname);
//console.log(platform);
let push = await bot.messageHandler(platform , user_nickname + ' 發送了好友邀請給您');
bot.sendAPI(platform, 'push', invite_user_id, push);
push = await bot.messageHandler(platform , '功能表', '是否接受好友邀請?');
bot.sendAPI(platform, 'push', invite_user_id, push);
con_label = 'one';
con_action = '';
}
}
if(typeof payload === 'undefined' && skip === 1){
skip = 0;
}
} else if(con_label === 'three'){
if(con_action === '傳送跨平台訊息' && skip < 1){
let user_nickname = await plugin.run('plugin_sql', 'getUser_nickname', user_id);
let nickname_array = nickname_list.split(' ');
let nickname = received_message.substring(0, received_message.indexOf('#'));
let message = user_nickname + '傳了訊息給您 : ' + received_message.substring(received_message.indexOf('#')+1);
platform = await plugin.run('plugin_sql', 'getPlatform', nickname);
for(let item of nickname_array){
if(nickname === item){
let receiver_id = await plugin.run('plugin_sql', 'getUserId', nickname);
let push = await bot.messageHandler(platform , message);
bot.sendAPI(platform, 'push', receiver_id, push);
con_label = 'one';
con_action = '';
}
}
if(con_label === 'three'){
let push = await bot.messageHandler(platform , '此人非群組成員');
bot.sendAPI(platform, 'push', user_id, push);
con_label = 'one';
con_action = '';
}
}
if(typeof payload === 'undefined' && skip === 1){
skip = 0;
}
}
});
server.get('/', function(req, res, next){
bot.fbSubscribe(req, res);
});