F
F
From Prog2021-08-03 20:42:06
Python
From Prog, 2021-08-03 20:42:06

Bot doesn't send a message to one user until it sends to another?

Code:

elif message.text == ' Конвертер валют':
    await message.answer('Выберите вашу валюту', reply_markup=currency_converterkbd)
    await Finance_states.to_convert.set()

@dptr.message_handler(state=Finance_states.to_convert)
async def answer_for_act_to_small(message: types.Message, state: FSMContext):
  can_do = True
  await state.update_data(text=message.text)
  data = await state.get_data()
  global currencies_for_currency_converter
  currencies_for_currency_converter[message.chat.id] = data['text']
  if currencies_for_currency_converter[message.chat.id] != ' RUB' and currencies_for_currency_converter[message.chat.id] != ' USD' and currencies_for_currency_converter[message.chat.id] != ' EUR' and currencies_for_currency_converter[message.chat.id] != ' GBP' and currencies_for_currency_converter[message.chat.id] != ' JPY' and currencies_for_currency_converter[message.chat.id] != ' AED' and currencies_for_currency_converter[message.chat.id] != ' CHF' and currencies_for_currency_converter[message.chat.id] != ' CNY' and currencies_for_currency_converter[message.chat.id] != ' KZT' and currencies_for_currency_converter[message.chat.id] != ' THB' and currencies_for_currency_converter[message.chat.id] != ' AZN' and currencies_for_currency_converter[message.chat.id] != ' CAD':
    if users_id_and_selected_language[message.chat.id] == 'ru':
      try:
        await state.finish()
      except KeyError:		
        await message.answer('Такой валюты нет в списке, выберите валюту', reply_markup=currency_converterkbd)
        await Finance_states.to_convert.set()
    elif users_id_and_selected_language[message.chat.id] == 'en':
      try:
        await state.finish()
      except KeyError:
        await message.answer("This currency is not on the list, choose currency", reply_markup=currency_converterkbd_en)
        await Finance_states.to_convert.set()
    can_do = False
  if users_id_and_selected_language[message.chat.id] == 'ru':
    if currencies_for_currency_converter[message.chat.id] == '↩️ Назад':
      try:
        await state.finish()
      except KeyError:
        can_do = False
        await mybot.send_message(message.chat.id, 'Выберите раздел ', reply_markup=mainkbd)	
    if can_do == True:
      try:
        await state.finish()
      except KeyError:
        await message.answer('Напишите количество валюты или нажмите ниже', reply_markup=number_of_currency_for_convertingkbd)
        await Finance_states.to_convert_main.set()
  elif users_id_and_selected_language[message.chat.id] == 'en':
    if currencies_for_currency_converter[message.chat.id] == '↩️ Back':
      try:
        await state.finish()
      except KeyError:
        can_do = False
        await mybot.send_message(message.chat.id, 'Choose section ', reply_markup=mainkbd_en)	
    if can_do == True:
      try:
        await state.finish()
      except KeyError:
        await message.answer('Write the amount of currency or click below', reply_markup=number_of_currency_for_convertingkbd)
        await Finance_states.to_convert_main.set()


@dptr.message_handler(state=Finance_states.to_convert_main)
async def answer_for_act_to_small(message: types.Message, state: FSMContext):
  await state.update_data(text=message.text)
  can_convert = True
  data = await state.get_data()
  try:
    global number_of_currency_for_converting
    number_of_currency_for_converting = int(data['text'])
  except ValueError:
    if users_id_and_selected_language[message.chat.id] == 'ru':
      try:
        await state.finish()
      except KeyError:
        await message.answer('Количество должно быть целым числом, напишите или выберите ниже', reply_markup=number_of_currency_for_convertingkbd)
        await Finance_states.to_convert_main.set()
    elif users_id_and_selected_language[message.chat.id] == 'en':
      try:
        await state.finish()
      except KeyError:
        await message.answer('Quantity must be a whole number, write or select below', reply_markup=number_of_currency_for_convertingkbd)
        await Finance_states.to_convert_main.set()
    can_convert = False
  if can_convert == True:
    if users_id_and_selected_language[message.chat.id] == 'ru':
        await mybot.send_message(message.chat.id, 'Получаем данные...')
    elif users_id_and_selected_language[message.chat.id] == 'en':
      await mybot.send_message(message.chat.id, 'Getting data...')
    if currencies_for_currency_converter[message.chat.id] == ' RUB':
      
      if users_id_and_selected_language[message.chat.id] == 'ru':
        await mybot.send_message(
          message.chat.id, 
        f'''
  Доллар — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd(), 1)}</b></ins>
  Евро — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_eur(), 1)}</b></ins>
  Фунт — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_gbp(), 1)}</b></ins>
  Юань — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_cny(), 1)}</b></ins>
  Франк — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_chf(), 1)}</b></ins>
  Иена — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_jpy(), 1)}</b></ins>
  Доллар — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_cad(), 1)}</b></ins>
  Дирхам — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_aed(), 1)}</b></ins>
  Бат — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_thb(), 1)}</b></ins>
  Тенге — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_kzt(), 1)}</b></ins>
  Манат — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_azn(), 1)}</b></ins>
        ''', reply_markup=mainkbd, parse_mode='HTML'
      )
      elif users_id_and_selected_language[message.chat.id] == 'en':
        await mybot.send_message(
          message.chat.id, 
        f'''
  Dollar — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd(), 1)}</b></ins>
  Euro — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_eur(), 1)}</b></ins>
  Pound — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_gbp(), 1)}</b></ins>
  Yuan — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_cny(), 1)}</b></ins>
  Franc — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_chf(), 1)}</b></ins>
  Yen — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_jpy(), 1)}</b></ins>
  Dollar — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_cad(), 1)}</b></ins>
  Dirham — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_aed(), 1)}</b></ins>
  Baht — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_thb(), 1)}</b></ins>
  Tenge — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_kzt(), 1)}</b></ins>
  Manat — <ins><b>{round(number_of_currency_for_converting * end_convert_for_usd() * end_convert_for_azn(), 1)}</b></ins>
        ''', reply_markup=mainkbd, parse_mode='HTML'
      )

И так дальше с 12 валютами


For example, I write the currency from one account to the bot, then the amount, and until he sends a message with rates, he will not respond to another account with the same request (the bot sends a message with rates for 10 seconds, there you need to parse more than 20 sites for it)

Maybe it's because of not using asyncio? Or what could be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-08-04
@FromProg

To summarize the discussion in the comments:
1. Rewrite functions like end_convert_for_<currency>() via aiohttp and call via await
2. Call these functions once, otherwise there are too many requests. work with stored values. In any case, the rate will change in seconds, but there is no point in chasing this accuracy (especially given the presence of round), because the user himself does not know how long he will press the desired button. By that time, the course will already be different. So it's better to request the rates once and work with these values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question