T
T
twink1322022-04-15 12:19:49
Python
twink132, 2022-04-15 12:19:49

Implementation of resending a message from the bot to the chat (just text OR photo with text in caption) how?

I am making a small bot, the task is as follows:
Make a bot that, depending on the button pressed, changes the chat_id (channel name) and resends requests from the bot to the channel.
Everything works, depending on the button, the id changes, and TEXT messages are sent to the desired channel.
But I need to make it so that the user can send a Picture (with a caption in the picture with a question) or Text and it is sent according to their preferences. Now only text is sent, I don’t understand how to implement data validation in the handler so that, depending on the type of data, it sends a photo with text or just text.

Below is the implementation of the function:
It is called after selecting a button in the inline keyboard after assigning a channel ID.

@bot.message_handler(func=lambda message: True) ##Пересылка сообщений в канал
def echo_message(message):
    
    bot.send_message(chat_id=chat_id, text=message.text+"\n\nЗапрос от монтера - #"+message.from_user.first_name)
    bot.send_message(message.chat.id, "Для создания нового обращения введите команду /start")


It turned out to develop a separate function that simply sends back a picture (echo bot). But she only sends the photo to herself (from bot to bot), but does not want to send it to the channel.
And another question, perhaps everything will be collected in one function (def echo_message) sending text messages and sending photos with text (the text will go to the caption).
@bot.message_handler(content_types = ['photo']) ##фотки эхо
def echo_photo(message):
    global chat_id
    chat_id = '@xxx'
    bot.send_photo(message.chat.id, message.photo[0].file_id, message.caption)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2022-04-15
@twink132

But she only sends a photo to herself

Check where you are sending image in methodsend_photo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question