Skip to content

Commit

Permalink
add proxy support.
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderzhng committed Dec 25, 2023
1 parent fde940a commit 242ca24
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Control your ASF instance anywhere.
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```
Expand Down
12 changes: 12 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_REGEX_COMMAND = '^[/!]\w+\s*(?P<bot>\w+)?'
_ENV_TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN"
_ENV_TELEGRAM_USER_ALIAS = "TELEGRAM_USER_ALIAS"
_ENV_TELEGRAM_PROXY = "TELEGRAM_PROXY"
_ENV_ASF_IPC_HOST = "ASF_IPC_HOST"
_ENV_ASF_IPC_PORT = "ASF_IPC_PORT"
_ENV_ASF_IPC_PASSWORD = "ASF_IPC_PASSWORD"
Expand All @@ -31,6 +32,7 @@
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("--alias", type=str, help="Telegram alias of the bot owner.", default=None)
args = parser.parse_args()

Expand All @@ -52,6 +54,10 @@
LOG.critical(
"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]
except KeyError as key_error:
pass

# ASF IPC related environment variables.
try:
Expand All @@ -77,11 +83,14 @@
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()


LOG.info("Starting up bot...")
LOG.debug("Telegram token: %s", args.token)
LOG.debug("Telegram Proxy %s", args.proxy)
LOG.debug("User alias: %s", args.alias)
LOG.debug("ASF IPC host: %s", args.host)
LOG.debug("ASF IPC port: %s", args.port)
Expand All @@ -97,6 +106,9 @@
except Exception as e:
LOG.error("Couldn't communicate with ASF. Host: '%s' Port: '%s' \n %s",
args.host, args.port, str(e))

if _ENV_TELEGRAM_PROXY != '':
telebot.apihelper.proxy = {'http':_ENV_TELEGRAM_PROXY}

bot = telebot.TeleBot(args.token)

Expand Down

0 comments on commit 242ca24

Please sign in to comment.