@@ -25,7 +25,8 @@ function Bot(token) {
2525 this . emitter = new events . EventEmitter ( ) ;
2626
2727 this . handleAPIError = ( err ) => {
28- console . log ( `An error occurred while querying the Telegram API - ${ JSON . parse ( err . response . text ) . error_code } ${ JSON . parse ( err . response . text ) . description } ` )
28+ if ( err . response ) { console . log ( `An error occurred while querying the Telegram API - ${ err . response . text ? JSON . parse ( err . response . text ) . error_code : "" } ${ err . response . text ? JSON . parse ( err . response . text ) . description : "" } ` ) }
29+ else console . log ( err )
2930 } ;
3031
3132 this . get = ( path , query ) => {
@@ -93,14 +94,15 @@ function Bot(token) {
9394 } ) ;
9495 }
9596 if ( result . inline_query ) this . emitter . emit ( 'inline_query' , new InlineQuery ( result . inline_query , this ) ) ;
96- if ( result . callback_query ) this . emitter . emit ( 'callback_query' , new CallbackQuery ( result . callback_query , this ) )
97- if ( result . edited_message ) this . emitter . emit ( 'edited_message' , new Message ( result . edited_message , this ) )
97+ if ( result . callback_query ) this . emitter . emit ( 'callback_query' , new CallbackQuery ( result . callback_query , this ) ) ;
98+ if ( result . edited_message ) this . emitter . emit ( 'edited_message' , new Message ( result . edited_message , this ) ) ;
99+ if ( result . chosen_inline_result ) this . emitter . emit ( 'chosen_inline_result' , new ChosenInlineResult ( result . chosen_inline_result , this ) )
98100 } ) ;
99101 }
100102 this . getUpdates ( ) ;
101103 } )
102104 . catch ( ( err ) => {
103- console . log ( err )
105+ this . handleAPIError ( err ) ;
104106 // Wait before retrying in order not to flood Telegram's servers
105107 setTimeout ( ( ) => {
106108 this . getUpdates ( ) ;
@@ -113,19 +115,21 @@ function Bot(token) {
113115 this . commands . push ( { command : command , description : description , callback : callback } ) ;
114116 } ;
115117
116- this . getChat = ( id ) => {
117- return this . call ( 'getChat' , { chat_id : id } ) . then ( ( res ) => {
118+ this . getChat = ( chat_id ) => {
119+ return this . call ( 'getChat' , { chat_id : chat_id } ) . then ( ( res ) => {
118120 return new Chat ( res . body . result , this )
119121 } )
120122 } ;
121123
122- this . leaveChat = ( id ) => {
123- return this . call ( 'leaveChat' , { chat_id : id } ) . then ( ( res ) => {
124+ this . leaveChat = ( chat_id ) => {
125+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
126+ return this . call ( 'leaveChat' , { chat_id : chat_id } ) . then ( ( res ) => {
124127 return res . body . result ;
125128 } )
126129 } ;
127130
128131 this . sendMessage = ( chat_id , text , options ) => {
132+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
129133 options = options || { } ;
130134 options . chat_id = chat_id ;
131135 options . text = text ;
@@ -135,6 +139,7 @@ function Bot(token) {
135139 } ;
136140
137141 this . sendLocation = ( chat_id , longitude , latitude , options ) => {
142+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
138143 options = options || { } ;
139144 options . chat_id = chat_id ;
140145 options . longitude = longitude ;
@@ -145,6 +150,7 @@ function Bot(token) {
145150 } ;
146151
147152 this . sendVenue = ( chat_id , longitude , latitude , title , address , options ) => {
153+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
148154 options = options || { } ;
149155 options . chat_id = chat_id ;
150156 options . longitude = longitude ;
@@ -157,6 +163,7 @@ function Bot(token) {
157163 } ;
158164
159165 this . sendContact = ( chat_id , phone_number , first_name , options ) => {
166+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
160167 options = options || { } ;
161168 options . chat_id = chat_id ;
162169 options . phone_number = phone_number ;
@@ -168,6 +175,7 @@ function Bot(token) {
168175
169176
170177 this . forwardMessage = ( chat_id , from_chat_id , message_id , options ) => {
178+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
171179 options = options || { } ;
172180 options . chat_id = chat_id ;
173181 options . from_chat_id = from_chat_id ;
@@ -181,9 +189,10 @@ function Bot(token) {
181189 return this . call ( 'answerCallbackQuery' , { callback_query_id : id , text : text , alert : alert } ) ;
182190 } ;
183191
184- this . getUserProfilePhotos = ( id , options ) => {
192+ this . getUserProfilePhotos = ( user_id , options ) => {
193+ if ( user_id instanceof User ) user_id = user_id . id ;
185194 options = options || { } ;
186- options . user_id = id ;
195+ options . user_id = user_id ;
187196 return this . call ( 'getUserProfilePhotos' , options ) . then ( ( res ) => {
188197 return new UserProfilePhotos ( res . body . result , this ) ;
189198 } ) ;
@@ -196,6 +205,7 @@ function Bot(token) {
196205 } ;
197206
198207 this . sendFile = ( chat_id , type , path , options ) => {
208+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
199209 options = options || { } ;
200210 options . chat_id = chat_id ;
201211 var method ;
@@ -310,6 +320,7 @@ function Bot(token) {
310320 } ;
311321
312322 this . getChatAdministrators = ( chat_id ) => {
323+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
313324 return this . call ( 'getChatAdministrators' , { chat_id : chat_id } ) . then ( ( res ) => {
314325 var administrators = res . body . result ;
315326 var processed = [ ] ;
@@ -321,32 +332,37 @@ function Bot(token) {
321332 } ;
322333
323334 this . getChatMember = ( chat_id , user_id ) => {
335+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
324336 if ( user_id instanceof User ) user_id = user_id . id ;
325337 return this . call ( 'getChatMember' , { chat_id : chat_id , user_id : user_id } ) . then ( ( res ) => {
326338 return new User ( res . body . result , this ) ;
327339 } )
328340 } ;
329341
330342 this . kickChatMember = ( chat_id , user_id ) => {
343+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
331344 if ( user_id instanceof User ) user_id = user_id . id ;
332345 return this . call ( 'kickChatMember' , { chat_id : chat_id , user_id : user_id } ) . then ( ( res ) => {
333346 return res . body . result ;
334347 } )
335348 } ;
336349 this . unbanChatMember = ( chat_id , user_id ) => {
350+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
337351 if ( user_id instanceof User ) user_id = user_id . id ;
338352 return this . call ( 'unbanChatMember' , { chat_id : chat_id , user_id : user_id } ) . then ( ( res ) => {
339353 return res . body . result ;
340354 } )
341355 } ;
342356
343357 this . getChatMembersCount = ( chat_id ) => {
358+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
344359 return this . call ( 'getChatMembersCount' , { chat_id : chat_id } ) . then ( ( res ) => {
345360 return res . body . result ;
346361 } )
347362 } ;
348363
349364 this . sendChatAction = ( chat_id , action ) => {
365+ if ( chat_id instanceof Chat ) chat_id = chat_id . id ;
350366 return this . call ( 'sendChatAction' , { chat_id : chat_id , action : action } ) . then ( ( res ) => {
351367 return res . body . result ;
352368 } )
@@ -364,11 +380,23 @@ function Bot(token) {
364380 } ;
365381
366382 this . handleMessage = ( message ) => {
383+ if ( message . new_chat_member ) this . emitter . emit ( 'new_chat_member' , message . new_chat_member , message ) ;
384+ if ( message . left_chat_member ) this . emitter . emit ( 'left_chat_member' , message . left_chat_member , message ) ;
385+ if ( message . new_chat_title ) this . emitter . emit ( 'new_chat_title' , message . new_chat_title , message ) ;
386+ if ( message . new_chat_photo ) this . emitter . emit ( 'new_chat_photo' , message . new_chat_photo , message ) ;
387+ if ( message . delete_chat_photo ) this . emitter . emit ( 'delete_chat_photo ' , message . delete_chat_photo , message ) ;
388+ if ( message . group_chat_created ) this . emitter . emit ( 'group_chat_created' , message . group_chat_created , message ) ;
389+ if ( message . supergroup_chat_created ) this . emitter . emit ( 'supergroup_chat_created' , message . supergroup_chat_created , message ) ;
390+ if ( message . channel_chat_created ) this . emitter . emit ( 'channel_chat_created' , message . channel_chat_created , message ) ;
391+ if ( message . migrate_to_chat_id ) this . emitter . emit ( 'migrate_to_chat_id' , message . migrate_to_chat_id , message ) ;
392+ if ( message . migrate_from_chat_id ) this . emitter . emit ( 'migrate_from_chat_id' , message . migrate_from_chat_id , message ) ;
393+ if ( message . pinned_message ) this . emitter . emit ( 'pinned_message' , message . pinned_message , message ) ;
394+
367395 this . emitter . emit ( 'message' , message ) ;
368396 } ;
369397
370398 this . init = ( ) => {
371- if ( ! this . token ) throw "Unvalid token!" ;
399+ if ( ! this . token ) throw "Invalid token!" ;
372400 this . messageHandlers = [
373401 this . handleCommands ,
374402 this . handleMessage
@@ -384,8 +412,9 @@ function Bot(token) {
384412 } ;
385413
386414 this . call ( 'getMe' , { } ) . then ( ( res ) => {
387- this . me = res . body . result ;
388- } ) ;
415+ if ( res ) this . me = res . body . result ;
416+ else throw "Unable to retrieve informations about the bot!"
417+ } ) . catch ( err => this . handleAPIError ( err ) ) ;
389418
390419 // For more complex event handling use this.emitter
391420 this . on = ( event , listener ) => {
0 commit comments