@Filian
Начинаюший Python разроботчик

Почему отправляться одно сообщение вместо двух?

Мне нужно что бы при отправки команды /start бот отправлял все сообщения с значением True. Когда я отправляю команду, то приходить только одно сообщения с BTC, а мне нужно что бы приходило оба и с BTC и с ETH
import telebot
import config

import time
from bs4 import BeautifulSoup
import requests

bot = telebot.TeleBot(config.TOKEN)



@bot.message_handler(commands=['start'])
def send_welcome(message):

    BTCon = True
    ETHon = True


    r_btc = requests.get('https://bcs-express.ru/kotirovki-i-grafiki/btcusd')
    soup_btc = BeautifulSoup(r_btc.content, "lxml")

    if BTCon == True:
        btcST = soup_btc.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
        btc_msg = bot.send_message(message.chat.id, f'*BTC*  {btcST}', parse_mode= 'Markdown')
        nur = 0
        while True:
            nur +=1
            btc = soup_btc.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
            bot.edit_message_text(chat_id = message.chat.id, message_id = btc_msg.message_id, text = f'*BTC*  {btc} - {nur}', parse_mode= 'Markdown')
            time.sleep(1)


    r_eth = requests.get('https://bcs-express.ru/kotirovki-i-grafiki/ethusd')
    soup_eth = BeautifulSoup(r_eth.content, "lxml")

    if ETHon == True:
        ethST = soup_eth.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
        eth_msg = bot.send_message(message.chat.id, f'*ETH*  {ethST}', parse_mode= 'Markdown')
        nur = 0
        while True:
            nur +=1
            eth = soup_eth.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
            bot.edit_message_text(chat_id = message.chat.id, message_id = eth_msg.message_id, text = f'*ETH*  {eth} - {nur}', parse_mode= 'Markdown')
            time.sleep(1)



bot.polling()
  • Вопрос задан
  • 66 просмотров
Решения вопроса 1
@KrimsN
python dev
Когда у тебя начинает работать функция, отправляется сообщение про Биткоин, и там уходит в бесконечный цикл while True
из которого никогда не выходит.

Вот не самое лучшее решение, но оно хотя бы будет работать:
upd: НЕ КОПИРУЙТЕ ЭТО РЕШЕНИЕ как готовое, оно решает проблему в вопросе, но оставляет главную проблему. Согласен, с комментатором, это очень плохое решение. Причины в комментариях к ответу.
import telebot
import config

import time
from bs4 import BeautifulSoup
import requests

bot = telebot.TeleBot(config.TOKEN)



@bot.message_handler(commands=['start'])
def send_welcome(message):

    BTCon = True
    ETHon = True


    r_btc = requests.get('https://bcs-express.ru/kotirovki-i-grafiki/btcusd')
    soup_btc = BeautifulSoup(r_btc.content, "lxml")

    if BTCon:
        btcST = soup_btc.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
        btc_msg = bot.send_message(message.chat.id, f'*BTC*  {btcST}', parse_mode= 'Markdown')
        nur_btc = 0
        
    r_eth = requests.get('https://bcs-express.ru/kotirovki-i-grafiki/ethusd')
    soup_eth = BeautifulSoup(r_eth.content, "lxml")
    
    if ETHon:
        ethST = soup_eth.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
        eth_msg = bot.send_message(message.chat.id, f'*ETH*  {ethST}', parse_mode= 'Markdown')
        nur_eth = 0
     

    while True:
        if BTCon:
            nur_btc +=1
            btc = soup_btc.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
            bot.edit_message_text(chat_id = message.chat.id, message_id = btc_msg.message_id, text = f'*BTC*  {btc} - {nur_btc}', parse_mode= 'Markdown')
            
        if ETHon:
            nur_eth +=1
            eth = soup_eth.find(class_="quote-head__price-value js-quote-head-price js-price-close").text
            bot.edit_message_text(chat_id = message.chat.id, message_id = eth_msg.message_id, text = f'*ETH*  {eth} - {nur_eth}', parse_mode= 'Markdown')
        time.sleep(1)
        	

bot.polling()
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@MEDIOFF
Python Developer
while True:

Хм, почему только одно сообщение отправляется?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы