Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

Commit 03efed1

Browse files
committed
Added basic (and unstable) support for the new Telegram gaming platform
1 parent 54f603c commit 03efed1

File tree

10 files changed

+243
-9
lines changed

10 files changed

+243
-9
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var Bot = module.exports.Bot = require('./lib/Bot.js'),
1111
Keyboard = module.exports.Keyboard = require('./lib/Keyboard.js'),
1212
ChatMember = module.exports.ChatMember = require('./lib/ChatMember.js'),
1313
UserProfilePhotos = module.exports.UserProfilePhotos = require ('./lib/UserProfilePhotos.js'),
14+
Animation = module.exports.Animation = require('./lib/Animation.js'),
15+
GameHighScore = module.exports.GameHighScore = require('./lib/GameHighScore.js'),
1416
InlineQueryResultArticle = module.exports.InlineQueryResultArticle = require('./lib/InlineQueryResultArticle.js'),
1517
InlineQueryResultLocation = module.exports.InlineQueryResultLocation = require('./lib/InlineQueryResultLocation.js'),
1618
InlineQueryResultPhoto = module.exports.InlineQueryResultPhoto = require('./lib/InlineQueryResultPhoto.js'),
@@ -21,4 +23,5 @@ var Bot = module.exports.Bot = require('./lib/Bot.js'),
2123
InlineQueryResultVoice = module.exports.InlineQueryResultVoice = require('./lib/InlineQueryResultVoice.js'),
2224
InlineQueryResultDocument = module.exports.InlineQueryResultDocument = require('./lib/InlineQueryResultDocument.js'),
2325
InlineQueryResultVenue = module.exports.InlineQueryResultVenue = require('./lib/InlineQueryResultVenue.js'),
24-
InlineQueryResultContact = module.exports.InlineQueryResultContact = require('./lib/InlineQueryResultContact.js');
26+
InlineQueryResultContact = module.exports.InlineQueryResultContact = require('./lib/InlineQueryResultContact.js'),
27+
InlineQueryResultGame = module.exports.InlineQueryResultGame = require('./lib/InlineQueryResultGame.js');

lib/Animation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const extend = require('util')._extend;
3+
4+
function Animation(object, bot) {
5+
this.bot = bot;
6+
extend(this, object);
7+
8+
if (this.photo) {
9+
let sizes = [];
10+
this.photo.forEach((size) => {
11+
sizes.push(new PhotoSize(size, this.bot))
12+
});
13+
this.photo = sizes;
14+
}
15+
}
16+
17+
module.exports = Animation;

lib/Bot.js

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const request = require('superagent-promise')(require('superagent'), Promise),
1010
CallbackQuery = require('./CallbackQuery.js'),
1111
ChatMember = require('./ChatMember.js'),
1212
UserProfilePhotos = require('./UserProfilePhotos.js'),
13+
GameHighScore = require('./GameHighScore.js'),
1314
fs = require('fs'),
1415
events = require('events'),
1516
endpoint = 'https://api.telegram.org';
@@ -28,6 +29,7 @@ function Bot(token, options) {
2829
this.saveChats = true;
2930
this.enableHelp = true;
3031
this.forms = {}; // user_id: {form, answers}
32+
this.debug = process.env.NODEOGRAM_DEBUG || false;
3133
this.webhookRoute = '/';
3234
this.webhookPort = 8080;
3335
this.autoFetch = true;
@@ -86,14 +88,15 @@ function Bot(token, options) {
8688
};
8789

8890
this.handleUpdates = (updates) => {
89-
console.log(JSON.stringify(updates));
91+
if (this.debug) console.log(JSON.stringify(updates));
9092
if (updates.length > 0) {
9193
this.lastUpdate = updates[updates.length - 1].update_id + 1;
9294
var now = Date.now() / 1000;
9395
updates.forEach((result) => {
9496
if (result.message) {
97+
if (this.debug) console.log(`New message #${result.message.message_id}`);
9598
if (result.message.date < now - 2000) {
96-
console.log(`Skipping backlog update ${result.update_id}`);
99+
if (this.debug) console.log(`Skipping backlog update #${result.update_id}`);
97100
return;
98101
}
99102
var message = new Message(result.message, this);
@@ -102,6 +105,8 @@ function Bot(token, options) {
102105
if (handleNext) {
103106
var res = handler(message);
104107
handleNext = res === undefined ? true : res;
108+
} else {
109+
if (this.debug) console.log(`Not executing any other message handler for message #${message.message_id}`);
105110
}
106111
});
107112
}
@@ -222,6 +227,17 @@ function Bot(token, options) {
222227
});
223228
};
224229

230+
this.sendGame = (chat_id, game_short_name, options) => {
231+
if (chat_id instanceof Chat) chat_id = chat_id.id;
232+
if (chat_id instanceof User) chat_id = chat_id.id;
233+
options = options || {};
234+
options.chat_id = chat_id;
235+
options.game_short_name = game_short_name;
236+
return this.call('sendGame', options).then((res) => {
237+
return new Message(res.body.result, this);
238+
});
239+
}
240+
225241

226242
this.forwardMessage = (chat_id, from_chat_id, message_id, options) => {
227243
if (chat_id instanceof Chat) chat_id = chat_id.id;
@@ -235,8 +251,18 @@ function Bot(token, options) {
235251
});
236252
};
237253

238-
this.answerCallbackQuery = (id, text, alert) => {
239-
return this.call('answerCallbackQuery', {callback_query_id: id, text: text, alert: alert}).then((res) => {
254+
this.answerCallbackQuery = (id, options, alert) => {
255+
// TODO Drop support for the old method signature
256+
if (typeof options === 'string') {
257+
options = {
258+
text: options,
259+
alert: alert
260+
}
261+
}
262+
options = options || {};
263+
options.callback_query_id = id;
264+
265+
return this.call('answerCallbackQuery', options).then((res) => {
240266
return res.body.result;
241267
});
242268
};
@@ -372,12 +398,71 @@ function Bot(token, options) {
372398
} else {
373399
options.chat_id = chat_id;
374400
options.message_id = id;
375-
return this.call('editMessageReplyMarkupn', options).then((res) => {
401+
return this.call('editMessageReplyMarkup', options).then((res) => {
376402
return new Message(res.body.result)
377403
})
378404
}
379405
};
380406

407+
this.setGameScore = (id, user_id, score, inline, options, chat_id) => {
408+
if (chat_id instanceof Chat) chat_id = chat_id.id;
409+
if (chat_id instanceof User) chat_id = chat_id.id;
410+
if (user_id instanceof Chat) user_id = user_id.id;
411+
if (user_id instanceof User) user_id = user_id.id;
412+
413+
options = options || {};
414+
options.user_id = user_id;
415+
options.score = score;
416+
417+
if (inline) {
418+
options.inline_message_id = id;
419+
return this.call('setGameScore', options).then((res) => {
420+
return res.body.result;
421+
})
422+
} else {
423+
options.chat_id = chat_id;
424+
options.message_id = id;
425+
return this.call('setGameScore', options).then((res) => {
426+
return new Message(res.body.result)
427+
})
428+
}
429+
};
430+
431+
this.getGameHighScores = (id, user_id, inline, chat_id) => {
432+
console.log(id);
433+
console.log(user_id);
434+
console.log(inline);
435+
console.log(chat_id)
436+
if (chat_id instanceof Chat) chat_id = chat_id.id;
437+
if (chat_id instanceof User) chat_id = chat_id.id;
438+
if (user_id instanceof Chat) user_id = user_id.id;
439+
if (user_id instanceof User) user_id = user_id.id;
440+
441+
options = {};
442+
options.user_id = user_id;
443+
444+
if (inline) {
445+
options.inline_message_id = id;
446+
return this.call('getGameHighScores', options).then((res) => {
447+
var scores = [];
448+
res.body.result.forEach(score => {
449+
scores.push(new GameHighScore(score, this))
450+
});
451+
return scores;
452+
})
453+
} else {
454+
options.chat_id = chat_id;
455+
options.message_id = id;
456+
return this.call('getGameHighScores', options).then((res) => {
457+
var scores = [];
458+
res.body.result.forEach(score => {
459+
scores.push(new GameHighScore(score, this))
460+
});
461+
return scores;
462+
})
463+
}
464+
};
465+
381466
this.getChatAdministrators = (chat_id) => {
382467
if (chat_id instanceof Chat) chat_id = chat_id.id;
383468
return this.call('getChatAdministrators', {chat_id: chat_id}).then((res) => {
@@ -445,6 +530,7 @@ function Bot(token, options) {
445530
};
446531

447532
this.handleMessage = (message) => {
533+
if (this.debug) console.log(`Executing message handler handleMessage for message #${message.message_id}`);
448534
if (message.new_chat_member) this.emitter.emit('new_chat_member', message.new_chat_member, message);
449535
if (message.left_chat_member) this.emitter.emit('left_chat_member', message.left_chat_member, message);
450536
if (message.new_chat_title) this.emitter.emit('new_chat_title', message.new_chat_title, message);
@@ -461,6 +547,7 @@ function Bot(token, options) {
461547
};
462548

463549
this.handleProfiles = (message) => {
550+
if (this.debug) console.log(`Executing message handler handleProfiles for message #${message.message_id}`);
464551
var edited = false;
465552
if (!profiles.users[message.from.id] && this.saveUsers) {
466553
profiles.users[message.from.id] = message.from;
@@ -478,6 +565,7 @@ function Bot(token, options) {
478565
};
479566

480567
this.handleForms = (message) => {
568+
if (this.debug) console.log(`Executing message handler handleForms for message #${message.message_id}`);
481569
var id = message.from.id;
482570
if (this.forms[id] && message.chat.type == 'private') {
483571
var next = false;
@@ -567,7 +655,6 @@ function Bot(token, options) {
567655
}
568656
if (!this.webhookFunction) {
569657
this.express_app.post(this.webhookRoute, (req, res) => {
570-
console.log(req.body);
571658
this.handleUpdates([req.body]);
572659
res.send({})
573660
})

lib/CallbackQuery.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const User = require('./User.js'),
33
Message = require('./Message.js'),
4+
Chat = require('./Chat.js'),
45
extend = require('util')._extend;
56

67
function CallbackQuery(object, bot) {
@@ -11,8 +12,15 @@ function CallbackQuery(object, bot) {
1112
this.from = new User(this.from, bot);
1213
if (this.message) this.message = new Message(this.message, bot);
1314

14-
this.answer = (text, alert) => {
15-
this.bot.answerCallbackQuery(this.id, text, alert)
15+
this.answer = (options, alert) => {
16+
// TODO Drop support for the old method signature
17+
if (typeof options === 'string') {
18+
options = {
19+
text: options,
20+
alert: alert
21+
}
22+
}
23+
this.bot.answerCallbackQuery(this.id, options || options)
1624
}
1725
}
1826

lib/Chat.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function Chat(object, bot) {
3434
return this.bot.sendContact(this.id, phone_number, first_name, options)
3535
};
3636

37+
this.sendGame = (game_short_name, options) => {
38+
return this.bot.sendGame(this.id, game_short_name, options)
39+
}
40+
3741
this.kickMember = (user_id) => {
3842
if (user_id instanceof User) user_id = user_id.id;
3943
return this.bot.kickChatMember(this.id, user_id)

lib/Game.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
const User = require('./User.js'),
3+
Chat = require('./Chat.js'),
4+
PhotoSize = require('./PhotoSize.js'),
5+
Animation = require('./Animation.js'),
6+
extend = require('util')._extend;
7+
8+
function Game(object, bot) {
9+
if (object == undefined) return;
10+
extend(this, object);
11+
12+
this.commands = [];
13+
this.mentions = [];
14+
this.text_mentions = [];
15+
this.hashtags = [];
16+
this.links = [];
17+
this.command = undefined;
18+
this.bot = bot;
19+
20+
if (this.text_entities) {
21+
this.text_entities.forEach((entity) => {
22+
var offset = entity.offset,
23+
length = entity.length,
24+
text = this.text.substr(offset, offset + length);
25+
switch (entity.type) {
26+
case 'bot_command':
27+
var command,
28+
rawCommand = command = text,
29+
args = this.text.substr(offset + length +1).split(" "),
30+
index = rawCommand.indexOf('@'),
31+
recipient;
32+
33+
if (index !== -1) {
34+
recipient = rawCommand.substr(index);
35+
command = rawCommand.substr(0, index);
36+
}
37+
this.commands.push({command: command, recipient: recipient, args: args});
38+
if (offset === 0) {
39+
this.command = {command: command, recipient: recipient, args: args}
40+
}
41+
break;
42+
case 'mention':
43+
this.mentions.push(text);
44+
break;
45+
case 'hashtag':
46+
this.hashtags.push(text);
47+
break;
48+
case 'url':
49+
this.links.push({type: 'url', url: text, text: text});
50+
break;
51+
case 'text_link':
52+
this.links.push({type: 'link', url: entity.url, text: text});
53+
break;
54+
case 'text_mention':
55+
this.text_mentions.push({text: text, user: new User(entity.user, this.bot)});
56+
break;
57+
}
58+
})
59+
}
60+
61+
if (this.photo) {
62+
let sizes = [];
63+
this.photo.forEach((size) => {
64+
sizes.push(new PhotoSize(size, this.bot))
65+
});
66+
this.photo = sizes;
67+
}
68+
69+
if (this.animation) {
70+
this.animation = new Animation(this.animation, this.bot);
71+
}
72+
}
73+
74+
module.exports = Game;

lib/GameHighScore.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const User = require('./User.js'),
3+
extend = require('util')._extend;
4+
5+
function GameHighScore(object, bot) {
6+
extend(this, object);
7+
this.bot = bot;
8+
9+
this.user = new User(this.user, bot);
10+
11+
}
12+
13+
module.exports = GameHighScore;

lib/InlineQueryResultGame.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const extend = require('util')._extend;
3+
4+
function InlineQueryResultGame(id, game_short_name, options) {
5+
extend(this, options);
6+
this.type = 'article';
7+
this.id = id;
8+
this.game_short_name = game_short_name;
9+
}
10+
11+
module.exports = InlineQueryResultGame;

lib/Message.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const User = require('./User.js'),
33
Chat = require('./Chat.js'),
44
PhotoSize = require('./PhotoSize.js'),
5+
Game = require('./Game'),
56
extend = require('util')._extend;
67

78
function Message(object, bot) {
@@ -81,6 +82,10 @@ function Message(object, bot) {
8182
this.new_chat_photo = sizes;
8283
}
8384

85+
if (this.game) {
86+
this.game = new Game(this.game, this.bot);
87+
}
88+
8489
this.reply = (text, options) => {
8590
options = options || {};
8691
options.reply_to_message_id = this.message_id;
@@ -105,6 +110,14 @@ function Message(object, bot) {
105110
this.editReplyMarkup = (markup, inline, options) => {
106111
return bot.editMessageReplyMarkup(this.message_id, markup, inline, options, this.chat.id)
107112
};
113+
114+
this.setGameScore = (user_id, score, inline, options) => {
115+
return bot.setGameScore(this.message_id, user_id, score, inline, options, this.chat.id)
116+
}
117+
118+
this.getGameHighScores = (user_id, inline) => {
119+
return bot.getGameHighScores(this.message_id, user_id, inline, this.chat.id)
120+
}
108121
}
109122

110123
module.exports = Message;

0 commit comments

Comments
 (0)