Compare commits

..

No commits in common. "62e61d21bf8a2b7e9296ee150ad619eeb93f7a45" and "5fd8147ff3658649498084e04441812e866d8741" have entirely different histories.

8 changed files with 134 additions and 16 deletions

View File

@ -2,6 +2,23 @@ version: "3.4"
services: services:
poll:
image: mathwave/sprint-repo:pizda-bot
command: poll
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
STAGE: "development"
networks:
- queues-development
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
worker: worker:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
command: worker command: worker
@ -20,12 +37,30 @@ services:
parallelism: 1 parallelism: 1
order: start-first order: start-first
mailbox:
image: mathwave/sprint-repo:pizda-bot
command: mailbox
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
STAGE: "development"
networks:
- queues-development
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
pizda-bot-nginx: pizda-bot-nginx:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
command: api command: api
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
MONGO_HOST: "mongo.develop.sprinthub.ru" MONGO_HOST: "mongo.develop.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "development" STAGE: "development"
networks: networks:
- common-infra-nginx-development - common-infra-nginx-development

View File

@ -2,6 +2,23 @@ version: "3.4"
services: services:
poll:
image: mathwave/sprint-repo:pizda-bot
command: poll
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
STAGE: "production"
networks:
- queues
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
worker: worker:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
command: worker command: worker
@ -21,12 +38,31 @@ services:
parallelism: 1 parallelism: 1
order: start-first order: start-first
mailbox:
image: mathwave/sprint-repo:pizda-bot
command: mailbox
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
STAGE: "production"
networks:
- queues
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
pizda-bot-nginx: pizda-bot-nginx:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
command: api command: api
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
MONGO_HOST: "mongo.sprinthub.ru" MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "production"
networks: networks:
- common-infra-nginx - common-infra-nginx
deploy: deploy:

View File

@ -39,5 +39,7 @@ jobs:
ref: dev ref: dev
- name: deploy - name: deploy
env: env:
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }} MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot-development run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot-development

View File

@ -39,5 +39,7 @@ jobs:
ref: prod ref: prod
- name: deploy - name: deploy
env: env:
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }} MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot

27
daemons/mailbox.py Normal file
View File

@ -0,0 +1,27 @@
import telebot
import os
from daemons import base
from utils import queues
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
def __init__(self):
super().__init__()
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@property
def queue_name(self):
return 'pizda_bot_mailbox'
def execute(self):
self.poll()
def process(self, payload):
body = {
'chat_id': payload['chat_id'],
'text': payload['message'],
}
if payload['action'] == 'reply':
body['reply_to_message_id'] = payload['reply_to']
self.bot.send_message(**body)

16
daemons/poll.py Normal file
View File

@ -0,0 +1,16 @@
import os
import json
import telebot
from daemons import base
from telebot import types
from utils import queues
class Daemon(base.BaseDaemon):
def execute(self):
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@bot.message_handler()
def do_action(message: types.Message):
queues.set_task('pizda_bot_worker', message.json, 1)
bot.polling()

View File

@ -49,29 +49,23 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
def reply(self, text: str, chat_id: int, message_id: int): def reply(self, text: str, chat_id: int, message_id: int):
queues.set_task( queues.set_task(
'botalka_mailbox', 'pizda_bot_mailbox',
{ {
'project': 'pizda-bot', 'action': 'reply',
'name': 'telegram-bot', 'message': text,
'body': { 'reply_to': message_id,
'text': text, 'chat_id': chat_id
'reply_to_message_id': message_id,
'chat_id': chat_id,
}
}, },
1, 1,
) )
def send(self, text: str, chat_id: int): def send(self, text: str, chat_id: int):
queues.set_task( queues.set_task(
'botalka_mailbox', 'pizda_bot_mailbox',
{ {
'project': 'pizda-bot', 'action': 'send',
'name': 'telegram-bot', 'message': text,
'body': { 'chat_id': chat_id
'text': text,
'chat_id': chat_id,
}
}, },
1, 1,
) )

View File

@ -2,9 +2,15 @@ import sys
arg = sys.argv[-1] arg = sys.argv[-1]
if arg == 'worker': if arg == 'poll':
from daemons import poll
daemon = poll.Daemon()
elif arg == 'worker':
from daemons import worker from daemons import worker
daemon = worker.Daemon() daemon = worker.Daemon()
elif arg == 'mailbox':
from daemons import mailbox
daemon = mailbox.Daemon()
else: else:
from api import app from api import app
app.run(host="0.0.0.0", port=1238) app.run(host="0.0.0.0", port=1238)