@@ -24,6 +24,8 @@ function Bot(token, options) {
2424 this . messageHandlers = [ ] ;
2525 this . me = { } ;
2626 this . emitter = new events . EventEmitter ( ) ;
27+ this . saveUsers = true ;
28+ this . saveChats = true ;
2729 extend ( this , options ) ;
2830
2931 this . handleAPIError = ( err ) => {
@@ -49,10 +51,12 @@ function Bot(token, options) {
4951 return this . post ( path , args )
5052 . end ( )
5153 . then ( ( res ) => {
52- if ( ! res . body . ok ) throw "Telegram API response was not OK" ;
54+ if ( ! res . body . ok ) throw new Error ( "Telegram API response was not OK" ) ;
5355 return res ;
5456 } ) . catch ( ( err ) => {
55- this . handleAPIError ( err ) ;
57+ if ( err . response . error . status == 403 || err . response . error . status == 400 ) {
58+ throw new Error ( JSON . parse ( err . response . error . text ) . description ) ;
59+ } else this . handleAPIError ( err ) ;
5660 } )
5761 } ;
5862
@@ -68,7 +72,9 @@ function Bot(token, options) {
6872 if ( ! res . body . ok ) throw "Telegram API response was not OK" ;
6973 return res ;
7074 } ) . catch ( ( err ) => {
71- this . handleAPIError ( err ) ;
75+ if ( err . response . error . status == 403 || err . response . error . status == 400 ) {
76+ throw new Error ( JSON . parse ( err . response . error . text ) . description ) ;
77+ } else throw err ;
7278 } )
7379 } ;
7480
@@ -418,21 +424,21 @@ function Bot(token, options) {
418424
419425 this . handleProfiles = ( message ) => {
420426 var edited = false ;
421- if ( ! profiles . users [ message . from . id ] ) {
427+ if ( ! profiles . users [ message . from . id ] && this . saveUsers ) {
422428 profiles . users [ message . from . id ] = message . from ;
423429 profiles . users [ message . from . id ] . bot = undefined ;
424430 edited = true ;
425431 }
426- if ( ! profiles . chats [ message . chat . id ] ) {
432+ if ( ! profiles . chats [ message . chat . id ] && this . saveChats ) {
427433 profiles . chats [ message . chat . id ] = message . chat ;
428434 profiles . chats [ message . chat . id ] . bot = undefined ;
429435 edited = true ;
430436 }
431- if ( edited ) fs . writeFile ( this . profiles_path , JSON . stringify ( profiles ) , ( err ) => { console . log ( err ) } )
437+ if ( edited ) fs . writeFile ( this . profiles_path , JSON . stringify ( profiles ) , ( err ) => { if ( err ) console . log ( err ) } )
432438 } ;
433439
434440 this . broadcast = ( filter , callback ) => {
435- if ( ! this . profiles_path ) throw "Profiles path is not defined" ;
441+ if ( ! this . profiles_path ) throw new Error ( "Profiles path is not defined" ) ;
436442 if ( profiles . chats ) {
437443 for ( var chat in profiles . chats ) {
438444 if ( profiles . chats . hasOwnProperty ( chat ) ) {
@@ -447,7 +453,7 @@ function Bot(token, options) {
447453 } ;
448454
449455 this . init = ( ) => {
450- if ( ! this . token ) throw "Invalid token!" ;
456+ if ( ! this . token ) throw new Error ( "Invalid token!" ) ;
451457 this . messageHandlers = [
452458 this . handleCommands ,
453459 this . handleMessage
@@ -469,8 +475,7 @@ function Bot(token, options) {
469475
470476 this . call ( 'getMe' , { } ) . then ( ( res ) => {
471477 if ( res ) this . me = res . body . result ;
472- else throw "Unable to retrieve informations about the bot!"
473- } ) . catch ( err => this . handleAPIError ( err ) ) ;
478+ } ) . catch ( err => console . log ( err ) ) ;
474479
475480 // For more complex event handling use this.emitter
476481 this . on = ( event , listener ) => {
0 commit comments