I’m working on a Telegram bot using Python that converts currencies. The bot fetches exchange rate data from an API and returns it to users, but the output looks messy with all the JSON formatting.
Right now when users get the response, it shows something like {'rates': {'GBP': 0.75432}, 'base': 'USD', 'date': '2017-09-18'} which looks ugly with all those brackets and quotes. How can I make this output look cleaner and more user friendly?
Just use json.dumps() with indent if you want pretty formatting, but that’s still not what users want to see. Grab the actual rate value like rate = result['rates'][currency_code] and send something simple like "Exchange rate: {rate}". Way cleaner than dumping raw json.
I’ve dealt with this exact same mess. Your bot’s dumping raw JSON on users, which looks super unprofessional. You’re sending the whole API response instead of pulling out what people actually need. I fixed mine by writing a quick formatting function. Something like formatted_text = f"Current exchange rate:\n1 USD = {result['rates'][selected_currency]} {selected_currency}\nLast updated: {result['date']}" gives you clean output that’s actually readable. Your USD and GBP branches are doing the same thing - just merge them into one block that handles formatting for any currency. Way easier to maintain when you add more currencies later.
You’re sending the raw dictionary straight to users - that’s the problem. Extract the values first and format them into something readable. Instead of bot.sendMessage(user_id, result), do exchange_rate = result['rates'][selected_currency] then build a proper message like f"1 USD = {exchange_rate} {selected_currency}". Throw in the date from the API response too - makes it way more useful. I ran into this exact issue when I started building bots. Trust me, nobody wants to see raw JSON dumped in their chat. Also, you’re duplicating the same code for USD and GBP cases. Just consolidate that into one block that handles formatting for any currency.
The JSON formatting issue happens because you’re passing the entire dictionary to sendMessage. You need to format it as a string first. I hit this same problem with my first bot - here’s the fix: parse the JSON response and grab only what users need. Try rate_value = result['rates'][selected_currency] then formatted_message = f"Current rate: 1 USD = {rate_value} {selected_currency} (as of {result['date']})". Send formatted_message instead of the raw result. Users get clean text, not dictionary syntax. Also noticed you’ve got the same sendMessage call for both USD and GBP branches - you could clean up that logic too.
Yeah, that raw JSON dump looks terrible and makes your bot seem broken. But you’ve got a bigger problem - hardcoding everything will bite you hard when you add more features.
Stop manually parsing JSON and building message strings. Automate it. Set up a workflow that grabs API data, formats it cleanly, and sends proper messages automatically.
I built a currency bot last year and made the same mistakes. Started with manual string formatting, then spent more time fixing message templates than writing actual bot features.
Automation handles the JSON parsing and spits out clean messages like “ 1 USD = 0.75 GBP (Sep 18, 2017)”. Want rate alerts or historical data? Add them without touching your main bot code.
You also get error handling, rate limiting, and response caching built in. No more API failures breaking everything or users seeing garbage error messages.
Those duplicate if/elif blocks you’re using? They’ll turn into a mess when you add more currencies. Automation fixes that scaling nightmare right away.