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

3794
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@
"main": "server.js",
"scripts": {
"start": "nodemon src/server.js",
"serve": "node src/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
@ -25,6 +26,7 @@
},
"devDependencies": {
"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 prompt = require('prompt');
const publisher = redis.createClient();
require('dotenv').config();
publisher.publish("NotificamBot", JSON.stringify({
code:'generic',
description: 'event test',
options: {}
}));
const {
REDIS_CHANNEL,
REDIS_PASSWORD
} = process.env;
const publisher = redis.createClient({
url: `redis://:${REDIS_PASSWORD}@localhost`
});
publisher.quit();
prompt.start();
prompt.get(['type', 'description'], (err, result) => {
publisher.publish(REDIS_CHANNEL, JSON.stringify({
type: result.type ,
description: result.description,
options: {}
}));
publisher.quit();
})

View File

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

View File

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