[Python] Bad Request: chat not found, error_code:400, i am have issue while sending message on...

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project where I want to send all the reminders due in 3 days from now, but I am facing a problem. I have an error code 400 while sending the message on Telegram (for which I am using a Telegram bot)

    This is the Code

    def send_message(message, id):
    url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
    response = requests.post(url, data={
    "chat_id": id,
    "text": message,
    "parse_mode": "Markdown"
    })
    print("Telegram Response:", response.text)
    return response.status_code

    def send_due_reminders():
    today = datetime.today().date()

    try:
    conn, cursor = database_con()

    # 1. Check last update date
    cursor.execute("SELECT * FROM update_mess WHERE id = %s", ('reminders',))
    result = cursor.fetchone()
    print(result)

    if result:
    last_updated = result[2]
    telegram_id = int(result[1])
    else:
    return "No 'reminders' row found in update_mess table."

    if last_updated == today:
    return "Reminders already sent today."

    if not telegram_id:
    return "Telegram ID is not set."

    # 2. Get due reminders
    threshold_date = today + timedelta(days=3)
    cursor.execute("""
    SELECT id, title, task_detail, due_date, assigned_to
    FROM reminders
    WHERE status = 'Pending' AND due_date <= %s
    """, (threshold_date,))
    reminders = cursor.fetchall()

    if not reminders:
    return "No due reminders within 3 days."

    # 3. Format and send message
    message = " Upcoming Reminders (Due in 3 Days)\n\n"
    for r in reminders:
    message += (
    f" Title: {r[1]}\n"
    f" Task: {r[2]}\n"
    f" Assigned To: {r[4]}\n"
    f" Due Date: {r[3]}\n\n"
    )

    print(len(message))

    response = send_message(telegram_id, message)

    if response == 200:
    status = "Reminders were sent to Telegram successfully!"
    else:
    status = f"Telegram send failed with status code: {response}"

    # 4. Update last_updated
    cursor.execute("UPDATE update_mess SET last_updated = %s WHERE id = %s", (today, 'reminders'))
    conn.commit()

    return status

    except Exception as err:
    return f"Error: {err}"

    finally:
    cursor.close()
    conn.close()


    Output

    ('reminders', '62XXXXXXX', datetime.date(2025, 6, 2))

    len of mess:652

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

    What I have tried

    The telegram ID is working. I have tried the same ID in different parts of the project, where it sends reminder details on completion, updating, and creation of the reminder

    I have changed message to plain message so it does not mess with the "Markdown" part of the code "parse_mode": "Markdown"

    I have checked if the message is not too long len of mess: 652, and I guess this is not that big (chatGPT told me we can send 4096 characters)

    At this point I don't know what is the problem

    Continue reading...

Compartilhe esta Página