-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
62 lines (56 loc) · 1.95 KB
/
bot.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
/**
* This is telegram bot for recognising text from images
*
* @author Vasyl Kutsyk
* @licence MIT
*
*/
const Telegraf = require('telegraf'),
Telegram = require('telegraf/telegram'),
request = require('request');
const azureCongitiveServiceKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
uriBase = "https://northeurope.api.cognitive.microsoft.com/vision/v1.0/ocr?language=unk",
headers = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": azureCongitiveServiceKey
},
options = {
url: uriBase,
method: 'POST',
headers: headers
};
const botKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
bot = new Telegraf(botKey),
telegram = new Telegram(botKey);
const extractTextFromResponse = (response) => {
let text = '';
response.regions.forEach((region) => {
region.lines.forEach((line) => {
line.words.forEach((word) => {
text += word.text + ' ';
});
});
});
return text;
};
bot.command('help', (ctx) => ctx.reply('This bot recognise text from image. Just send a picture for it.'));
bot.on('photo', (ctx) => {
let receivedPhoto = ctx.update.message.photo;
let receivedPhotoFileId = receivedPhoto[receivedPhoto.length - 1].file_id;
telegram.getFileLink(receivedPhotoFileId).then((fileLink) => {
options.body = `{"url": "${fileLink}"}`;
request(options, (err, res, body) => {
if (!err && res.statusCode === 200) {
let response = JSON.parse(body);
if (response.regions.length > 0)
ctx.reply(extractTextFromResponse(response));
else
ctx.replyWithHTML("<code>No text was detected.</code>");
}
else {
ctx.replyWithHTML(`<code>${err}</code>`);
}
});
}).catch((error) => ctx.replyWithHTML(`<code>${error}</code>`))
});
bot.startPolling();