NotificamBot/src/server.js

58 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-06-21 17:24:53 +02:00
const { Telegraf } = require('telegraf');
const redis = require('redis');
const templates = require('./templates');
require('dotenv').config();
const {
BOT_TOKEN,
CHAT_ID,
2022-01-31 16:19:33 +01:00
REDIS_CHANNEL,
REDIS_HOST,
REDIS_PASSWORD
2020-06-21 17:24:53 +02:00
} = process.env;
const bot = new Telegraf(BOT_TOKEN);
2022-01-31 16:19:33 +01:00
const subscriber = redis.createClient({
url: `redis://:${REDIS_PASSWORD}@${REDIS_HOST}`
});
2020-06-21 17:24:53 +02:00
bot.start((ctx) => {
2022-01-31 16:19:33 +01:00
chatId = ctx.chat.id;
console.log(`NotificamBot started from ${chatId}`)
if (chatId != CHAT_ID) {
console.log(`
Please configure this chatId into .env and reboot
2020-06-21 17:24:53 +02:00
2022-01-31 16:19:33 +01:00
Chat ID: ${ctx.chat.id}`
);
return;
2020-06-21 17:24:53 +02:00
}
2022-01-31 16:19:33 +01:00
console.log(`${ctx.chat.id} connected`)
ctx.reply('Welcome');
2020-06-21 17:24:53 +02:00
});
subscriber.on("subscribe", (channel, message) => {
2022-01-31 16:19:33 +01:00
console.log(`Subscribed on ${channel}`);
2020-06-21 17:24:53 +02:00
});
subscriber.on('message', (channel, message) => {
2022-01-31 16:19:33 +01:00
console.log('Message: ' + message);
2020-06-21 17:24:53 +02:00
bot.telegram.sendMessage(CHAT_ID, templates.handler(message));
});
subscriber.subscribe(REDIS_CHANNEL);
bot.launch();