Update && add Dockerfile

This commit is contained in:
Francesco Esposito 2022-01-31 16:19:33 +01:00
parent 85c48f1fb5
commit 8e3b3ca8bf
8 changed files with 3206 additions and 701 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"]

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 +1,29 @@
const redis = require('redis'); const redis = require('redis');
const prompt = require('prompt');
const publisher = redis.createClient(); require('dotenv').config();
publisher.publish("NotificamBot", JSON.stringify({ const {
code:'generic', REDIS_CHANNEL,
description: 'event test', 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: {} options: {}
})); }));
publisher.quit(); 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;