Initial version

This commit is contained in:
Francesco Esposito 2020-06-21 17:24:53 +02:00
parent 48dbe93f1c
commit a4f763799a
8 changed files with 2123 additions and 1 deletions

3
.env.sample Normal file
View File

@ -0,0 +1,3 @@
BOT_TOKEN=insert-bot-token
CHAT_ID=insert-chat-id
REDIS_CHANNEL=NotificamBot

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
.env

View File

@ -1 +1,22 @@
# NotificamBot # NotificamBot
Telegram bot to reiceve notification.
Notificambot reads notifications from a queue (pub/sub on redis).
## Configuration
1. Create telegram bot with **@botFater**.
2. Clone this repository.
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 events into `src/template.js`.
## Test
You can perform a test with command: `node pub-redis-example.js`

1972
package-lock.json generated Executable file

File diff suppressed because it is too large Load Diff

30
package.json Executable file
View File

@ -0,0 +1,30 @@
{
"name": "NotificamBot",
"version": "0.1.0",
"description": "",
"private": "true",
"main": "server.js",
"scripts": {
"start": "nodemon src/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/franjsco/ricordami-bot.git"
},
"author": "franjsco (Francesco Esposito)",
"license": "ISC",
"bugs": {
"url": "https://github.com/franjsco/ricordami-bot/issues"
},
"homepage": "https://github.com/franjsco/ricordami-bot#readme",
"dependencies": {
"dotenv": "^8.2.0",
"redis": "^3.0.2",
"telegraf": "^3.38.0"
},
"devDependencies": {
"eslint": "^7.1.0",
"nodemon": "^2.0.4"
}
}

12
pub-redis-example.js Normal file
View File

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

43
src/server.js Executable file
View File

@ -0,0 +1,43 @@
const { Telegraf } = require('telegraf');
const redis = require('redis');
const templates = require('./templates');
require('dotenv').config();
const {
BOT_TOKEN,
CHAT_ID,
REDIS_CHANNEL
} = process.env;
const bot = new Telegraf(BOT_TOKEN);
const subscriber = redis.createClient();
bot.start((ctx) => {
console.log(`Chat ID: ${ctx.chat.id}`);
console.log(`Configured CHAT_ID: ${CHAT_ID}`);
if (ctx.chat.id == CHAT_ID) {
ctx.reply('Welcome to NotificamBot');
}
});
subscriber.on("subscribe", (channel, message) => {
console.log(`Subscribe on ${channel}`);
});
subscriber.on('message', (channel, message) => {
console.log('Messaggio: ' + message);
bot.telegram.sendMessage(CHAT_ID, templates.handler(message));
});
subscriber.subscribe(REDIS_CHANNEL);
bot.launch();

39
src/templates.js Normal file
View File

@ -0,0 +1,39 @@
const genericTemplate = (msg) => {
value = `====
Code: ${msg.code}
Description: ${msg.description}`;
return value;
}
const handler = (msg) => {
let msgParsed;
let value;
try {
msgParsed = JSON.parse(msg);
} catch (e) {
console.error('Error parse');
return 'Error';
}
switch (msgParsed.code) {
case 'generic':
value = genericTemplate(msgParsed);
break;
default:
value = {}
}
return value;
}
const template = {
genericTemplate,
handler
}
module.exports = template;