Unresponsive Server Despite Configuring Telegram Bot API Webhook

My PythonAnywhere webhook for Telegram Bot API triggers a ‘Bad Request’ error. How can I adjust the webhook setup to resolve this issue? See below snippet:

import time
from flask import Flask, request
import custombot

API_KEY = 'your_api_key_here'
HOST_NAME = 'user.pythonanywhere.com'
PORT_NUM = 9000

app = Flask(__name__)
bot_obj = custombot.Bot(API_KEY)

@app.route('/' + API_KEY, methods=['POST'])

def process_update():
    payload = request.get_json(force=True)
    update_instance = custombot.Update.from_dict(payload, bot_obj)
    bot_obj.send_message(chat_id=update_instance.message.chat_id, text='Hi there!')
    return 'Success'

if __name__ == '__main__':
    time.sleep(3)
    app.run(host='0.0.0.0', port=PORT_NUM, debug=False)
import sys

project_folder = '/home/user/telegram_project'
if project_folder not in sys.path:
    sys.path.insert(0, project_folder)

from app_main import app as application

I encountered a similar issue in the past and found that the key detail was ensuring that the webhook URL properly aligns with the host settings. On PythonAnywhere the web app is served on restricted ports and the URL must be externally reachable. I ended up adjusting the configuration to use the designated HTTPS endpoint and revising any port restrictions imposed by the hosting environment. Additionally, verifying the API key formatting resolved issues on my end. The resolution came after cross-checking the service documentation and ensuring the web hook callback adhered to the host’s requirements.

hey, i had similar probs. make sure you use the https endpoint and allowed port on pythonanywhere. custom ports may not work and could block reqests. also, a quick re-check of your api key may help solve the issue.