From 8af20ad7e9b01b8ff1c33182ebabfa8aa57a1255 Mon Sep 17 00:00:00 2001 From: Joan Date: Sun, 24 Mar 2024 18:17:11 +0100 Subject: [PATCH] Added socks5 proxy support --- .env.example | 7 ++++++- conjuntasbot/conjuntasbot.py | 14 ++++++++++++-- conjuntasbot/requirements.txt | 2 +- docker-compose.yml | 4 ++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 866be12..e077b7b 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,9 @@ TELEGRAM_TOKEN= ADMIN_IDS= GROUP_CHAT_ID= -SPREADSHEET_ID= \ No newline at end of file +SPREADSHEET_ID= +TELEGRAM_PROXY= +TELEGRAM_SOCKS_USER= +TELEGRAM_SOCKS_PASSWORD= +TELEGRAM_SOCKS_SERVER= +TELEGRAM_SOCKS_PORT= \ No newline at end of file diff --git a/conjuntasbot/conjuntasbot.py b/conjuntasbot/conjuntasbot.py index 2dff98d..8a33c1f 100644 --- a/conjuntasbot/conjuntasbot.py +++ b/conjuntasbot/conjuntasbot.py @@ -7,7 +7,7 @@ import gspread from oauth2client.service_account import ServiceAccountCredentials from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup -from telegram.ext import Application, CommandHandler, MessageHandler, filters, ConversationHandler, CallbackContext, CallbackQueryHandler +from telegram.ext import Application, ApplicationBuilder, CommandHandler, MessageHandler, filters, ConversationHandler, CallbackContext, CallbackQueryHandler from telegram.constants import ParseMode PRODUCT_NAME, PRODUCT_DESCRIPTION, PRICE_MEMBER, PRICE, PRODUCT_IMAGE, LIMIT, LIMIT_PER_USER, UNLIMITED, LIMITED = range(9) @@ -28,6 +28,13 @@ general_group_chat_id = os.environ.get("GENERAL_GROUP_CHAT_ID") vip_group_chat_id = os.environ.get("VIP_GROUP_CHAT_ID") bot_token = os.environ.get("TELEGRAM_TOKEN") spreadsheet_id = os.environ.get("SPREADSHEET_ID") +telegram_proxy = os.environ.get("TELEGRAM_PROXY") +if telegram_proxy: + telegram_socks_user = os.environ.get("TELEGRAM_SOCKS_USER") + telegram_socks_password = os.environ.get("TELEGRAM_SOCKS_PASSWORD") + telegram_socks_server = os.environ.get("TELEGRAM_SOCKS_SERVER") + telegram_socks_port = os.environ.get("TELEGRAM_SOCKS_PORT") + proxy_url = f"socks5://{telegram_socks_user}:{telegram_socks_password}@{telegram_socks_server}:{telegram_socks_port}" # Configura la base de datos SQLite conn = sqlite3.connect('/app/data/conjuntas.db') @@ -757,7 +764,10 @@ async def help(update: Update, context: CallbackContext): await update.message.reply_text(text=message, parse_mode=ParseMode.HTML) def main()->None: - application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(bot_token).build() + if telegram_proxy: + application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(bot_token).proxy(proxy_url).get_updates_proxy(proxy_url).build() + else: + application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(bot_token).build() conv_handler = ConversationHandler( entry_points=[CommandHandler('start_conjunta', start_conjunta), diff --git a/conjuntasbot/requirements.txt b/conjuntasbot/requirements.txt index 8b72fe5..e49e7ed 100644 --- a/conjuntasbot/requirements.txt +++ b/conjuntasbot/requirements.txt @@ -1,3 +1,3 @@ -python-telegram-bot==20.4 +python-telegram-bot[socks]==21.0.1 gspread==5.12.0 oauth2client==4.1.3 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0dae5e4..93207dc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,5 +15,9 @@ services: - GENERAL_GROUP_CHAT_ID=${GENERAL_GROUP_CHAT_ID} - VIP_GROUP_CHAT_ID=${VIP_GROUP_CHAT_ID} - SPREADSHEET_ID=${SPREADSHEET_ID} + - TELEGRAM_SOCKS_USER=${TELEGRAM_SOCKS_USER} + - TELEGRAM_SOCKS_PASSWORD=${TELEGRAM_SOCKS_PASSWORD} + - TELEGRAM_SOCKS_SERVER=${TELEGRAM_SOCKS_SERVER} + - TELEGRAM_SOCKS_PORT=${TELEGRAM_SOCKS_PORT} dns: - 8.8.8.8 \ No newline at end of file