D
D
Danila Smirnov2020-06-02 01:38:30
Python
Danila Smirnov, 2020-06-02 01:38:30

Nothing is displayed in the chat, but in the console (bottom) how to fix it?

from covid import Covid
import telebot
import config


bot = telebot.TeleBot(config.TOKEN)
covid = Covid()


@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
  bot.reply_to(message, "Начнем")


@bot.message_handler(content_types=['text'])
def start(message):
  final_message = ""
  get_message_bot = message.text.strip().lower()
  if get_message_bot == "США":
    location = covid.get_status_by_country_name("us")
  elif get_message_bot == "Украина":
    location = covid.get_status_by_country_name("Ukraine")
  elif get_message_bot == "Россия":
    location = covid.get_status_by_country_name("russia")
  elif get_message_bot == "Беларусь":
    location = covid.get_status_by_country_name("Belarus")
  elif get_message_bot == "Казахстан":
    location = covid.get_status_by_country_name("Kazakhstan")
  elif get_message_bot == "Италия":
    location = covid.get_status_by_country_name("italy")
  elif get_message_bot == "Франция":
    location = covid.get_status_by_country_name("france")
  elif get_message_bot == "Германия":
    location = covid.get_status_by_country_name("Germany")
  elif get_message_bot == "Япония":
    location = covid.get_status_by_country_name("Japan")
    final_message = f"<u>Данные по стране:</u>\n<b>Заболевших: </b>{location['confirmed']:,}\n<b>Умерших: </b>{location['deaths']:,}\n<b>Выздоровевших: </b>{location['recovered']:,}\n<b>Болеющих: </b>{location['active']:,}"
    
  else:
        
    location = covid.get_status_by_country_name("russia")
    
  bot.send_message(message.chat.id, final_message, parse_mode='html')

bot.polling(none_stop=True)

ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}']"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-06-02
@aske280

The logic of your check is not clear to me. First, you convert the string to lowercase, and then compare it with a string that begins with a capital letter. Lots of if, elif, and else constructs are bad. I would replace them all with a dictionary.
For example, like this:

from covid import Covid
import telebot
import config

bot = telebot.TeleBot(config.TOKEN)
covid = Covid()

country = { 'сша':'Us',
      'украина':'Ukraine',
      'россия':'Russia',
      'беларусь':'Belarus',
      'казахстан':'Kazakhstan',
      'италия':'Italy',
      'франция':'France',
      'германия':'Germany',
      'япония':'Japan',
      }

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
  bot.reply_to(message, "Начнем")

@bot.message_handler(content_types=['text'])
def start(message):
  get_message_bot = message.text.strip().lower()
  location = covid.get_status_by_country_name(country.get(get_message_bot,'russia'))

  final_message = f"<u>Данные по стране:</u>\n<b>Заболевших: </b>{location['confirmed']:,}\n<b>Умерших: </b>{location['deaths']:,}\n<b>Выздоровевших: </b>{location['recovered']:,}\n<b>Болеющих: </b>{location['active']:,}"
  bot.send_message(message.chat.id, final_message, parse_mode='html')

bot.polling(none_stop=True)

Let's look at the line:
location = covid.get_status_by_country_name(country.get(get_message_bot,'russia'))

country.get(get_message_bot, 'russia') - here we are trying to get the dictionary value by key. As a key, we pass the string that we received from the user. If there is an element with a key in the country dictionary
, then we return the value of the element by the key, and pass this value to the
get_status_by_country_name method. If there is no such key in the dictionary, then we return the standard value (in our case, it is 'russia') and pass it to the get_status_by_country_name method.
You can still go further. Store the dictionary of countries in a file. (In general, it is not customary to hardcore such values ​​in the code.)
They also invented the greatest debugging invention - the print function) It is especially useful if you code in sublime or in notepad. You can display the value of variables on the console, and understand what is stored in them.
From myself I will add advice - all the functions that are used in the bot (which do not need the telegram api) should be written and debugged in a separate file in a simple script. And then, when you debug it, integrate it into your bot. After all, it is much easier to pass a value from your script to a function and look at the output than to launch a bot, open telegrams, write to a bot, etc.

D
Dr. Bacon, 2020-06-02
@bacon

Here they obviously write "message text is empty" is it difficult to think a bit? What message will be for russia?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question