Skip to content

Commit d471c1c

Browse files
authored
Merge pull request #2 from awesomelike/cli
Added CLI for fetching channel's chatId
2 parents ab525c0 + 5d9d9ec commit d471c1c

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

bin/which

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../cli.js');

cli.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-console */
2+
const inquirer = require('inquirer');
3+
const https = require('https');
4+
const ora = require('ora');
5+
const { printError } = require('./src/utils/print');
6+
7+
inquirer
8+
.prompt([
9+
{
10+
type: 'input',
11+
message: 'Enter your botToken:',
12+
name: 'botToken',
13+
},
14+
{
15+
type: 'input',
16+
message: 'Enter channel\'s username (without @):',
17+
name: 'channelUsername',
18+
},
19+
])
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+
});

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "express-notify-telegram",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A simple Express middleware to notify errors into Telegram",
55
"main": "index.js",
66
"scripts": {
77
"start": "node index.js"
88
},
9+
"bin": {
10+
"express-notify-telegram": "./bin/which"
11+
},
912
"keywords": [
1013
"telegram",
1114
"express",
@@ -19,7 +22,9 @@
1922
"eslint-plugin-import": "^2.22.1"
2023
},
2124
"dependencies": {
22-
"on-finished": "^2.3.0"
25+
"inquirer": "^7.3.3",
26+
"on-finished": "^2.3.0",
27+
"ora": "^5.1.0"
2328
},
2429
"repository": {
2530
"type": "git",

src/utils/print.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ const printWarning = (text) => console.warn(
55
'\x1b[0m', text,
66
);
77

8-
module.exports = { printWarning };
8+
const printError = (text) => console.error(
9+
'\x1b[31m', 'Invalid botToken or channel username provided!',
10+
);
11+
12+
module.exports = { printWarning, printError };

0 commit comments

Comments
 (0)