skip to Main Content

I’m studying Python for create a simple Bot for Telegram, but I have a problem. I created 2 files exchange.py and BotHtmlTelegram.py

exchange.py:

EXCHANGE=1.125

def from_usd_to_eur(usd):
    return usd/EXCHANGE

def from_eur_to_usd(eur):
    return EXCHANGE*eur

and this BotHtmlTelegram.py:

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import exchange

TOKEN="My_Token"

def extract_number(text):
     return text.split()[1].strip()

def convert_usd(update, context):
     usd=float(extract_number(update.message.text))
     eur=exchange.from_usd_to_eur(usd)
     print(f'Eseguita conversione da {usd} USD a {eur} EUR')
     update.message.reply_text(f'{eur} EUR')

def convert_eur(update, context):
     eur=float(extract_number(update.message.text))
     usd=exchange.from_eur_to_usd(eur)
     print(f'Eseguita conversione da {eur} EUR a {usd} USD')
     update.message.reply_text(f'{usd} USD')

def main():
   upd= Updater("My_Token", use_context=True)
   disp=upd.dispatcher

   disp.add_handler(CommandHandler("usd", convert_usd))
   disp.add_handler(CommandHandler("eur", convert_eur))

   upd.start_polling()

   upd.idle()

if __name__=='__main__':
   main()

In my Bot Telegram I set command /eur and /usd – If I write /eur 100 the result it’s ok, but when I write /usr 100 nothing happens –

What am I doing wrong?

Thanks a lot mate

2

Answers


  1. Chosen as BEST ANSWER

    Thanks guys, your answer will be my next step to study. At the moment the small program work. I run for a second time and now /usd (thanks @nordmanden) work correctly


    • Check if the output you’re getting from from_usd_to_eur(usd) isn’t
      too long to be displayed. Also try with "//" instead of "/". The
      first one giving you an int instead of a float.

    • You misspelled "usd" as "usr" when you called the command, maybe
      that’s the reason.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search