Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed webhook setup for work with revers proxy #197

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,21 @@ To use Webhook instead Polling, you need a signed certificate file in the system
openssl req -newkey rsa:2048 -sha256 -nodes -keyout private.key -x509 -days 3650 -out cert.pem
```

Once you have the key and cert files, setup the next lines in "settings.py" file to point to expected Webhook Host address, port and certificate file:
Once you have the key and cert files, setup the next lines in "settings.py" file to point to expected Webhook URL, Host address, port, path and certificate file:

```python
"WEBHOOK_HOST": "Current system IP/DNS here",
"WEBHOOK_URL": "https://example.com:8443/TLG_JoinCaptchaBot"
"WEBHOOK_IP": "0.0.0.0",
"WEBHOOK_PORT": 8443,
"WEBHOOK_PATH": "/TLG_JoinCaptchaBot"
"WEBHOOK_CERT" : SCRIPT_PATH + "/cert.pem",
"WEBHOOK_CERT_PRIV_KEY" : SCRIPT_PATH + "/private.key",
```

To use Polling instead Webhook, just set host value back to none:

```python
"WEBHOOK_HOST": "None",
"WEBHOOK_URL": "None",
```

## Environment Variables Setup
Expand Down
24 changes: 17 additions & 7 deletions sources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,35 @@
"BOT_OWNER":
os_getenv("CAPTCHABOT_OWNER", SETTINGS["CAPTCHABOT_OWNER"]),

# Bot Webhook Host addres (keep in None for Polling or set to a
# Bot Webhook URL (keep in None for Polling or set to a
# valid address for Webhook)
"WEBHOOK_HOST":
os_getenv("CAPTCHABOT_WEBHOOK_HOST",
SETTINGS["CAPTCHABOT_WEBHOOK_HOST"]),
"WEBHOOK_URL":
os_getenv("CAPTCHABOT_WEBHOOK_URL",
SETTINGS["CAPTCHABOT_WEBHOOK_URL"]),

# Bot Webhook Host Port (this is not used if WEBHOOK_HOST is None)
# Bot Webhook Listen Address (this is not used if WEBHOOK_URL is None)
"WEBHOOK_IP":
os_getenv("CAPTCHABOT_WEBHOOK_IP",
SETTINGS["CAPTCHABOT_WEBHOOK_IP"]),

# Bot Webhook Listen Port (this is not used if WEBHOOK_URL is None)
"WEBHOOK_PORT":
int(os_getenv("CAPTCHABOT_WEBHOOK_PORT",
SETTINGS["CAPTCHABOT_WEBHOOK_PORT"])),

# Bot Webhook path (this is not used if WEBHOOK_URL is None)
"WEBHOOK_PATH":
os_getenv("CAPTCHABOT_WEBHOOK_PATH",
SETTINGS["CAPTCHABOT_WEBHOOK_PATH"]),

# Bot Webhook Certificate file path (this is not used if
# WEBHOOK_HOST is None)
# WEBHOOK_URL is None)
"WEBHOOK_CERT":
os_getenv("CAPTCHABOT_WEBHOOK_CERT",
SETTINGS["CAPTCHABOT_WEBHOOK_CERT"]),

# Bot Webhook Certificate private key file path (this is not used
# if WEBHOOK_HOST is None)
# if WEBHOOK_URL is None)
"WEBHOOK_CERT_PRIV_KEY":
os_getenv("CAPTCHABOT_WEBHOOK_CERT_PRIV_KEY",
SETTINGS["CAPTCHABOT_WEBHOOK_CERT_PRIV_KEY"]),
Expand Down
11 changes: 5 additions & 6 deletions sources/join_captcha_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3896,7 +3896,7 @@ def tlg_app_run(app: Application) -> None:
function will be called at the end.
'''
logger.info("Bot Application Started")
if CONST["WEBHOOK_HOST"] == "None":
if CONST["WEBHOOK_URL"] == "None":
logger.info("Setup Bot for Polling.")
app.run_polling(
drop_pending_updates=True,
Expand All @@ -3905,14 +3905,13 @@ def tlg_app_run(app: Application) -> None:
else:
logger.info("Setup Bot for Webhook.")
app.run_webhook(
webhook_url=f'https://{CONST["WEBHOOK_HOST"]}:'
f'{CONST["WEBHOOK_PORT"]}/{CONST["TOKEN"]}',
url_path=CONST["TOKEN"],
webhook_url=CONST["WEBHOOK_URL"],
listen=CONST["WEBHOOK_IP"],
port=CONST["WEBHOOK_PORT"],
key=CONST["WEBHOOK_CERT_PRIV_KEY"],
url_path=CONST["WEBHOOK_PATH"],
cert=CONST["WEBHOOK_CERT"],
key=CONST["WEBHOOK_CERT_PRIV_KEY"],
secret_token=CONST["WEBHOOK_SECRET_TOKEN"],
listen="0.0.0.0",
drop_pending_updates=True,
allowed_updates=Update.ALL_TYPES
)
Expand Down
20 changes: 15 additions & 5 deletions sources/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,30 @@
# Bot Owner (i.e. "@JoseTLG" or "123456789")
"CAPTCHABOT_OWNER": "XXXXXXXXX",

# Bot Webhook Host address (keep in "None" for Polling or set to a
# Bot Webhook URL (keep in "None" for Polling or set to a
# valid address for Webhook)
"CAPTCHABOT_WEBHOOK_HOST": "None",
# This is the address that the Telegram server should call.
# If you are not using a reverse proxy, then the WEBHOOK_URL
# should look something like this:
# https://example.com:{WEBHOOK_PORT}/{WEBHOOK_PATH}
"CAPTCHABOT_WEBHOOK_URL": "None",

# Bot Webhook Host Port (this is not used if WEBHOOK_HOST is None)
# Bot Webhook Listen address (this is not used if WEBHOOK_URL is None)
"CAPTCHABOT_WEBHOOK_IP": "0.0.0.0",

# Bot Webhook Listen Port (this is not used if WEBHOOK_URL is None)
# For Heroku, use the following: os_environ.get("PORT", "80")
"CAPTCHABOT_WEBHOOK_PORT": 8443,

# Bot Webhook Path (this is not used if WEBHOOK_URL is None)
"CAPTCHABOT_WEBHOOK_PATH": "/TLG_JoinCaptchaBot",

# Bot Webhook Certificate file path (this is not used if
# WEBHOOK_HOST is None)
# WEBHOOK_URL is None)
"CAPTCHABOT_WEBHOOK_CERT": SCRIPT_PATH + "/cert.pem",

# Bot Webhook Certificate private key file path (this is not used
# if WEBHOOK_HOST is None)
# if WEBHOOK_URL is None)
"CAPTCHABOT_WEBHOOK_CERT_PRIV_KEY": SCRIPT_PATH + "/private.key",

# Bot Webhook Secret Token to verify request from Telegram Server
Expand Down
Loading