From a1842aec7707e6e1f193ac3bab09b614ad76d5c3 Mon Sep 17 00:00:00 2001 From: Naidis Date: Thu, 4 Jan 2024 16:39:29 +0800 Subject: [PATCH] Added ://: to Telegram proxy support (#18) * fixing some bugs of proxy support in bot.py and readme * fixed readme. * Added ://: to Telegram proxy support. Updated documentation. Fixed several bugs. Messed a bit with code style. --------- Co-authored-by: dmcallejo --- README.md | 29 +++++++++++++++-------------- bot.py | 26 +++++++++++++++++++------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c23f695..51cdcb2 100755 --- a/README.md +++ b/README.md @@ -8,27 +8,28 @@ Control your ASF instance anywhere. ## Usage: - Executable: (python3) bot.py - Arguments: - - ```--token``` : Telegram API token given by @botfather (**mandatory**). - - ```--alias``` : Telegram alias of the bot owner. Only this user can send commands to ASF. (**mandatory**). - - ```--host``` : ASF IPC host (defaults to ```127.0.0.1```) - - ```--port``` : ASF IPC listening port (defaults to ```1242```) - - ```--password``` : ASF IPC password (if you have set one) - - ```--verbosity```: Log verbosity (DEBUG, INFO, WARN, ERROR) + - `--token` : Telegram API token given by @botfather (**mandatory**). + - `--alias` : Telegram alias of the bot owner. Only this user can send commands to ASF. (**mandatory**). + - `--proxy` : Telegram Proxy (if you have one. Input format: `://:`. For example: `http://192.168.1.1:7890`) + - `--host` : ASF IPC host (defaults to `127.0.0.1`) + - `--port` : ASF IPC listening port (defaults to `1242`) + - `--password` : ASF IPC password (if you have set one) + - `--verbosity`: Log verbosity (DEBUG, INFO, WARN, ERROR) You can also use **environment variables** to configure the bot. Environment variables would override any command argument set. The naming is pretty self-explanatory: - - ```TELEGRAM_BOT_TOKEN``` - - ```TELEGRAM_USER_ALIAS``` - - ```TELEGRAM_PROXY``` - - ```ASF_IPC_HOST``` - - ```ASF_IPC_PORT``` - - ```ASF_IPC_PASSWORD``` + - `TELEGRAM_BOT_TOKEN` + - `TELEGRAM_USER_ALIAS` + - `TELEGRAM_PROXY` + - `ASF_IPC_HOST` + - `ASF_IPC_PORT` + - `ASF_IPC_PASSWORD` Once the bot has started and verified the connection to the ASF instance, you can send commands through your telegram bot using standard ASF notation (i.e.: ```!status asf```) or Telegram notation (i.e.: ```/status asf```). The bot also reads messages containing Steam cd-keys. It will automatically parse every key and activate them on your accounts with ```!redeem asf {{parsed_cdkey}}``` notifying you the process. ## Quickstart (with docker) 1. Create a bot via [@botfather](t.me/BotFather). -2. Copy and fill the ```docker-compose.yml``` example below. +2. Copy and fill the `docker-compose.yml` example below. 3. Start it! ## ASF Configuration @@ -61,7 +62,7 @@ I recommend running ASFBot via its Docker image. Here it is an example docker-co - (2) Your Telegram bot token. - (3) Your Telegram user alias. - Run ```docker-compose up -d``` + Run docker-compose up -d` P.S.: ARMv7 and ARM64 docker builds are untested. Did you try them? Contact me! diff --git a/bot.py b/bot.py index 3df01da..6de73fb 100755 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import os os.chdir(os.path.dirname(os.path.realpath(__file__))) import telebot +from telebot import apihelper import re import argparse import logger @@ -32,7 +33,9 @@ parser.add_argument("--password", help="ASF IPC password.", default=None) parser.add_argument("--token", type=str, help="Telegram API token given by @botfather.", default=None) -parser.add_argument("--proxy", help="Telegram Proxy, like http://192.168.1.1:7890", default=None) +parser.add_argument("--proxy", help="Use a proxy to connect to Telegram. Format: " + "://:. For example: http://192.168.1.1:7890" + , default=None) parser.add_argument("--alias", type=str, help="Telegram alias of the bot owner.", default=None) args = parser.parse_args() @@ -44,7 +47,8 @@ except KeyError as key_error: if not args.token: LOG.critical( - "No telegram bot token provided. Please do so using --token argument or %s environment variable.", _ENV_TELEGRAM_BOT_TOKEN) + "No telegram bot token provided. Please do so using --token argument or %s environment variable.", + _ENV_TELEGRAM_BOT_TOKEN) exit(1) try: @@ -52,7 +56,8 @@ except KeyError as key_error: if not args.alias: LOG.critical( - "No telegram user alias provided. Please do so using --alias argument or %s environment variable.", _ENV_TELEGRAM_USER_ALIAS) + "No telegram user alias provided. Please do so using --alias argument or %s environment variable.", + _ENV_TELEGRAM_USER_ALIAS) exit(1) try: args.proxy = os.environ[_ENV_TELEGRAM_PROXY] @@ -83,10 +88,11 @@ args.alias = args.alias.strip() args.host = args.host.strip() args.port = args.port.strip() -args.proxy = args.proxy.strip() if args.password: args.password = args.password.strip() +if args.proxy: + args.proxy = args.proxy.strip() LOG.info("Starting up bot...") LOG.debug("Telegram token: %s", args.token) @@ -105,10 +111,16 @@ except Exception as e: LOG.error("Couldn't communicate with ASF. Host: '%s' Port: '%s' \n %s", - args.host, args.port, str(e)) + args.host, args.port, str(e)) -if _ENV_TELEGRAM_PROXY != '': - telebot.apihelper.proxy = {'http':_ENV_TELEGRAM_PROXY} +if args.proxy and args.proxy != '' and '://' in args.proxy: + protocol, url = args.proxy.split('://', 1) + apihelper.proxy = { + protocol: url + } + LOG.info(f"Using proxy to connect to Telegram: {protocol}://{apihelper.proxy[protocol]}") +elif args.proxy and args.proxy != '' and '://' not in args.proxy: + LOG.error(f"Invalid proxy provided: {args.proxy}. Skipping...") bot = telebot.TeleBot(args.token)