W
W
why22021-04-01 21:29:02
Python
why2, 2021-04-01 21:29:02

Time.sleep() How to put user value?

Bot in tg. Monitors the dollar exchange rate and sends messages about the dollar (fell, rose). I do not understand how to make the program delay from the user. User input via inline keyboard: 606610aaa9a4a436075531.png
code:

elif message.text == ' Track dollar ':
      #keyboard inline
      markup = types.InlineKeyboardMarkup(row_width = 2)
      kb1 = types.InlineKeyboardButton('10 min', callback_data = '10')
      kb2 = types.InlineKeyboardButton('1 horse', callback_data = '1h')
      kb3 = types.InlineKeyboardButton('5 horse', callback_data = '10h')
      markup.add(kb1, kb2, kb3)
      #send message
      bot.send_message(message.chat.id, ' Now I am tracking the dollar. ⚡️ \n   How long does it take for me to receive notifications❓ \n  \n                                            ', reply_markup = markup)

      xt()
      
      while True:
        
        if rate == False:	
          break
        
        full_page1 = requests.get(url, headers = headers)
        soup1 = BeautifulSoup(full_page1.content, 'html.parser')
        convert1 = soup1.findAll('span', {'class': 'DFlfde', 'class': 'SwHCTb', 'data-precision': 2})
        currency1 = convert1[0].text.replace(',','.')
        cur1 = float(currency1)
        
        
        inline()
        
        if rate == False:	
          break
        

        
        

        #parsing()
        
        full_page = requests.get(url, headers = headers)
        soup = BeautifulSoup(full_page.content, 'html.parser')
        convert = soup.findAll('span', {'class': 'DFlfde', 'class': 'SwHCTb', 'data-precision': 2})
        currency = convert[0].text.replace(',','.')
        cur = float(currency)
    
        
        if cur > cur1:
          bot.send_message(message.chat.id, ' Course increased: ' + str(cur) + ' ')
        
        elif cur < cur1:
          bot.send_message(message.chat.id, ' Course lowered: ' + str(cur) + ' ')
        elif cur == cur1:
          bot.send_message(message.chat.id, '⚖️ Course has not changed ' + str(cur) + ' 〽️')

@bot.callback_query_handler(func = lambda call: True)
def inline (call):
  if call.message:
    if call.data == '10':
      time.sleep(10)


    elif call.data == '1h':
      time.sleep(30)	
    
    elif call.data == '10h':

      
      time.sleep(60)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexa2007, 2021-04-01
@why2

What does it mean

1 horse
5 horse

kb2 = types.InlineKeyboardButton('1 horse', callback_data = '1h')
      kb3 = types.InlineKeyboardButton('5 horse', callback_data = '10h')

def inline (call):
  if call.message:
    if call.data == '10':
      time.sleep(10)

That's how you stop the whole bot. It is necessary to create a database, and write the user, the pause start time, and the length of the pause into it.
And the second cycle to compare the current time with the data from the database. And as a result of a little math, send a message to the user from the database at the right time. And it will work on a computer or VPS. Free hostings will either not run the code, or you will spend a day's limit in a couple of hours.
Here is an example of two threads in a bot:
import telebot
import time
import threading
API_TOKEN = '11111111111111111111111111111111'
class my_bot(telebot.TeleBot):
    def loop_poop(self):
        while True:
            print(time.ctime())
            time.sleep(1)

    def start_action(self):
        thread = threading.Thread(target=self.loop_poop)
        thread.start()

bot = my_bot(token = API_TOKEN, threaded=False)

@bot.message_handler(commands=['start'])
def wellcome(message):
    if message.chat.type == 'private':
        bot.send_message(message.chat.id,'Hello')
bot.start_action()
bot.polling()

S
SashaN69, 2021-04-01
@SashaN69

I recommend using asyncio.sleep so as not to stop the entire bot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question