Skip to content

Commit 9852e19

Browse files
Merge pull request #13 from s00500/master
Whitelisting of Chat Ids
2 parents 533af3c + c6b9f8b commit 9852e19

6 files changed

Lines changed: 187 additions & 118 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typings/
6262

6363
config/config.js
6464

65-
images
65+
images/*
66+
!images/.gitkeep
6667

6768
*.DS_Store

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
## Table Of Contents
1414

15+
- [Table Of Contents](#table-of-contents)
1516
- [Installation](#installation)
16-
- [Raspberry Pi](#automatic-installation-raspberry-pi-only)
17-
- [General](#manual-installation)
17+
- [Automatic Installation (Raspberry Pi only!)](#automatic-installation-raspberry-pi-only)
18+
- [Manual Installation](#manual-installation)
1819
- [Configuration](#configuration)
20+
- [Whitelist Chats](#whitelist-chats)
1921
- [Updating](#updating)
2022
- [Bot only mode (no GUI)](#bot-only-mode-no-gui)
2123
- [Building a TeleFrame](#building-a-teleframe)
@@ -62,6 +64,7 @@ The following properties can be configured:
6264
| **Option** | **Description** |
6365
| --- | --- |
6466
| `botToken` | The token of the Telegram Bot, which will recieve the images. How to create a bot and get the token is explained [here](https://core.telegram.org/bots#6-botfather). |
67+
| `whitelistChats` | Use this to only allow certain users to send photos to your Teleframe. See hints below. |
6568
| `showVideos` | When set to true, videos that are send to the bot are also shown. |
6669
| `imageFolder` | The folder where the images are stored. |
6770
| `fullscreen` | When set to true, TeleFrame will run in fullscreen mode. |
@@ -77,6 +80,10 @@ The following properties can be configured:
7780
| `turnOnHour` | Defines when the monitor shuld be turned on. |
7881
| `turnOffHour` | Defines when the monitor shuld be turned off. |
7982

83+
## Whitelist Chats
84+
85+
When you start your teleframe and send a "Hi" to the bot it will send you back the current chat id. Paste this id or several of them into the `whitelistChats` config option to only allow only pictures from these ids (eg `[1234567, 89101010]`). Leave empty (`[]`) for no whitelist.
86+
8087
## Updating
8188

8289
If you want to update your TeleFrame to the latest version, use your terminal to go to your TeleFrame folder and type the following command:

config/config.js.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var config = {
22
botToken: '',
3+
whitelistChats: [],
34
showVideos: true,
45
imageFolder: 'images',
56
fullscreen: true,
67
fadeTime: 1500,
78
interval: 10 * 1000,
89
imageCount: 30,
910
newPhotoMessage: 'Neues Foto von',
10-
newVideoMessage: 'New video from',
11+
newVideoMessage: 'Neues Video von',
1112
showSender: true,
1213
showCaption: true,
1314
toggleMonitor: true,

images/.gitkeep

Whitespace-only changes.

js/bot.js

Lines changed: 135 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,153 @@
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");
55

66
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;
1523

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+
});
2131

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"));
2434

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."));
2737

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+
}
4857

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+
});
7178

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+
}
7598

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+
});
78119

79-
this.logger.info('Bot created!');
80-
}
120+
this.bot.catch((err) => {
121+
this.logger.error(err);
122+
});
81123

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+
});
88131

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+
}
93134

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+
}
94143

95-
}
144+
newImage(src, sender, caption) {
145+
//tell imageWatchdog that a new image arrived
146+
this.imageWatchdog.newImage(src, sender, caption);
147+
}
148+
};
96149

97150
/*************** DO NOT EDIT THE LINE BELOW ***************/
98151
if (typeof module !== "undefined") {
99-
module.exports = Bot;
152+
module.exports = Bot;
100153
}

0 commit comments

Comments
 (0)