-3

I have a simple Telegram Bot in python which worked normal, I have run it today and I got an error for a specific country which says the following;

return self.callback(update, context)
  File "main.py", line 21, in stats
    data['confirmed'],
TypeError: 'NoneType' object is not subscriptable

Here is my code;

from telegram import ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, ConversationHandler, MessageHandler, Filters
from covid19 import Covid19

buttons = ReplyKeyboardMarkup([['Statistics'], ['World']], resize_keyboard=True)
covid = Covid19()
TOKEN = '**********'

def start(update, context):
    update.message.reply_html(
        '<b>Greetings, {}</b>\n \n some text'.format(update.message.from_user.first_name), reply_markup=buttons)
    return 1

def stats(update, context):
    data = covid.getByCountryCode('UA')
    update.message.reply_html(
        ' <b>In Ukraine</b>\n \n<b>Infected:</b> {}\n<b>Recovered:</b> {}\n<b>Dead:</b> {}'.
            format(
            data['confirmed'],
            data['recovered'],
            data['deaths']), reply_markup=buttons)

def world(update, context):
    data = covid.getLatest()
    update.message.reply_html(
        ' <b>World</b>\n \n<b>Infected:</b> {}\n<b>Recovered:</b> {}\n<b>Dead:</b> {}'.format(
            "{:,}".format(data['confirmed']),
            "{:,}".format(data['recovered']),
            "{:,}".format(data['deaths'])
        ), reply_markup=buttons)

updater = Updater(TOKEN, use_context=True)
conv_handler = ConversationHandler(
    entry_points = [CommandHandler('start', start)],
    states={
        1: [MessageHandler(Filters.regex('^(Statistics)$'), stats),
            MessageHandler(Filters.regex('^(World)$'), world),
            ]
    },
    fallbacks=[MessageHandler(Filters.text, start)]
)

updater.dispatcher.add_handler(conv_handler)
updater.start_polling()
updater.idle()

After /start it brings two buttons - Statistics and World, when I click or type World it works normally when I click on Statistics, it brings that error I mentioned above.

So what is wrong with my code, it worked a couple of days ago, and now what am I doing wrong?

My question is different than this one Python Math - TypeError: 'NoneType' object is not subscriptable. Mine is Telegram Tracker Bot which worked a day before and now it stopped working, the reason is this part;

def stats(update, context):
    data = covid.getByCountryCode('UA')
    update.message.reply_html(
        ' <b>In Ukraine</b>\n \n<b>Infected:</b> {}\n<b>Recovered:</b> {}\n<b>Dead:</b> {}'.
            format(
            data['confirmed'],
            data['recovered'],
            data['deaths']), reply_markup=buttons)

What is wrong here, the error could be the same but this is written in telegram and COVID modules not only math. I will appreciate it if someone corrects my code.

1 Answers1

0

What this message is saying is that your variable data has no value (i.e. it has the value of None). Therefore, you cannot ask Python what is data['confirmed'].

Since you are getting the value of data from covid.getByCountryCode('UA'), it would make sense to check why this call is returning None. Perhaps read the documentation or look at its source code. I'd venture to guess that it's making a call to some online webservice which is either down or not working as expected.

jurez
  • 4,436
  • 2
  • 12
  • 20