From c1f256be35cddd3fce4d441791a582c400d685e3 Mon Sep 17 00:00:00 2001 From: Gabriel Wong Date: Mon, 19 Jul 2021 13:06:08 +0800 Subject: [PATCH] Reorganize files in Telegram messaging API and bot Move files to `src` directory, separating project dependencies and files from source code --- .gitignore | 2 ++ apis/telemsg/requirements.txt | 1 + apis/telemsg/run.sh | 6 +++++- apis/telemsg/{ => src}/app.py | 4 ++-- apis/telemsg/{ => src}/bot.py | 8 ++++---- hosts/telebot/bot/config.py | 8 -------- hosts/telebot/requirements.txt | 1 + hosts/telebot/{ => src}/bot/__init__.py | 0 hosts/telebot/{ => src}/bot/bot.py | 0 hosts/telebot/src/bot/config.py | 10 ++++++++++ hosts/telebot/{ => src}/bot/handlers/__init__.py | 0 hosts/telebot/{ => src}/bot/handlers/commands.py | 0 hosts/telebot/{ => src}/bot/handlers/conv/__init__.py | 0 .../{ => src}/bot/handlers/conv/add_subscription.py | 0 hosts/telebot/{ => src}/bot/handlers/conv/feedback.py | 4 ++-- .../{ => src}/bot/handlers/conv/view_subscription.py | 0 hosts/telebot/{ => src}/bot/handlers/logging.py | 0 hosts/telebot/{ => src}/main.py | 8 +++++--- hosts/telebot/{ => src}/message.py | 7 ++++--- 19 files changed, 36 insertions(+), 23 deletions(-) rename apis/telemsg/{ => src}/app.py (91%) rename apis/telemsg/{ => src}/bot.py (72%) delete mode 100644 hosts/telebot/bot/config.py rename hosts/telebot/{ => src}/bot/__init__.py (100%) rename hosts/telebot/{ => src}/bot/bot.py (100%) create mode 100644 hosts/telebot/src/bot/config.py rename hosts/telebot/{ => src}/bot/handlers/__init__.py (100%) rename hosts/telebot/{ => src}/bot/handlers/commands.py (100%) rename hosts/telebot/{ => src}/bot/handlers/conv/__init__.py (100%) rename hosts/telebot/{ => src}/bot/handlers/conv/add_subscription.py (100%) rename hosts/telebot/{ => src}/bot/handlers/conv/feedback.py (93%) rename hosts/telebot/{ => src}/bot/handlers/conv/view_subscription.py (100%) rename hosts/telebot/{ => src}/bot/handlers/logging.py (100%) rename hosts/telebot/{ => src}/main.py (55%) rename hosts/telebot/{ => src}/message.py (67%) diff --git a/.gitignore b/.gitignore index b4166c6..9ec654b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ venv/ data/ config/ + +.env diff --git a/apis/telemsg/requirements.txt b/apis/telemsg/requirements.txt index 612153b..c01e056 100644 --- a/apis/telemsg/requirements.txt +++ b/apis/telemsg/requirements.txt @@ -1,2 +1,3 @@ flask +gunicorn python-telegram-bot>=12.0.0b1 diff --git a/apis/telemsg/run.sh b/apis/telemsg/run.sh index ade913b..b648cf5 100755 --- a/apis/telemsg/run.sh +++ b/apis/telemsg/run.sh @@ -1,2 +1,6 @@ #!/bin/bash -gunicorn -b 127.0.0.1:5001 app:app +set -a +source .env +set +a + +gunicorn -b 127.0.0.1:5001 --chdir src app:app diff --git a/apis/telemsg/app.py b/apis/telemsg/src/app.py similarity index 91% rename from apis/telemsg/app.py rename to apis/telemsg/src/app.py index c405d7c..f86255c 100644 --- a/apis/telemsg/app.py +++ b/apis/telemsg/src/app.py @@ -1,10 +1,10 @@ +import os from flask import Flask, request from operator import itemgetter from bot import send_message app = Flask(__name__) -with open('admin.txt', 'r') as f: - ADMIN_ID = int(f.read().strip()) +ADMIN_ID = os.environ.get('TELEGRAM_ADMIN_ID') @app.route('/message', methods=['POST']) diff --git a/apis/telemsg/bot.py b/apis/telemsg/src/bot.py similarity index 72% rename from apis/telemsg/bot.py rename to apis/telemsg/src/bot.py index f8347ac..e7e120c 100644 --- a/apis/telemsg/bot.py +++ b/apis/telemsg/src/bot.py @@ -1,13 +1,13 @@ +import os from telegram.ext import Updater from telegram.error import BadRequest, Unauthorized -with open('token.txt', 'r') as f: - token = f.read().strip() +TOKEN = os.environ.get('TELEGRAM_TOKEN') +updater = Updater(token=TOKEN) +bot = updater.dispatcher.bot def send_message(chat_id: int, text: str): - updater = Updater(token=token) - bot = updater.dispatcher.bot try: bot.send_message(chat_id=chat_id, text=text) return True diff --git a/hosts/telebot/bot/config.py b/hosts/telebot/bot/config.py deleted file mode 100644 index 2d224a4..0000000 --- a/hosts/telebot/bot/config.py +++ /dev/null @@ -1,8 +0,0 @@ -WEB_API_HOST = 'localhost' -WEB_API_PORT = 5000 - -DB_API_HOST = 'localhost' -DB_API_PORT = 8001 - -TELE_API_HOST = 'localhost' -TELE_API_PORT = 5001 diff --git a/hosts/telebot/requirements.txt b/hosts/telebot/requirements.txt index 8cde8ed..f92c0b6 100644 --- a/hosts/telebot/requirements.txt +++ b/hosts/telebot/requirements.txt @@ -1 +1,2 @@ python-telegram-bot>=12.0.0b1 +requests diff --git a/hosts/telebot/bot/__init__.py b/hosts/telebot/src/bot/__init__.py similarity index 100% rename from hosts/telebot/bot/__init__.py rename to hosts/telebot/src/bot/__init__.py diff --git a/hosts/telebot/bot/bot.py b/hosts/telebot/src/bot/bot.py similarity index 100% rename from hosts/telebot/bot/bot.py rename to hosts/telebot/src/bot/bot.py diff --git a/hosts/telebot/src/bot/config.py b/hosts/telebot/src/bot/config.py new file mode 100644 index 0000000..54488cd --- /dev/null +++ b/hosts/telebot/src/bot/config.py @@ -0,0 +1,10 @@ +import os + +WEB_API_HOST = os.environ.get('WEB_API_HOST', 'localhost') +WEB_API_PORT = os.environ.get('WEB_API_PORT', 5000) + +DB_API_HOST = os.environ.get('DB_API_HOST', 'localhost') +DB_API_PORT = os.environ.get('DB_API_PORT', 8001) + +TELEMSG_API_HOST = os.environ.get('TELEMSG_API_HOST', 'localhost') +TELEMSG_API_PORT = os.environ.get('TELEMSG_API_PORT', 5001) diff --git a/hosts/telebot/bot/handlers/__init__.py b/hosts/telebot/src/bot/handlers/__init__.py similarity index 100% rename from hosts/telebot/bot/handlers/__init__.py rename to hosts/telebot/src/bot/handlers/__init__.py diff --git a/hosts/telebot/bot/handlers/commands.py b/hosts/telebot/src/bot/handlers/commands.py similarity index 100% rename from hosts/telebot/bot/handlers/commands.py rename to hosts/telebot/src/bot/handlers/commands.py diff --git a/hosts/telebot/bot/handlers/conv/__init__.py b/hosts/telebot/src/bot/handlers/conv/__init__.py similarity index 100% rename from hosts/telebot/bot/handlers/conv/__init__.py rename to hosts/telebot/src/bot/handlers/conv/__init__.py diff --git a/hosts/telebot/bot/handlers/conv/add_subscription.py b/hosts/telebot/src/bot/handlers/conv/add_subscription.py similarity index 100% rename from hosts/telebot/bot/handlers/conv/add_subscription.py rename to hosts/telebot/src/bot/handlers/conv/add_subscription.py diff --git a/hosts/telebot/bot/handlers/conv/feedback.py b/hosts/telebot/src/bot/handlers/conv/feedback.py similarity index 93% rename from hosts/telebot/bot/handlers/conv/feedback.py rename to hosts/telebot/src/bot/handlers/conv/feedback.py index 8a0156b..c1a898f 100644 --- a/hosts/telebot/bot/handlers/conv/feedback.py +++ b/hosts/telebot/src/bot/handlers/conv/feedback.py @@ -3,7 +3,7 @@ from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, Filters from enum import Enum -from ...config import TELE_API_HOST, TELE_API_PORT +from ...config import TELEMSG_API_HOST, TELEMSG_API_PORT from ..logging import logger, log_command_in_db COMMAND_NAME = 'feedback' @@ -44,7 +44,7 @@ def cancel(update, context): def message_admin(chat_id, text): - url = f'http://{TELE_API_HOST}:{TELE_API_PORT}/message/admin' + url = f'http://{TELEMSG_API_HOST}:{TELEMSG_API_PORT}/message/admin' headers = {'Content-Type': 'application/json'} data = json.dumps({'chat_id': chat_id, 'text': text}) req = requests.post(url, headers=headers, data=data) diff --git a/hosts/telebot/bot/handlers/conv/view_subscription.py b/hosts/telebot/src/bot/handlers/conv/view_subscription.py similarity index 100% rename from hosts/telebot/bot/handlers/conv/view_subscription.py rename to hosts/telebot/src/bot/handlers/conv/view_subscription.py diff --git a/hosts/telebot/bot/handlers/logging.py b/hosts/telebot/src/bot/handlers/logging.py similarity index 100% rename from hosts/telebot/bot/handlers/logging.py rename to hosts/telebot/src/bot/handlers/logging.py diff --git a/hosts/telebot/main.py b/hosts/telebot/src/main.py similarity index 55% rename from hosts/telebot/main.py rename to hosts/telebot/src/main.py index 78509a2..d8b3453 100644 --- a/hosts/telebot/main.py +++ b/hosts/telebot/src/main.py @@ -1,10 +1,12 @@ +import os from bot import Bot +TOKEN = os.environ.get('TELEGRAM_TOKEN') + + def main(): - with open('token.txt', 'r') as f: - token = f.read().strip() - bot = Bot(token) + bot = Bot(TOKEN) bot.add_handlers() bot.start() diff --git a/hosts/telebot/message.py b/hosts/telebot/src/message.py similarity index 67% rename from hosts/telebot/message.py rename to hosts/telebot/src/message.py index 0b3e09c..a4eaa9f 100644 --- a/hosts/telebot/message.py +++ b/hosts/telebot/src/message.py @@ -1,10 +1,11 @@ +import os from bot import Bot +TOKEN = os.environ.get('TELEGRAM_TOKEN') + def main(): - with open('token.txt', 'r') as f: - token = f.read().strip() - bot = Bot(token) + bot = Bot(TOKEN) chat_id = int(input('Enter chat ID: ')) text = input('Enter message: ')