Skip to content

Commit

Permalink
Reorganize files in Telegram messaging API and bot
Browse files Browse the repository at this point in the history
Move files to `src` directory, separating project dependencies and files
from source code
  • Loading branch information
gabrielwong159 committed Jul 19, 2021
1 parent 7efd785 commit c1f256b
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ venv/

data/
config/

.env
1 change: 1 addition & 0 deletions apis/telemsg/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flask
gunicorn
python-telegram-bot>=12.0.0b1
6 changes: 5 additions & 1 deletion apis/telemsg/run.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions apis/telemsg/app.py → apis/telemsg/src/app.py
Original file line number Diff line number Diff line change
@@ -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'])
Expand Down
8 changes: 4 additions & 4 deletions apis/telemsg/bot.py → apis/telemsg/src/bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 0 additions & 8 deletions hosts/telebot/bot/config.py

This file was deleted.

1 change: 1 addition & 0 deletions hosts/telebot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
python-telegram-bot>=12.0.0b1
requests
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions hosts/telebot/src/bot/config.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions hosts/telebot/main.py → hosts/telebot/src/main.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
7 changes: 4 additions & 3 deletions hosts/telebot/message.py → hosts/telebot/src/message.py
Original file line number Diff line number Diff line change
@@ -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: ')
Expand Down

0 comments on commit c1f256b

Please sign in to comment.