Search code examples
pythontelegram-botpython-telegram-botpy-telegram-bot-api

Return does not return information


When wanting to return a value with the return and wanting to use it later in a variable, nothing is returned to me.

#Actualizacion de informacion
def messageHandler(update: Update, context: CallbackContext):
    
    userName = update.effective_user['username']
    #text = update.message.text
    Usuario = userName
    ConsultarServer = ("Ingresa tu nombre de troncal correcta de lo contrario no se te regresara ninguna informacion.")

    if ConsultarSaldoSw2 in update.message.text:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Ingresa tu nombre de troncal correcta de lo contrario no se te regresara ninguna informacion.")
        text2 = update.message.text
        return text2
    
   
    print (text2)
    
    if text2 in update.message.text:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Ingresa el servidor sw o sw2")
        text3 = update.message.text
        return text3
    
    
    
    print (text3)

Solution

  • If the function is exhausted, and it doesn't meet any of your conditions, it will return None by default. Consider this shortened example:

    def messageHandler(update: Update, context: CallbackContext):
    
        if ConsultarSaldoSw2 in update.message.text:
            return text2
        
        if text2 in update.message.text:
            return text3
        
        # None of these if conditions are met, so nothing is returned
    

    If you don't enter any of these if statements, you never return a value.