[Python] Chat not found - Telegram bot

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I created a Telegram bot using @BotFather, and it works correctly with my own chat_id. However, when I use a different chat_id, it raises the following error:

    API Response: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}


    Do I need to add permissions, or how does it work?

    This is my code for sending the message:

    """Telegram utilities."""

    # Django
    from django.conf import settings

    # Utilities
    import requests


    def send_telegram_message(chat_id, message, parse_mode=None):
    """Send a message to a Telegram chat."""

    token = settings.TELEGRAM_TOKEN
    url = f"https://api.telegram.org/bot{token}/sendMessage?parse_mode=HTML"
    data = {"chat_id": chat_id, "text": message}
    if parse_mode:
    data['parse_mode'] = parse_mode
    try:
    response = requests.post(url, data=data)
    response.raise_for_status()
    except requests.exceptions.RequestException as e:
    print(f"Error al enviar mensaje de Telegram: {e}")
    if e.response is not None:
    print(f"Respuesta de la API: {e.response.text}")

    Continue reading...

Compartilhe esta Página