0

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'}
frankfalse
  • 1,553
  • 1
  • 4
  • 17

1 Answers1

0

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.

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • thanks but it steal errors description: 'not found' `data = { "chat_id": chat_id, "media": json.dumps([ {"type": "document", "media": "attach:C:\\Users\\...\\0.jpg"}, {"type": "document", "media": "attach:C:\\Users\\...\\0.jpg"} ]) } files = { "photo1.png" : open("C:\\Users\\Achak\\Desktop\\graphiccs\\typhoone\\outcome\\0.jpg", 'rb'), "photo2.png" : open("C:\\Users\\Achak\\Desktop\\graphiccs\\typhoone\\outcome\\1.jpg", 'rb') } temp = requests.post("https://api.telegram.org/bot" + TOKEN + "/sendDocumentGroup", data=data, files=files) ` –  Mar 16 '23 at 11:49
  • I wondered if I could get some peace of advice from you. –  Mar 16 '23 at 11:54
  • 1
    Please see my edit, you're using the wrong method. – 0stone0 Mar 16 '23 at 12:19
  • 1
    thank you so much, i made a mistake. youre code works correct. –  Mar 16 '23 at 14:20
  • 1
    Glad it worked! Please consider upvoting / accepting the answer to show the community that the answer has been useful, and will give some rep to both the questioner and answerer. – 0stone0 Mar 16 '23 at 14:38