Search code examples
pythonjsonhttptelegram

post request errors "description not found" 404


I'm tiring to send some files to telegram, but the program receives the errors 404.

This is my code:

import json
import requests

chat_id ="-"
TOKEN = "--"

data = {
    "chat_id": chat_id,
    "document": json.dumps([
        {"type": "document", "document":"attach:path_to_file"},
        {"type": "document", "document":"attach:path_to_file"}
# paths are like C:\\a\\b\\c\\d.jpg
    ])
}

files = {
    "photo1.png" : open("path_to_file", 'rb'),
    "photo2.png" : open("path_to_file", 'rb')
}

temp = requests.post("https://api.telegram.org/bot" + TOKEN + "/sendDocumentGroup", data=data, files=files)

print(temp.json())

The error appears as below:

{'ok': False, 'error_code': 404, 'description': 'Not Found'}

Solution

  • If you want to send a group of files, you'll just need to alter the type.

    The media key from your previous question should stay.

    So use the following data:

    data = {
        "chat_id": chat_id,
        "media": json.dumps([
            {"type": "document", "media": "attach://photo1.png"},
            {"type": "document", "media": "attach://photo2.png"}
        ])
    }
    

    enter image description here


    Based on your comment, you're using sendDocumentGroup, but you'll need sendMediaGroup. The sendMediaGroup works for files and images.