22const inquirer = require ( 'inquirer' ) ;
33const https = require ( 'https' ) ;
44const ora = require ( 'ora' ) ;
5+ const parseChunk = require ( './src/utils/parseChunk' ) ;
56const { printError } = require ( './src/utils/print' ) ;
67
8+ const handleAnswers = ( answers ) => {
9+ const { botToken, channelUsername } = answers ;
10+ const url = `https://api.telegram.org/bot${ botToken } /getChat?chat_id=@${ channelUsername } ` ;
11+ const spinner = ora ( 'Fetching ChatId...' ) . start ( ) ;
12+ https . get ( url , ( res ) => {
13+ res . on ( 'data' , ( data ) => {
14+ spinner . stop ( ) ;
15+
16+ const response = parseChunk ( data ) ;
17+ if ( ! response . ok ) return printError ( 'Invalid botToken or channel username provided!' ) ;
18+ return console . log ( 'ChatId of the channel:' , response . result . id ) ;
19+ } ) ;
20+ res . on ( 'error' , ( error ) => printError ( `Telegram server error: ${ error } ` ) ) ;
21+ } ) ;
22+ } ;
23+
24+ const handleError = ( error ) => {
25+ if ( error . isTtyError ) {
26+ console . log ( 'Prompt couldn\'t be rendered in the current environment' ) ;
27+ // Prompt couldn't be rendered in the current environment
28+ } else {
29+ // Something else when wrong
30+ }
31+ } ;
32+
733inquirer
834 . prompt ( [
935 {
@@ -17,26 +43,5 @@ inquirer
1743 name : 'channelUsername' ,
1844 } ,
1945 ] )
20- . then ( ( answers ) => {
21- const { botToken, channelUsername } = answers ;
22- const url = `https://api.telegram.org/bot${ botToken } /getChat?chat_id=@${ channelUsername } ` ;
23- const spinner = ora ( 'Fetching ChatId...' ) . start ( ) ;
24- https . get ( url , ( res ) => {
25- res . on ( 'data' , ( data ) => {
26- spinner . stop ( ) ;
27-
28- const response = JSON . parse ( data . toString ( ) ) ;
29- if ( ! response . ok ) return printError ( 'Invalid botToken or channel username provided!' ) ;
30- return console . log ( 'ChatId of the channel:' , response . result . id ) ;
31- } ) ;
32- res . on ( 'error' , ( error ) => printError ( `Telegram server error: ${ error } ` ) ) ;
33- } ) ;
34- } )
35- . catch ( ( error ) => {
36- if ( error . isTtyError ) {
37- console . log ( 'Prompt couldn\'t be rendered in the current environment' ) ;
38- // Prompt couldn't be rendered in the current environment
39- } else {
40- // Something else when wrong
41- }
42- } ) ;
46+ . then ( handleAnswers )
47+ . catch ( handleError ) ;
0 commit comments