@@ -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 } )
0 commit comments