Compare commits

...

3 Commits

Author SHA1 Message Date
Francesco Esposito 1fa4231e09 edit readme 2022-01-31 16:45:07 +01:00
Francesco Esposito 8ac79a06a8 Edit readme 2022-01-31 16:42:21 +01:00
Francesco Esposito 8e3b3ca8bf Update && add Dockerfile 2022-01-31 16:19:33 +01:00
10 changed files with 3246 additions and 718 deletions

View File

@ -1,3 +1,5 @@
BOT_TOKEN=insert-bot-token BOT_TOKEN=insert-bot-token
CHAT_ID=insert-chat-id CHAT_ID=insert-chat-id
REDIS_CHANNEL=NotificamBot REDIS_CHANNEL=NotificamBot
REDIS_HOST=redis
REDIS_PASSWORD=your-password

13
Dockerfile Normal file
View File

@ -0,0 +1,13 @@
FROM alpine:3.15
WORKDIR /app
RUN apk add --update --no-cache nodejs=~16 npm=~8.1
COPY . /app/
ENV NODE_ENV production
RUN npm install --only=prod
CMD ["npm", "run", "serve"]

View File

@ -1,22 +1,45 @@
# NotificamBot # NotificamBot
Telegram bot to reiceve notifications. Telegram bot to receive notifications reads from a queue (pub/sub on Redis).
Notificambot reads notifications from a queue (pub/sub on redis).
## Configuration ## Configuration
1. Create telegram bot with **@BotFather**.
2. Clone this repository. Rename .env.sample in .env and configure it:
3. Start a redis instance on localhost.
4. Install dependencies with `npm install`.
5. Configure env file (and rename from `env.sampe` to `.env`).
6. Launch with `node src/server.js`
You can configure templates into `src/templates.js`. | KEY |DESCRIPTION |
|-- | --|
| BOT_TOKEN | Telegram Bot Token created with @BotFather |
| CHAT_ID | Your chat id with the bot (you can configure this after the first launch of the bot from client) |
| REDIS_CHANELL | Channel where the bot reads|
| REDIS_HOST | Host redis (don't touch if you use docker-compose).
| REDIS_PASSWORD| Redis password |
## Test
You can perform a test with command: `node pub-redis-example.js`
## Usage
1. Clone the project with
```sh
git clone https://github.com/franjsco/NotificamBot.git
```
2. Build docker image and run it with docker-compose:
```sh
docker-compose up --build
```
3. Search your bot on telegram and start it. The chat id will be displayed on the terminal.
Copy it into .env and stop (CTRL+C) and start containers.
```sh
docker-compose up
```
4. Now you can perform a test.
```sh
node test-publish.js
```
5. You can send notifications to the bot by connecting to the container redis instance from any application and post messages to the configured queue.

22
docker-compose.yml Normal file
View File

@ -0,0 +1,22 @@
version: '3.3'
services:
bot:
container_name: notificam-bot-server
build: .
image: 'notificam-bot'
restart: always
env_file: .env
depends_on:
- redis
redis:
container_name: notificam-bot-redis
image: redis
restart: always
ports:
- 6379:6379
env_file: .env
command: >
--requirepass ${REDIS_PASSWORD}

3790
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"start": "nodemon src/server.js", "start": "nodemon src/server.js",
"serve": "node src/server.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
@ -25,6 +26,7 @@
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.1.0", "eslint": "^7.1.0",
"nodemon": "^2.0.4" "nodemon": "^2.0.4",
"prompt": "^1.2.1"
} }
} }

View File

@ -1,12 +0,0 @@
const redis = require('redis');
const publisher = redis.createClient();
publisher.publish("NotificamBot", JSON.stringify({
code:'generic',
description: 'event test',
options: {}
}));
publisher.quit();

View File

@ -8,30 +8,45 @@ require('dotenv').config();
const { const {
BOT_TOKEN, BOT_TOKEN,
CHAT_ID, CHAT_ID,
REDIS_CHANNEL REDIS_CHANNEL,
REDIS_HOST,
REDIS_PASSWORD
} = process.env; } = process.env;
const bot = new Telegraf(BOT_TOKEN); const bot = new Telegraf(BOT_TOKEN);
const subscriber = redis.createClient(); const subscriber = redis.createClient({
url: `redis://:${REDIS_PASSWORD}@${REDIS_HOST}`
});
bot.start((ctx) => { bot.start((ctx) => {
console.log(`Chat ID: ${ctx.chat.id}`); chatId = ctx.chat.id;
console.log(`Configured CHAT_ID: ${CHAT_ID}`);
if (ctx.chat.id == CHAT_ID) { console.log(`NotificamBot started from ${chatId}`)
ctx.reply('Welcome to NotificamBot');
if (chatId != CHAT_ID) {
console.log(`
Please configure this chatId into .env and reboot
Chat ID: ${ctx.chat.id}`
);
return;
} }
console.log(`${ctx.chat.id} connected`)
ctx.reply('Welcome');
}); });
subscriber.on("subscribe", (channel, message) => { subscriber.on("subscribe", (channel, message) => {
console.log(`Subscribe on ${channel}`); console.log(`Subscribed on ${channel}`);
}); });
subscriber.on('message', (channel, message) => { subscriber.on('message', (channel, message) => {
console.log('Messaggio: ' + message); console.log('Message: ' + message);
bot.telegram.sendMessage(CHAT_ID, templates.handler(message)); bot.telegram.sendMessage(CHAT_ID, templates.handler(message));

View File

@ -1,8 +1,8 @@
const genericTemplate = (msg) => { const genericTemplate = (msg) => {
value = `==== value = `---
Code: ${msg.code} type: ${msg.type}
Description: ${msg.description}`; description: ${msg.description}`;
return value; return value;
} }
@ -22,7 +22,7 @@ const handler = (msg) => {
return 'Error'; return 'Error';
} }
switch (msgParsed.code) { switch (msgParsed.type) {
case 'generic': case 'generic':
value = genericTemplate(msgParsed); value = genericTemplate(msgParsed);
break; break;

29
test-publish.js Normal file
View File

@ -0,0 +1,29 @@
const redis = require('redis');
const prompt = require('prompt');
require('dotenv').config();
const {
REDIS_CHANNEL,
REDIS_PASSWORD
} = process.env;
const publisher = redis.createClient({
url: `redis://:${REDIS_PASSWORD}@localhost`
});
prompt.start();
prompt.get(['type', 'description'], (err, result) => {
publisher.publish(REDIS_CHANNEL, JSON.stringify({
type: result.type ,
description: result.description,
options: {}
}));
publisher.quit();
})