-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
167 lines (141 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Load config params
*/
const config = require('./config');
/**
* Require necessary packages
*/
const webp = require('webp-converter');
const TelegramBot = require('node-telegram-bot-api');
const path = require('path');
const fs = require('fs');
const { uploadByBuffer } = require('telegraph-uploader');
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.rmdirSync(uploadsDir, { recursive: true });
}
/**
* Throw error if bot toke is missing
*/
if (!config.token) {
throw new Error('Bot token is missing. Check config file.');
}
/**
* Bot connection settings
*/
let botParams = {
polling: true
};
/**
* Create a Bot's instance
*/
const bot = new TelegramBot(config.token, botParams);
/**
* Process command /start
*/
bot.onText(/\/start/, msg => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Send me a sticker and I\'ll return you a link to the image');
});
/**
* Process message with a sticker
*/
bot.on('sticker', (msg) => {
/**
* Define chatId and directory for uploaded files
*/
const chatId = msg.chat.id;
try {
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
if (msg.sticker.is_animated) {
bot.sendMessage(chatId, 'Sorry, I cannot process animated stickers yet.');
return;
}
/**
* Download webp file by sticker's id
*/
bot.downloadFile(msg.sticker.file_id, uploadsDir)
.then(async (pathToImage) => {
/**
* Define path to png image
* Add '.png' at the end of name of webp archive
*/
const pathToImagePng = `${pathToImage}.png`;
/**
* Send chat action
*/
bot.sendChatAction(chatId, 'upload_photo');
/**
* Convert webp to png
*/
webp.dwebp(pathToImage, pathToImagePng, "-o")
.then((status, error) => {
return uploadByBuffer(fs.readFileSync(pathToImagePng), 'image/png');
})
.then(({ link, path }) => {
bot.sendMessage(chatId, link);
try { fs.unlinkSync(pathToImage); } catch(e) {}
try { fs.unlinkSync(pathToImagePng); } catch(e) {}
})
.catch(error => {
/**
* On error we just send a message to user
*/
bot.sendMessage(chatId, 'Send me another sticker please');
console.log(error);
});
})
.catch(error => {
bot.sendMessage(chatId, 'Something went wrong');
console.log(error);
});
} catch (error) {
bot.sendMessage(chatId, 'Something bad went wrong');
console.log(error);
}
});
/**
* Process message with a photo or photos
*/
bot.on('photo', (msg) => {
const chatId = msg.chat.id;
try {
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
// get the last image item from array
const photoItem = msg.photo[msg.photo.length - 1];
const photoFileId = photoItem.file_id;
/**
* Download image file by id
*/
bot.downloadFile(photoFileId, uploadsDir)
.then(async (pathToImage) => {
/**
* Send chat action
*/
bot.sendChatAction(chatId, 'upload_photo');
uploadByBuffer(fs.readFileSync(pathToImage), 'image/png')
.then(({ link, path }) => {
bot.sendMessage(chatId, link);
try { fs.unlinkSync(pathToImage); } catch(e) {}
})
.catch(error => {
/**
* On error we just send a message to user
*/
bot.sendMessage(chatId, 'Send me another image please');
console.log(error);
});
})
.catch(error => {
bot.sendMessage(chatId, 'Something went wrong');
console.log(error);
});
} catch (error) {
bot.sendMessage(chatId, 'Something bad went wrong');
console.log(error);
}
})