A
A
Azama Vaniev2020-09-28 15:53:22
Python
Azama Vaniev, 2020-09-28 15:53:22

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?
5f71dd2fea4ff992727563.jpeg
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

1 answer(s)
M
Mikhail Krostelev, 2020-09-29
@twistfire92

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()

The result of this code (All this is one message in which the picture was redrawn):
5f72cdac977d9702675906.png
5f72cdc24d779354601542.png
5f72cddaf2294374777349.png
The only point is if you change the hare for a hare, for example, then the code will fall into an error.
But that's another problem.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question