diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 4899bb3..efb4218 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -26,6 +26,30 @@ services: parallelism: 1 order: start-first + roulette-nginx: + image: mathwave/sprint-repo:roulette-bot + command: bot + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV + MONGO_HOST: "mongo.develop.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_DEV + MINIO_HOST: "minio.develop.sprinthub.ru" + MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "development" + REDIS_HOST: "redis.develop.sprinthub.ru" + REDIS_PASSWORD: $REDIS_PASSWORD_DEV + networks: + - net + - common-infra-nginx + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + networks: net: driver: overlay diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index e0a64e2..9128f39 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -4,6 +4,29 @@ version: "3.4" services: bot: + image: mathwave/sprint-repo:roulette-bot + command: bot + networks: + - net + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD + MONGO_HOST: "mongo.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_PROD + MINIO_HOST: "minio.sprinthub.ru" + MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "production" + REDIS_HOST: "redis.sprinthub.ru" + REDIS_PASSWORD: $REDIS_PASSWORD_PROD + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + + roulette-nginx: image: mathwave/sprint-repo:roulette-bot command: bot networks: diff --git a/api.py b/api.py new file mode 100644 index 0000000..f5d7259 --- /dev/null +++ b/api.py @@ -0,0 +1,96 @@ +import io +from time import sleep + +from bson import ObjectId +from flask import Flask, Response, request, redirect, jsonify, send_file +from minio import Minio, S3Error +from telebot.apihelper import ApiTelegramException + +import settings +from api_processors import steps_dict +from mongo import mongo + + +app = Flask("roulette") + + +@app.route('/letsgonumber') +def step(): + number = request.args.get('num') + action = steps_dict[int(number)] + for chat in mongo.chats_collection.find({}): + try: + action(chat) + except ApiTelegramException as e: + print(e) + sleep(.2) + return redirect('/adminkadlyapazanov') + + +@app.route('/adminkadlyapazanov') +def index(): + return Response(''' + + + + Приветствие


+ Спокойной ночи


+ Доброе утро


+ Квиз 1


+ Время фото


+ Музло


+ Квиз 2


+ Открытки


+ Промокоды


+ + + + ''') + + +@app.route('/info') +def info(): + s = "" + for chat in mongo.chats_collection.find({}): + for key, value in chat.items(): + s += f"{key}: {value}
" + s += f"

" + return Response(s) + + +@app.route('/photo') +def photo(): + chat_id = int(request.args.get('chat_id')) + doc = mongo.chats_collection.find_one({"chat_id": chat_id}) + client = Minio( + settings.MINIO_HOST, + access_key=settings.MINIO_ACCESS_KEY, + secret_key=settings.MINIO_SECRET_KEY, + secure=False + ) + try: + data = client.get_object( + "8march-bot", + f"selfies/{chat_id}-{doc['username']}-{doc['telegram_name']}.jpg", + ).data + except S3Error: + data = open('shrek.jpeg', 'rb').read() + return send_file(io.BytesIO(data), mimetype='image/jpg') + + +@app.route('/dialogs') +def main(): + html = "" + for dialog in mongo.dialogs_collection.find({}).sort([('started_at', -1)]): + html += f'{dialog["_id"]}
' + html += "" + return html + + +@app.route('/dialog') +def dialog(): + html = "" + for message in mongo.messages_collection.find({"dialog_id": ObjectId(request.args.get('dialog_id'))}).sort([('sent_at', 1)]): + html += f'{message["sender"]}: {message["text"]}
' + html += "" + return html diff --git a/main.py b/main.py index 7ec5195..caedf8f 100644 --- a/main.py +++ b/main.py @@ -4,5 +4,8 @@ import sys if sys.argv[-1] == "bot": from bot import run_bot run_bot() +elif sys.argv[-1] == "api": + from api import app + app.run(host="0.0.0.0", port=1238) else: raise NotImplementedError diff --git a/tools/mongo.py b/tools/mongo.py index 54e388a..31df9be 100644 --- a/tools/mongo.py +++ b/tools/mongo.py @@ -23,6 +23,9 @@ class Mongo: ("chat_id_2", 1), ('finished_at', 1) ]) + self.messages_collection.create_index([ + ('dialog_id', 1) + ]) def __getitem__(self, item): return self.database.get_collection(item)