DEV Community

Fenx Kishi
Fenx Kishi

Posted on

2 1 1 1

Issue in telegram bot by flask python and webhook.

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 -

Enter fullscreen mode Exit fullscreen mode

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")
`
Enter fullscreen mode Exit fullscreen mode

I searched and asked Ai and never solved

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (4)

Collapse
 
juliarizza profile image
Julia Rizza

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 your home() 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.

Collapse
 
fenx_kishi profile image
Fenx Kishi • Edited

It worked thanks

Collapse
 
juliarizza profile image
Julia Rizza

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:

Thread Thread
 
fenx_kishi profile image
Fenx Kishi

It worked thanks

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay