我的Covid Tracker Telegram Bot有什么问题?

问题描述

我在python中有一个简单的Telegram Bot,它可以正常工作,今天运行了它,并且在特定国家/地区出现错误提示以下内容

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

这是我的代码

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()

/start之后,它带来了两个按钮-统计和世界,当我单击或键入世界时,当我单击统计时它可以正常工作,它带来了我上面提到的错误

那么我的代码有什么问题,它几天前就起作用了,现在我在做什么错了?

我的问题不同于这个 Python Math - TypeError: 'NoneType' object is not subscriptable 。我的是Telegram Tracker Bot,它一天前就工作了,现在停止工作了,原因是这部分;

def stats(update,reply_markup=buttons)

这是怎么回事,错误可能是相同的,但这不仅是数学形式,而且是用电报和COVID模块编写的。 如果有人更正我的代码,我将不胜感激。

解决方法

此消息的意思是您的变量data没有值(即它的值为None)。因此,您不能问Python data['confirmed']是什么。

由于您从data获得了covid.getByCountryCode('UA')的值,因此检查此调用为何返回None的原因将是有意义的。也许阅读文档或查看其源代码。我冒昧地猜到它正在呼叫某些在线Web服务,该服务已关闭或未按预期工作。