F
F
From Prog2021-05-30 23:56:02
Python
From Prog, 2021-05-30 23:56:02

Messages are not displayed in the telegram bot?

The code:

import requests
from bs4 import BeautifulSoup
import time
from parse.dataPy import *
from settings.settings import *
import telebot
from telebot import *

my_bot = telebot.TeleBot(TOKEN)

def create_keyboard():
  keyboard = telebot.types.ReplyKeyboardMarkup(True, True)
  return keyboard

@my_bot.message_handler(commands = ['rate'])
def messages(message):
  loop = True

  my_bot.send_message(message.chat.id, 'Выберите, курс какой валюты вы хотите узнать', reply_markup = create_keyboard().row('Bitcoin', 'Ethereum', 'Tether', 'Cardano', 'Binance Coin', 'XRP', 'Dogecoin', 'USD Coin', 'Polkadot', 'Uniswap'))
  if message.text.lower() == 'bitcoin':
    second = my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_bitcoin, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    while loop:
      time.sleep(int(second))
      my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1057391-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'ethereum':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_ethereum, headers = headers)
    html = BeautifulSoup(source.text, 'lxml') 
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1061443-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'tether':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_tether, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1061453-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'cardano':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_cardano, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1062537-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'binance coin':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_binance_coin, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1061448-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'xrp':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_xrp, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1057392-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'dogecoin':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_dogecoin, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1061477-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'usd Coin':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_usd_coin, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1114630-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'polkadot':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_polkadot, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1165465-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))
  elif message.text.lower() == 'uniswap':
    my_bot.send_message(message.chat.id, 'Выберите, каждую какую секунду вы хотите видеть курс валюты?', reply_markup = create_keyboard().row('1', '2', '3', '5', '10'))
    source = requests.get(url_uniswap, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')	
    my_bot.send_message(message.chat.id, html.find('span', {'class': 'pid-1167226-last', 'id': 'last_last'}).get_text(), reply_markup = create_keyboard().row('stop'))

my_bot.polling(none_stop = True, interval = 0)


I write: /rate
Answer: Choose which currency rate you want to know
I write: Bitcoin
There is no answer, and there are no errors on the command line

But if you do it with the command(@my_bot.message_handler(commands = ['rate'])), but with the text(@my_bot.message_handler(content_types = ['text'])), then everything works fine

Tell me, what is the error, or am I doing something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Shevkunov, 2021-05-31
@FromProg

You only need to process the rate command.
in processing you send a request which currency to use but don't process the response.
You have to create a separate handler that catches all the text
like this:
@my_bot.message_handler(text='bitcoin')
I don't know for sure, I'm dealing with aiogram now and not with telebot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question