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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay