Skip to content

Commit 8ec5334

Browse files
committed
handle forbidden error
1 parent d471c1c commit 8ec5334

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

README.md

Whitespace-only changes.

cli.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@
22
const inquirer = require('inquirer');
33
const https = require('https');
44
const ora = require('ora');
5+
const parseChunk = require('./src/utils/parseChunk');
56
const { 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+
733
inquirer
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);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-notify-telegram",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A simple Express middleware to notify errors into Telegram",
55
"main": "index.js",
66
"scripts": {

src/sendMessage.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
const https = require('https');
2+
const parseChunk = require('./utils/parseChunk');
3+
const { printError } = require('./utils/print');
24

35
const sendMessage = (botToken, query) => {
46
const API = `https://api.telegram.org/bot${botToken}/sendMessage?${query}`;
5-
https.get(API);
7+
https.get(API, (res) => {
8+
res.on('data', (data) => {
9+
const response = parseChunk(data);
10+
if (!response.ok) {
11+
printError(`The notification got ignored. Reason: ${response.description}`);
12+
}
13+
});
14+
});
615
};
716

817
module.exports = sendMessage;

src/utils/parseChunk.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (data) => JSON.parse(data.toString());

0 commit comments

Comments
 (0)