Skip to content

Commit

Permalink
Added <protocol>://<host>:<port> to Telegram proxy support (#18)
Browse files Browse the repository at this point in the history
* fixing some bugs of proxy support in bot.py and readme

* fixed readme.

* Added <protocol>://<host>:<port> to Telegram proxy support.
Updated documentation.
Fixed several bugs.
Messed a bit with code style.

---------

Co-authored-by: dmcallejo <dmcelectrico@gmail.com>
  • Loading branch information
zanderzhng and dmcallejo authored Jan 4, 2024
1 parent 878b6a1 commit a1842ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<protocol>://<host>:<port>`. 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
Expand Down Expand Up @@ -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!

Expand Down
26 changes: 19 additions & 7 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: "
"<protocol>://<host>:<port>. 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()

Expand All @@ -44,15 +47,17 @@
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:
args.alias = os.environ[_ENV_TELEGRAM_USER_ALIAS]
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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit a1842ae

Please sign in to comment.