|
1 | | -const Telegraf = require('telegraf') |
2 | | -const Telegram = require('telegraf/telegram') |
3 | | -const download = require('image-downloader') |
4 | | -const moment = require('moment'); |
| 1 | +const Telegraf = require("telegraf"); |
| 2 | +const Telegram = require("telegraf/telegram"); |
| 3 | +const download = require("image-downloader"); |
| 4 | +const moment = require("moment"); |
5 | 5 |
|
6 | 6 | var Bot = class { |
7 | | - constructor(botToken, imageFolder, imageWatchdog, showVideo, logger) { |
8 | | - var self = this; |
9 | | - this.bot = new Telegraf(botToken); |
10 | | - this.telegram = new Telegram(botToken); |
11 | | - this.logger = logger; |
12 | | - this.imageFolder = imageFolder; |
13 | | - this.imageWatchdog = imageWatchdog; |
14 | | - this.showVideo = showVideo; |
| 7 | + constructor( |
| 8 | + botToken, |
| 9 | + imageFolder, |
| 10 | + imageWatchdog, |
| 11 | + showVideo, |
| 12 | + whitelistChats, |
| 13 | + logger |
| 14 | + ) { |
| 15 | + var self = this; |
| 16 | + this.bot = new Telegraf(botToken); |
| 17 | + this.telegram = new Telegram(botToken); |
| 18 | + this.logger = logger; |
| 19 | + this.imageFolder = imageFolder; |
| 20 | + this.imageWatchdog = imageWatchdog; |
| 21 | + this.showVideo = showVideo; |
| 22 | + this.whitelistChats = whitelistChats; |
15 | 23 |
|
16 | | - //get bot name |
17 | | - this.bot.telegram.getMe().then((botInfo) => { |
18 | | - this.bot.options.username = botInfo.username; |
19 | | - this.logger.info('Using bot with name ' + this.bot.options.username + '.'); |
20 | | - }) |
| 24 | + //get bot name |
| 25 | + this.bot.telegram.getMe().then((botInfo) => { |
| 26 | + this.bot.options.username = botInfo.username; |
| 27 | + this.logger.info( |
| 28 | + "Using bot with name " + this.bot.options.username + "." |
| 29 | + ); |
| 30 | + }); |
21 | 31 |
|
22 | | - //Welcome message on bot start |
23 | | - this.bot.start((ctx) => ctx.reply('Welcome')); |
| 32 | + //Welcome message on bot start |
| 33 | + this.bot.start((ctx) => ctx.reply("Welcome")); |
24 | 34 |
|
25 | | - //Help message |
26 | | - this.bot.help((ctx) => ctx.reply('Send me an image.')); |
| 35 | + //Help message |
| 36 | + this.bot.help((ctx) => ctx.reply("Send me an image.")); |
27 | 37 |
|
28 | | - //Download incoming photo |
29 | | - this.bot.on('photo', (ctx) => { |
30 | | - this.telegram.getFileLink(ctx.message.photo[ctx.message.photo.length - 1].file_id).then( |
31 | | - link => { |
32 | | - download.image({ |
33 | | - url: link, |
34 | | - dest: this.imageFolder + '/' + moment().format('x') + '.jpg' |
35 | | - }) |
36 | | - .then(({ |
37 | | - filename, |
38 | | - image |
39 | | - }) => { |
40 | | - this.newImage(filename, ctx.message.from.first_name, ctx.message.caption); |
41 | | - }) |
42 | | - .catch((err) => { |
43 | | - this.logger.error(err); |
44 | | - }) |
45 | | - } |
46 | | - ) |
47 | | - }); |
| 38 | + //Download incoming photo |
| 39 | + this.bot.on("photo", (ctx) => { |
| 40 | + if ( |
| 41 | + !( |
| 42 | + this.whitelistChats.length > 0 && |
| 43 | + this.whitelistChats.indexOf(ctx.update.message.from.id) !== -1 |
| 44 | + ) |
| 45 | + ) { |
| 46 | + console.log( |
| 47 | + "Whitelist triggered:", |
| 48 | + ctx.update.message.from.id, |
| 49 | + this.whitelistChats, |
| 50 | + this.whitelistChats.indexOf(ctx.update.message.from.id) |
| 51 | + ); |
| 52 | + ctx.reply( |
| 53 | + "Hey there, this bot is whitelisted, pls add your chat id to the config file" |
| 54 | + ); |
| 55 | + return; |
| 56 | + } |
48 | 57 |
|
49 | | - //Download incoming video |
50 | | - this.bot.on('video', (ctx) => { |
51 | | - if (this.showVideo) { |
52 | | - this.telegram.getFileLink(ctx.message.video.file_id).then( |
53 | | - link => { |
54 | | - download.image({ |
55 | | - url: link, |
56 | | - dest: this.imageFolder + '/' + moment().format('x') + '.mp4' |
57 | | - }) |
58 | | - .then(({ |
59 | | - filename, |
60 | | - image |
61 | | - }) => { |
62 | | - this.newImage(filename, ctx.message.from.first_name, ctx.message.caption); |
63 | | - }) |
64 | | - .catch((err) => { |
65 | | - this.logger.error(err); |
66 | | - }) |
67 | | - } |
68 | | - ) |
69 | | - } |
70 | | - }); |
| 58 | + this.telegram |
| 59 | + .getFileLink(ctx.message.photo[ctx.message.photo.length - 1].file_id) |
| 60 | + .then((link) => { |
| 61 | + download |
| 62 | + .image({ |
| 63 | + url: link, |
| 64 | + dest: this.imageFolder + "/" + moment().format("x") + ".jpg" |
| 65 | + }) |
| 66 | + .then(({ filename, image }) => { |
| 67 | + this.newImage( |
| 68 | + filename, |
| 69 | + ctx.message.from.first_name, |
| 70 | + ctx.message.caption |
| 71 | + ); |
| 72 | + }) |
| 73 | + .catch((err) => { |
| 74 | + this.logger.error(err); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
71 | 78 |
|
72 | | - this.bot.catch((err) => { |
73 | | - this.logger.error(err) |
74 | | - }) |
| 79 | + //Download incoming video |
| 80 | + this.bot.on("video", (ctx) => { |
| 81 | + if ( |
| 82 | + !( |
| 83 | + this.whitelistChats.length > 0 && |
| 84 | + this.whitelistChats.indexOf(ctx.update.message.from.id) !== -1 |
| 85 | + ) |
| 86 | + ) { |
| 87 | + console.log( |
| 88 | + "Whitelist triggered:", |
| 89 | + ctx.update.message.from.id, |
| 90 | + this.whitelistChats, |
| 91 | + this.whitelistChats.indexOf(ctx.update.message.from.id) |
| 92 | + ); |
| 93 | + ctx.reply( |
| 94 | + "Hey there, this bot is whitelisted, pls add your chat id to the config file" |
| 95 | + ); |
| 96 | + return; |
| 97 | + } |
75 | 98 |
|
76 | | - //Some small conversation |
77 | | - this.bot.hears('hi', (ctx) => ctx.reply('Hey there')); |
| 99 | + if (this.showVideo) { |
| 100 | + this.telegram.getFileLink(ctx.message.video.file_id).then((link) => { |
| 101 | + download |
| 102 | + .image({ |
| 103 | + url: link, |
| 104 | + dest: this.imageFolder + "/" + moment().format("x") + ".mp4" |
| 105 | + }) |
| 106 | + .then(({ filename, image }) => { |
| 107 | + this.newImage( |
| 108 | + filename, |
| 109 | + ctx.message.from.first_name, |
| 110 | + ctx.message.caption |
| 111 | + ); |
| 112 | + }) |
| 113 | + .catch((err) => { |
| 114 | + this.logger.error(err); |
| 115 | + }); |
| 116 | + }); |
| 117 | + } |
| 118 | + }); |
78 | 119 |
|
79 | | - this.logger.info('Bot created!'); |
80 | | - } |
| 120 | + this.bot.catch((err) => { |
| 121 | + this.logger.error(err); |
| 122 | + }); |
81 | 123 |
|
82 | | - startBot() { |
83 | | - //Start bot |
84 | | - var self = this; |
85 | | - this.bot.startPolling(30, 100, null, () => setTimeout(() => self.startBot(), 30000)); |
86 | | - this.logger.info('Bot started!'); |
87 | | - } |
| 124 | + //Some small conversation |
| 125 | + this.bot.hears(/hi/i, (ctx) => { |
| 126 | + ctx.reply( |
| 127 | + `Hey there ${ctx.chat.first_name} \n Your ChatID is ${ctx.chat.id}` |
| 128 | + ); |
| 129 | + console.log(ctx.chat); |
| 130 | + }); |
88 | 131 |
|
89 | | - newImage(src, sender, caption) { |
90 | | - //tell imageWatchdog that a new image arrived |
91 | | - this.imageWatchdog.newImage(src, sender, caption); |
92 | | - } |
| 132 | + this.logger.info("Bot created!"); |
| 133 | + } |
93 | 134 |
|
| 135 | + startBot() { |
| 136 | + //Start bot |
| 137 | + var self = this; |
| 138 | + this.bot.startPolling(30, 100, null, () => |
| 139 | + setTimeout(() => self.startBot(), 30000) |
| 140 | + ); |
| 141 | + this.logger.info("Bot started!"); |
| 142 | + } |
94 | 143 |
|
95 | | -} |
| 144 | + newImage(src, sender, caption) { |
| 145 | + //tell imageWatchdog that a new image arrived |
| 146 | + this.imageWatchdog.newImage(src, sender, caption); |
| 147 | + } |
| 148 | +}; |
96 | 149 |
|
97 | 150 | /*************** DO NOT EDIT THE LINE BELOW ***************/ |
98 | 151 | if (typeof module !== "undefined") { |
99 | | - module.exports = Bot; |
| 152 | + module.exports = Bot; |
100 | 153 | } |
0 commit comments