Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send telegram mediaGroup with caption/text

I'm currently using python-telegram-bot and basically what I want to achieve with it is to send telegram messages like this:

telegram post with 3 photos and caption

So the message consists of 2+ photos/videos with text message underneath.

What I've already tried:

  1. sending message with send_message method, and including photo URLs, but it only shows 1 picture which is under the text

  2. sending media group using send_media_group, but this method has no caption parameter as send_photo.

like image 886
marzique Avatar asked Nov 16 '19 17:11

marzique


People also ask

Can I send message by BOT in telegram?

Getting StartedOpen the telegram app and search for @BotFather. Click on the start button or send “/start”. Then send “/newbot” message to set up a name and a username. After setting name and username BotFather will give you an API token which is your bot token.

How do I send a caption to a telegram message?

You should use sendMediaGroup, where you can specify media field with an array of photo / video objects but the trick is to set caption property only for the first element of an array. In this case telegram will show that caption below the media content.

How do you send a direct message on Telegram?

How to Send a Direct Message on Telegram. 1 Launch Telegram. 2 Press “New message.”. 3 Select the recipient of the message. 4 Press the username of the person to open your chat with them. 5 Type in your message. 6 Hit the “Send” button, and you’re good to go.

How to send bulk messages to telegram group members?

Telegram Sender is a software for Windows PC to send bulk messages to Telegram group or channel members. With one click, you can send messages to all the Telegram users you have. You can add users manually or import them from a file. It helps you extract the user ID of all users from groups, whether you’re the admin or not.

How to send promotional messages on Telegram with Python?

You can search for Telegram groups of your interest, type a personalised message and send it to each member individually. With one tap, all Telegram group members receive your promotional message. You can also get the details of the users who received your message. 3. Python script to send bulk messages on Telegram


2 Answers

You should use sendMediaGroup, where you can specify media field with an array of photo/video objects but the trick is to set caption property only for the first element of an array. In this case telegram will show that caption below the media content.

If you'll specify captions for more than one element telegram will show them only when you click on photo preview for each photo separately.

like image 196
SleepWalker Avatar answered Oct 28 '22 09:10

SleepWalker


send_media_group works but the caption has to be added when creating the media_group and to the first image only. Let's say we have three images img0.png, img1.png and img2.png, we add them to the media_group using InputMediaPhoto with the parameter caption equal to the text we want to send only for the first image, otherwise we set caption equal to ''.

import telegram
from telegram import InputMediaPhoto

TOKEN = '' # token to access the HTTP API of your bot created with @BotFather
CHANNEL_ID = '' # id of your channel, for example @durov
bot = telegram.Bot(token = TOKEN)
media_group = []
text = 'some caption for album'
for num in range(3):
    media_group.append(InputMediaPhoto(open('img%d.png' % num, 'rb'), 
                                       caption = text if num == 0 else ''))
bot.send_media_group(chat_id = CHANNEL_ID, media = media_group)

enter image description here

like image 38
sound wave Avatar answered Oct 28 '22 09:10

sound wave