电报机器人是Invidition循环的堆栈
我制作了一个机器人,需要在无限循环中运行,直到用户告诉他停止。我为我的程序创建了一个简单的概念,该概念遵循相同的任务。当我编写“/start”时,该机器人不会响应其他消息。您对如何解决这个问题有任何想法吗? 先感谢您!
更新:
我的机器人正在从用户那里获取输入消息,并将其用作带有列表的网站的搜索关键字(该代码的一部分完美工作)。但是我需要该循环不断重新加载页面并检查列表。 ,但我希望用户能够通过命令禁用循环
import telebot
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
s=Service("C:\geckodriver.exe")
driver = webdriver.Firefox(service=s)
link_tmp = ""
loope = False
from telebot.async_telebot import AsyncTeleBot
bot = AsyncTeleBot('token')
@bot.message_handler(commands=['start'])
async def start(message):
print("work")
loope = True
loop_time = 0
while loope is True:
link = "https://www.vinted.co.uk/vetements?brand_id[]=326054&order=newest_first&search_text="
print(link)
driver.get(link)
time.sleep(2)
if loop_time == 0:
policy_but = driver.find_element(by=By.XPATH, value = """//*[@id="onetrust-accept-btn-handler"]""")
time.sleep(1)
policy_but.click()
time.sleep(1)
first_item = driver.find_element(by=By.XPATH, value="/html/body/main/div/section/div/div[2]/section/div/div/div[13]/div/div[1]/div/div/div/div[2]/a")
urll= first_item.get_attribute("href")
first_item.click()
first_item_pic = driver.find_element(by=By.XPATH, value="/html/body/main/div[1]/section/div/div[2]/main/div/section/div[1]/figure[1]/a")
picturee = first_item_pic.get_attribute("href")
if loop_time == 0:
link_tmp = urll
await bot.send_message(message.chat.id, f"""New: {urll}
Photo: {picturee}""")
else:
if urll != link_tmp:
await bot.send_message(message.chat.id, f"""New: {urll}
Photo: {picturee}""")
link_tmp = urll
loop_time = loop_time + 1
@bot.message_handler(commands=['stop'])
async def stop(message):
loope = False
await bot.send_message(message.chat.id, "stop")
import asyncio
asyncio.run(bot.polling(non_stop=True)) ```
I made a bot that needs to run in an infinity loop until the user will tell him to stop. I created an easy concept for my program that follows the same tasks. The bot doesn't respond to other messages while I wrote "/start". Do you have any ideas on how to fix that?
Thank you in advance!
Update:
My bot is taking the input message from users and using it as a search keyword for websites with listings(that part of the code works perfectly). But I need that loop to constantly reload the page and check listings. But I want the user to be able to disable the loop by command
import telebot
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
s=Service("C:\geckodriver.exe")
driver = webdriver.Firefox(service=s)
link_tmp = ""
loope = False
from telebot.async_telebot import AsyncTeleBot
bot = AsyncTeleBot('token')
@bot.message_handler(commands=['start'])
async def start(message):
print("work")
loope = True
loop_time = 0
while loope is True:
link = "https://www.vinted.co.uk/vetements?brand_id[]=326054&order=newest_first&search_text="
print(link)
driver.get(link)
time.sleep(2)
if loop_time == 0:
policy_but = driver.find_element(by=By.XPATH, value = """//*[@id="onetrust-accept-btn-handler"]""")
time.sleep(1)
policy_but.click()
time.sleep(1)
first_item = driver.find_element(by=By.XPATH, value="/html/body/main/div/section/div/div[2]/section/div/div/div[13]/div/div[1]/div/div/div/div[2]/a")
urll= first_item.get_attribute("href")
first_item.click()
first_item_pic = driver.find_element(by=By.XPATH, value="/html/body/main/div[1]/section/div/div[2]/main/div/section/div[1]/figure[1]/a")
picturee = first_item_pic.get_attribute("href")
if loop_time == 0:
link_tmp = urll
await bot.send_message(message.chat.id, f"""New: {urll}
Photo: {picturee}""")
else:
if urll != link_tmp:
await bot.send_message(message.chat.id, f"""New: {urll}
Photo: {picturee}""")
link_tmp = urll
loop_time = loop_time + 1
@bot.message_handler(commands=['stop'])
async def stop(message):
loope = False
await bot.send_message(message.chat.id, "stop")
import asyncio
asyncio.run(bot.polling(non_stop=True)) ```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在功能中使用循环,因为它可以防止回复其他消息。
只需检查此示例:
https://github.com/eternnoir/pytelegrambotapi/blob/master/master/examples/asynchronous_telebot/echo_bot.py.py
Don't use loop in your function because it prevents replying to another messages.
just check this example:
https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/asynchronous_telebot/echo_bot.py