-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit af5f14e
Showing
20 changed files
with
4,016 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
RABBITMQ_URL=amqp://user:password@host | ||
BOT_TOKEN=bot_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.