Search code examples
pythontelegramtelegram-botpy-telegram-bot-api

Text + image not working with telegram bot


Having troubles with sending image with text. Instead of actual image, I'm just getting the path to it. I wonder if someone knows what is the problem in my code. Instead of the path I want an actual image

enter image description here

import telebot
from telebot import types

bot = telebot.TeleBot("token")

@bot.message_handler(commands = ['start'])
def button(message):
    markup = types.InlineKeyboardMarkup(row_width=2)
    item_4 = types.InlineKeyboardButton('q1', callback_data ='da')
    item_3 = types.InlineKeyboardButton("asdas", callback_data = 'net')
    markup.add(item_4, item_3)

    img = r'C:\Python\k123s.jpg'
    text = 'Your profile!'

    bot.send_message(message.chat.id, f'{text}\n{img}', reply_markup = markup)

Solution

  • I found the solution:

    import telebot
    from telebot import types
    
    bot = telebot.TeleBot("token")
    
    @bot.message_handler(commands = ['start'])
    def button(message):
        markup = types.InlineKeyboardMarkup(row_width=2)
        item_4 = types.InlineKeyboardButton('q1', callback_data ='da')
        item_3 = types.InlineKeyboardButton("asdas", callback_data = 'net')
        markup.add(item_4, item_3)
    
        #img = r'C:\Python\k123s.jpg'
        #text = 'Your profile!'
    
        bot.send_message(message.chat.id, open=(r'C:\Python\k123s.jpg', 'rb'), caption="text", reply_markup = markup)
    
    

    PyTelegramBotApi has captions, which lets you add text to photos caption = 'text'