C4H7Cl2O4P
C4H7Cl2O4P

Reputation: 11

How to send multiple images which is album to Telegram by pure python without any frameworks, robots etc

I know I must use https://core.telegram.org/bots/api#sendmediagroup

I tried several different ways to pack 'media' parameter for it. Every time I get

{"ok":false,"error_code":400,"description":"Bad Request: can't parse media JSON object"}

Here is my current text:

def sendMediaGroup(pict_full_filename__list, description, to_whom=None, silent=True):
    command = (T_url + "sendMediaGroup")

    sent_picture__dict = dict()
    files_content_to_send = list()

    for one_pict in pict_full_filename__list:
        sent_picture__dict[one_pict] = open(one_pict, 'rb')
        files_content_to_send.append({
                                    'type': 'photo',
                                    'media':  f'attach://{one_pict}',
                                    'caption': description,
                                    'parse_mode': 'HTML' })

    parameters = {"chat_id": str(to_whom),
                  # "media": files_content_to_send,  # it caused the error
                  "media": str(files_content_to_send).replace("'", '"'),  # correct way
                  #### "protect_content":
                  #### "reply_to_message_id":
                  #### "allow_sending_without_reply":
                  "disable_notification": silent}

    # print(prettyjson(parameters))
    # exit()
    # input()

    response = requests.post(command,
                             data=parameters,
                             files=sent_picture__dict)

    if not json.loads(response.text)["ok"]:
        print('FAILED')
        print(f'Recipient: {to_whom}')
        print(f'Reply: {response.text}')
        print()

    for key in sent_picture__dict:
        sent_picture__dict[key].close()

What am I doing wrong and what do I have to do ?

Upvotes: 0

Views: 1164

Answers (1)

kroner108
kroner108

Reputation: 1

Possibly because you have one_pict both a filename and an identifier f'random-name-{}' between attach:// and files

Upvotes: 0

Related Questions