Search code examples
python-telegram-bot

Telegram python bot - Send a photo issue


I'm struggling with telegram-python-bot. I'm trying to send an imagen when an action is triggered. The bot has a menu with some options, when the user selects one of them the bot replies with some text response. What i would like is to also send a picture along with the information. What i did was:

def opciones(bot, update, context):
  
  query = bot.callback_query
  query.answer()
  
  if query.data == "option1":
    query.message.reply_text(text=option1_info(), parse_mode='html', quote=False)
    context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(image_option1, 'rb'))

This replies with a "TypeError: opciones() missing 1 required positional argument: 'context'"

I've also tried to add this after query.message.replytext.

requests.post('https://api.telegram.org/bot' + TOKEN + '/sendPhoto', files={'photo': (image_arcadyan, open(image_arcadyan,'rb'))}, data= {'chat_id': chatId})

This works but only for a chat_id in particular, i cannot update the chat_id for every person or group that asks for it, I've tried using chat_id=update.effective_chat.id but had issues with update parameter. Any ideas? Thanks in advance


Solution

  • TypeError: opciones() missing 1 required positional argument: 'context'
    

    You get this exception because in python-telegram-bot (v12+) handler callbacks must accept exactly two positional arguments of type telegram.Update and telegram.ext.CallbackContext. I.e. your function signature should read

    def opciones(update, context):
        ...
    

    where update is an instance of telegram.Update and context is an instance of telegram.ext.CallbackContext.

    Note that the Bot instance is available as context.bot. See the docs of CallbackContext for more info on that object and the tutorial for a general introduction into PTB.

    If you change the signature and change query = bot.callback_query to query = update.callback_query, the rest of your snippet looks okay.


    Disclaimer: I'm currently the maintainer of python-telegram-bot.