L
L
Liper2020-04-14 17:14:25
Python
Liper, 2020-04-14 17:14:25

Telegram bot. Where is the mistake?

import telebot
from telebot import types
import COVID19Py

covid19 = COVID19Py.COVID19()
bot = telebot.TeleBot('токен')

@bot.message_handler(commands=['start'])
def start(message):
  markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
  btn1 = types.KeyboardButton('Во всём мире')
  btn2 = types.KeyboardButton('Украина')
  btn3 = types.KeyboardButton('Россия')
  btn4 = types.KeyboardButton('Беларусь')
  markup.add(btn1, btn2, btn3, btn4)

  send_message = f"<b>Привет, {message.from_user.first_name}!</b>\nЧтобы узнать данные по коронавирусу напишите " \
    f"название страны, например: США, Украина, Россия и так далее\n\nЗаходи к нам на канал @lollinks"
  bot.send_message(message.chat.id, send_message, parse_mode='html', reply_markup=markup)

@bot.message_handler(content_types=['text'])
def mess(message):
  final_message = ""
  get_message_bot = message.text.strip().lower()
  if get_message_bot == "сша":
    location = covid19.getLocationByCountryCode("US")
  elif get_message_bot == "украина":
    location = covid19.getLocationByCountryCode("UA")
  elif get_message_bot == "россия":
    location = covid19.getLocationByCountryCode("RU")
  elif get_message_bot == "беларусь":
    location = covid19.getLocationByCountryCode("BY")
  elif get_message_bot == "казакхстан":
    location = covid19.getLocationByCountryCode("KZ")
  elif get_message_bot == "италия":
    location = covid19.getLocationByCountryCode("IT")
  elif get_message_bot == "франция":
    location = covid19.getLocationByCountryCode("FR")
  elif get_message_bot == "германия":
    location = covid19.getLocationByCountryCode("DE")
  elif get_message_bot == "япония":
    location = covid19.getLocationByCountryCode("JP")
  else:
    location = covid19.getLatest()
  final_message = f"<u>Данные по всему миру:</u>\n<b>Заболевших: </b>{location['confirmed']:,}\n<b>Сметрей: </b>{location['deaths']:,}"

  if final_message == "":
    date = location[0]['last_updated'].split("T")
    time = date[1].split(".")
    final_message = f"<u>Данные по стране:</u>\nНаселение: {location[0]['country_population']:,}\n" \
        f"Последнее обновление: {date[0]} {time[0]}\nПоследние данные:\n<b>" \
        f"Заболевших: </b>{location[0]['latest']['confirmed']:,}\n<b>Сметрей: </b>" \
        f"{location[0]['latest']['deaths']:,}"

  bot.send_message(message.chat.id, final_message, parse_mode='html')

bot.polling(none_stop=True)


Displays data around the world, but gives an error by country
5e95c74b7b9e6749565352.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-04-14
@sergey-gornostaev

The location variable contains a list, not a dictionary.

P
Pavel, 2020-04-23
@pashadark

The answer lies here

final_message = f"<u>Данные по всему миру:</u>\n<b>Заболевших: </b>{location['confirmed']:,}\n<b>Сметрей: </b>{location['deaths']:,}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question