-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
214 lines (184 loc) · 5.87 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
var TelegramBot = require('node-telegram-bot-api')
var core = require('./core')
var util = require('./util')
var game = require('./game')
var AsyncLock = require('async-lock');
var lock = new AsyncLock();
port = process.env.PORT || 443
host = '0.0.0.0' // probably this change is not required
externalUrl = process.env.URL || 'https://bluebird-minebot.herokuapp.com'
token = process.env.TOKEN
bot = new TelegramBot(process.env.TOKEN, {webHook: {port: port, host: host}});
bot.setWebHook(externalUrl + ':443'+'/bot' + token);
/*
// replace the value below with the Telegram token you receive from @BotFather
const token = '';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true, request: {proxy:"http://127.0.0.1:8000"}});
*/
bot.onText(/^\/start/,(msg, match) => {
bot.sendMessage(
msg.chat.id,
'我还活着',
{
reply_to_message_id: msg.message_id,
}
)
})
bot.onText(/^\/mine(@\w+)?(?: (\d+) (\d+) (\d+))?$/, (msg, match) => {
bot.sendMessage(
msg.chat.id,
'扫雷开始',
{
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [[{
text: '...',
callback_data: '-',
}]],
},
}
).then((sentmsg) => {
row = parseInt(match[2], 10) || 8
col = parseInt(match[3], 10) || 8
count = parseInt(match[4], 10) || 12
let errorMsg = util.validate(row, col, count)
if (errorMsg !== null) {
bot.editMessageText(
errorMsg,
{
chat_id: sentmsg.chat.id,
message_id: sentmsg.message_id,
reply_to_message_id: msg.message_id,
}
);
return
}
mineCore = game.gameInit(sentmsg.chat.id + '_' + sentmsg.message_id,row,col,count)
bot.editMessageReplyMarkup(
{
inline_keyboard: util.coreToKeyboard(mineCore,null),
},
{
chat_id: sentmsg.chat.id,
message_id: sentmsg.message_id,
}
)
})
}, 1);
bot.on('callback_query', (query) => {
const msg = query.message;
gameId = msg.chat.id + '_' + msg.message_id
try {
const info = JSON.parse(query.data);
} catch (e) {
throw Error("json error");
}
const info = JSON.parse(query.data);
if (typeof info[0] !== 'number' || typeof info[1] !== 'number') {
throw Error(JSON.stringify(query));
}
lock.acquire(gameId, function (done) {
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
if (!game.getGame(gameId)) {
done(new Error("not can found game"));
return
}
try {
var result = game.gameClick(msg.chat.id + '_' + msg.message_id, info[0], info[1])
} catch (err) {
bot.sendMessage(
msg.chat.id,
err.msg,
{
reply_to_message_id: msg.message_id,
}
);
done(err);
return
}
// 游戏已经结束或者点击已经打开/标记的格子
if (result === null) {
/*
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});*/
done("no error", "ok");
return
}
let win = game.isGameWin(msg.chat.id + '_' + msg.message_id)
if (win === 1) {
username = query.from.username || query.from.first_name;
bot.editMessageReplyMarkup(
{
inline_keyboard: util.coreToKeyboard(result, true),
},
{
chat_id: msg.chat.id,
message_id: msg.message_id,
}
)
bot.sendMessage(
msg.chat.id,
"@" + username + " 你已经打开了所有地雷",
{
reply_to_message_id: msg.message_id,
}
);
} else if (win === -1) {
username = query.from.username || query.from.first_name;
bot.editMessageReplyMarkup(
{
inline_keyboard: util.coreToKeyboard(result,true),
},
{
chat_id: msg.chat.id,
message_id: msg.message_id,
}
)
bot.sendMessage(
msg.chat.id,
"@" + username + " 你炸了",
{
reply_to_message_id: msg.message_id,
}
);
} else if (win === 2) {
username = query.from.username || query.from.first_name;
bot.editMessageReplyMarkup(
{
inline_keyboard: util.coreToKeyboard(result, true),
},
{
chat_id: msg.chat.id,
message_id: msg.message_id,
}
)
bot.sendMessage(
msg.chat.id,
"@" + username + " 你已经找到了所有地雷",
{
reply_to_message_id: msg.message_id,
}
);
} else {
bot.editMessageReplyMarkup(
{
inline_keyboard: util.coreToKeyboard(result, null),
},
{
chat_id: msg.chat.id,
message_id: msg.message_id,
}
)
}
done("no error", "ok");
}, function (err, ret) {
//console.log("error")
console.log(err)
// lock released
}, null);
})
bot.on("polling_error", console.log);