R
R
Roman2021-08-16 13:23:39
Python
Roman, 2021-08-16 13:23:39

Why send one message instead of two?

I need that when sending the /start command, the bot sends all messages with the True value. When I send a command, then only one message will come from BTC, but I need both BTC and ETH to come

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()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KrimsN, 2021-08-16
@Filian

When the function starts working for you, a message about Bitcoin is sent, and there it goes into an endless while True loop
from which it never exits.
Here is not the best solution, but at least it will work:
upd: DO NOT COPY THIS SOLUTION as a ready-made, it solves the problem in the question, but leaves the main problem . I agree with the commentator, this is a very bad decision. Reasons in the comments to the answer.

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()

S
Stefan, 2021-08-16
@MEDIOFF

while True:
Hmm, why is only one message sent?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question