switch to module structure
Marco Andronaco andronacomarco@gmail.com
Sat, 26 Aug 2023 11:13:31 +0200
10 files changed,
45 insertions(+),
38 deletions(-)
M
Forwarder.py
→
escarbot/forward.py
@@ -1,5 +1,5 @@
from telegram import Update -from Config import CHANNEL_ID, GROUP_ID, ADMIN_ID +from escarbot.config import CHANNEL_ID, GROUP_ID, ADMIN_ID import logging logger = logging.getLogger(__name__)
M
README.md
→
README.md
@@ -1,6 +1,6 @@
# EscarBot -Earthbound Café's custom delivery bot with other cool utilities built-in. +[Earthbound Café](https://linktr.ee/earthboundcafe)'s custom delivery bot with other cool utilities built-in. ## Features 1. The bot's main feature is listening for posts in a channel and forwarding them to a group.@@ -8,19 +8,22 @@ 2. If the bot receives a private message, it will be forwarded to the bot's admin.
3. If the bot senses an Instagram, Twitter (X) or YouTube link, it will try to send a custom link with better thumbnail generation. ## How to use - Copy the `.env.example` file into `.env` and insert the following info: * `token`: your Telegram Bot Token; * `channel_id`: the channel the bot will listen to; * `group_id`: the group that will receive the channel messages; * `admin_id`: the user that will receive the bot's private messages. +### With poetry +1. Install dependencies: `poetry install`; +2. Start the bot: `poetry run python escarbot`. + +### With virtualenv Create a new virtual environment and install required packages: ``` -python -m venv venv +python -m virtualenv venv source venv/bin/activate pip install -r requirements.txt ``` -Then, start the bot by running: -```python main.py``` +Then, start the bot: `python main.py`.
M
Replacer.py
→
escarbot/replace.py
@@ -1,6 +1,6 @@
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram.constants import ParseMode -from Config import INLINE_SEP, FEEDBACK_TIMEOUT +from escarbot.config import INLINE_SEP, FEEDBACK_TIMEOUT import logging, re, json from asyncio import sleep logger = logging.getLogger(__name__)
A
escarbot/__main__.py
@@ -0,0 +1,13 @@
+from telegram import Update +from telegram.ext import MessageHandler, ApplicationBuilder, CallbackQueryHandler, filters +from escarbot.config import TOKEN +from escarbot.inline import inline_handler +from escarbot.forward import forward, admin_forward +from escarbot.replace import replace + +application = ApplicationBuilder().token(TOKEN).build() +application.add_handler(CallbackQueryHandler(callback=inline_handler)) +application.add_handler(MessageHandler(filters.ChatType.CHANNEL, forward)) +application.add_handler(MessageHandler(filters.ChatType.PRIVATE, admin_forward)) +application.add_handler(MessageHandler(filters.ChatType.GROUPS, replace)) +application.run_polling(allowed_updates=[Update.CHANNEL_POST, Update.MESSAGE, Update.CALLBACK_QUERY])
A
escarbot/inline.py
@@ -0,0 +1,19 @@
+from telegram import Update +from telegram.ext import ContextTypes +from escarbot.config import INLINE_SEP +from escarbot.replace import feedback + +inline_mapping = [ ("feedback", feedback) ] + +async def inline_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + query = update.callback_query + data = query.data + + for t in inline_mapping: + if data.startswith(t[0]): + actual_data = data.split(INLINE_SEP, maxsplit=1)[1] + await t[1](update, context, actual_data) + return + + await query.answer("Questo tasto non fa nulla.") + return
D
main.py
@@ -1,28 +0,0 @@
-from telegram.ext import ContextTypes, MessageHandler, ApplicationBuilder, CallbackQueryHandler, filters -from telegram import Update -from Config import TOKEN, INLINE_SEP -from Replacer import replace, feedback -from Forwarder import forward, admin_forward - -inline_mapping = [ ("feedback", feedback) ] - -async def inline_keyboard(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - query = update.callback_query - data = query.data - - for t in inline_mapping: - if data.startswith(t[0]): - actual_data = data.split(INLINE_SEP, maxsplit=1)[1] - await t[1](update, context, actual_data) - return - - await query.answer("Questo tasto non fa nulla.") - return - -if __name__ == "__main__": - application = ApplicationBuilder().token(TOKEN).build() - application.add_handler(CallbackQueryHandler(callback=inline_keyboard)) - application.add_handler(MessageHandler(filters.ChatType.CHANNEL, forward)) - application.add_handler(MessageHandler(filters.ChatType.PRIVATE, admin_forward)) - application.add_handler(MessageHandler(filters.ChatType.GROUPS, replace)) - application.run_polling(allowed_updates=[Update.CHANNEL_POST, Update.MESSAGE, Update.CALLBACK_QUERY])
M
pyproject.toml
→
pyproject.toml
@@ -1,10 +1,10 @@
[tool.poetry] -name = "gif-escarbot" +name = "escarbot" version = "0.1.0" -description = "A gif-forwarding robot for Earthbound Café (https://linktr.ee/earthboundcafe)" +description = "Earthbound Café's custom delivery bot with other cool utilities built-in." authors = ["Andronaco Marco <marco.andronaco@olivetti.com>"] readme = "README.md" -packages = [{include = "gif_escarbot"}] +packages = [{include = "escarbot"}] [tool.poetry.dependencies] python = "^3.11"