DEV Community

Fenx Kishi
Fenx Kishi

Posted on

2 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

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

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

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay