Guys, I am writing a Telegram bot in (Python Flask) via webhook and I am facing a very strange problem that I have not found a solution for. It always appears in the logs of the site where the bot is hosted (render):
`127.0.0.1 - - [11/Mar/2025 02:20:01] "POST / HTTP/1.1" 405 -
THE CODE:
`> `#modules
> import os
> import json
> import requests
> from flask import Flask, request
> from pymongo import MongoClient
> from apscheduler.schedulers.background import BackgroundScheduler
> from apscheduler.triggers.date import DateTrigger
> from dotenv import load_dotenv
> from datetime import datetime
> #imp.data
> load_dotenv()
> BOT_TOKEN = os.getenv("BOT_TOKEN")
> MONGO_URI = os.getenv("MONGO_URI")
> WEBHOOK_URL = os.getenv("WEBHOOK_URL")
> AUTHORIZED_USERS = list(map(int, os.getenv("AUTHORIZED_USERS", "").split(",")))
> # Flask
> app = Flask(__name__)
> # إعداد قاعدة البيانات MongoDB
> client = MongoClient(MONGO_URI)
> db = client["task_manager"]
> tasks_collection = db["tasks"]
> reminders_collection = db["reminders"]
> # Task scheduler
> scheduler = BackgroundScheduler()
> scheduler.start()
> #webhook
> @app.route("/", methods=["GET", "POST"])
> def home():
> if request.method == "POST":
> return "This endpoint is not for webhooks!", 405
> return "Bot is running!"
> @app.route("/webhook", methods=["POST"])
> def webhook():
> update = request.get_json()
> if update:
> handle_message(update.get("message", {}))
> return "", 200 # Always return a successful response
> #code here..
> #code here..
> if __name__ == "__main__":`
app.run(host="0.0.0.0", port=5000)
requests.get(f"https://api.telegram.org/bot{TOKEN}setWebhook?url=https://testbot-zppg.onrender.com")
`
I searched and asked Ai and never solved
Top comments (4)
Hey, I don't see any errors. The endpoint is responding exactly as you wrote it to do.
You're doing a POST call to the
/
route, and then in yourhome()
function (where you linked that route), you check if it's a POST call and return a 405 status code. The 405 status is an error stating that the HTTP Method is not allowed.I believe you should configure your webhook to use the
/webhook
route instead of the/
one to get the result you're expecting. Try to set the webhook URL again adding/webhook
to the end of the URL.It worked thanks
I don't have any specific material that would help in this case. I think the best thing here is for you to understand each concept separately, then you'll understand how to link them:
It worked thanks