Answer the question
In order to leave comments, you need to log in
How to change bot photo in telegram?
I write a bot in telegram. The bottom line is that when I launch the bot, it sends me a welcome text and a photo, and then buttons. And if I click on one of the buttons, I want to change the greeting photo to whatever I want, how can I implement all this?
let's say if you click cake1, the text changes, but there is no photo
Answer the question
In order to leave comments, you need to log in
Play around with this code.
find the corresponding pictures and see how it all works in the example below
import telebot
from telebot import types
token = ''
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def send_welcome(message):
photo = open('racoon.jpg', 'rb')
markup = create_markup()
bot.send_photo(message.chat.id, photo, caption='Енот', reply_markup=markup)
@bot.callback_query_handler(func=lambda c: True)
def change_photo(call):
markup = create_markup()
chat_id=call.message.chat.id
message_id=call.message.message_id
if call.data == 'racoon':
photo = open('racoon.jpg', 'rb')
media = types.InputMediaPhoto(photo, caption='Енот')
bot.edit_message_media(media=media, chat_id=chat_id, message_id=message_id, reply_markup=markup)
elif call.data == 'fox':
photo = open('fox.jpg', 'rb')
media = types.InputMediaPhoto(photo, caption='Лиса')
bot.edit_message_media(media=media, chat_id=chat_id, message_id=message_id, reply_markup=markup)
elif call.data == 'rabbit':
photo = open('rabbit.jpg', 'rb')
media = types.InputMediaPhoto(photo, caption='Заяц')
bot.edit_message_media(media=media, chat_id=chat_id, message_id=message_id, reply_markup=markup)
def create_markup():
markup = types.InlineKeyboardMarkup()
button1 = types.InlineKeyboardButton(text='Енот', callback_data='racoon')
button2 = types.InlineKeyboardButton(text='Лиса', callback_data='fox')
button3 = types.InlineKeyboardButton(text='Заяц', callback_data='rabbit')
markup.row(button1, button2, button3)
return markup
bot.polling()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question