Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vitornogueira committed Aug 6, 2019
0 parents commit af5f14e
Show file tree
Hide file tree
Showing 20 changed files with 4,016 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RABBITMQ_URL=amqp://user:password@host
BOT_TOKEN=bot_token
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "airbnb",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
node_modules/
*.log*
coverage
*.swp
data/*
!data/.gitkeep
.env
!.env.example
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:dubnium-alpine

WORKDIR /app

RUN apk update && apk add --no-cache bash

COPY package.json /app
COPY package-lock.json /app

RUN npm install

COPY . /app

CMD ["npm", "start"]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Klick Telegram Service

## Dependencies to run in development environment

- docker
- docker-compose

## Running service

Copy `.env.example` to `.env` and update the environment variables.
Run `docker-compose up -d` to build and run service.
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const consumers = require('./app/consumers');
const bot = require('./app/services/bot');

consumers.load();
bot.launch();
9 changes: 9 additions & 0 deletions app/broker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Broker = require('roger-rabbit');

module.exports = Broker({
host: process.env.RABBITMQ_URL,
exchange: {
type: 'direct',
name: 'klick.account',
},
});
12 changes: 12 additions & 0 deletions app/business/send_message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const UserRepository = require('../repositories/user');
const bot = require('../services/bot');

module.exports = ({ identifier, title, content }) => {
const model = new UserRepository();
const user = model.find(identifier);

return bot.sendMessage({
chatId: user.telegramId,
message: `${title}\n${content}`,
});
};
13 changes: 13 additions & 0 deletions app/business/user_register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const UserRepository = require('../repositories/user');
const bot = require('../services/bot');

module.exports = (data) => {
const model = new UserRepository();

model.create(data);

return bot.sendMessage({
chatId: data.telegramId,
message: 'Hey! You have been registered!',
});
};
14 changes: 14 additions & 0 deletions app/consumers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');
const path = require('path');

module.exports = {
load() {
const consumerFileRegex = /_consumer.js$/;

fs.readdirSync(path.join(__dirname))
.filter(consumerPath => consumerFileRegex.test(consumerPath))
/* eslint-disable global-require, import/no-dynamic-require */
.map(consumerPath => require(`./${consumerPath}`));
/* eslint-enable global-require, import/no-dynamic-require */
},
};
13 changes: 13 additions & 0 deletions app/consumers/telegram_notification_send_consumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const broker = require('../broker');
const sendMessage = require('../business/send_message');

const queue = {
name: 'telegram.notification.send',
options: {
durable: true,
},
};

const routingKey = 'notification.send';

broker.consume({ queue, routingKey }, data => sendMessage(data));
13 changes: 13 additions & 0 deletions app/consumers/telegram_user_sync_consumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const broker = require('../broker');
const userRegister = require('../business/user_register');

const queue = {
name: 'telegram.user.sync',
options: {
durable: true,
},
};

const routingKey = 'user.sync';

broker.consume({ queue, routingKey }, user => userRegister(user));
10 changes: 10 additions & 0 deletions app/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');

const adapter = new FileSync('./data/db.json');
const db = low(adapter);

db.defaults({ users: [] })
.write();

module.exports = db;
25 changes: 25 additions & 0 deletions app/repositories/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const db = require('../db');

module.exports = class UserRepository {
constructor() {
this.table = db.get('users');
}

create({ identifier, telegramId }) {
const user = this.find(identifier);

if (user) {
return user;
}

this.table
.push({ identifier, telegramId })
.write();

return this.find(telegramId);
}

find(identifier) {
return this.table.find({ identifier }).value();
}
};
31 changes: 31 additions & 0 deletions app/services/bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Telegraf = require('telegraf');
const Telegram = require('telegraf/telegram');
const broker = require('../broker');

const bot = new Telegraf(process.env.BOT_TOKEN);
const telegram = new Telegram(process.env.BOT_TOKEN);

module.exports = {
launch() {
bot.start((ctx) => {
ctx.reply('Hey! Register your email using /register [email protected]');
});

bot.command('register', (ctx) => {
const { from, text } = ctx.message;
const telegramId = from.id;
const name = `${from.first_name} ${from.last_name}`;
const email = text.split(' ')[1];

if (email) {
broker.publish('user.register', { email, name, telegramId });
}
});


bot.launch();
},
sendMessage({ chatId, message }) {
telegram.sendMessage(chatId, message, { parse_mode: 'Markdown' });
},
};
Empty file added data/.gitkeep
Empty file.
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'
services:
telegram-service.local:
tty: true
stdin_open: true
container_name: telegram-service
build:
context: .
env_file:
- ./.env
volumes:
- .:/app
Loading

0 comments on commit af5f14e

Please sign in to comment.