【发布时间】:2023-03-18 19:30:01
【问题描述】:
这可能是我的错,但我无法使“run_repeating”方法起作用。我查看了官方 Github 存储库中的示例,发现 timerbot.py。我试图在我的代码中遵循同样的想法,但我失败了。我的意思是,timerbot 可以工作,但我做错了什么,我不知道这在我的代码中不起作用。而且我知道,timerbot 使用“run_once”而不是“run_repeating”,但文档指出pretty similar syntax for both。
顺便说一句,我的机器人应该是使用 Selenium 的亚马逊产品的价格跟踪器(那部分没问题)。机器人获取 url,然后跟踪产品,直到价格变得更便宜并通过消息通知用户。
这基本上是代码(重要的部分):
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters, ConversationHandler
from selenium import webdriver, common
import time, re
#Variables
token = open('note_token_telegram', 'r').read()
driver_path = open('note_driver','r').read()
bin_path = open('note_bin','r').read()
#Track
def track(update: Update, context: CallbackContext) -> None:
data = context.user_data
url = context.args[0]
chat_id = update.message.chat_id
if re.match(r'(https:\/\/www\.amazon\.).*', url):
print('track running')
#Scrape
driver_options = webdriver.FirefoxOptions()
driver_options.add_argument('--headless')
driver_options.binary_location = bin_path.strip()
driver = webdriver.Firefox(executable_path=driver_path.strip(), options=driver_options)
driver.get(url)
try:
try:
price = driver.find_element_by_css_selector('#priceblock_ourprice')
except common.exceptions.NoSuchElementException:
try:
price = driver.find_element_by_css_selector('#priceblock_saleprice')
except:
price = driver.find_element_by_css_selector('#priceblock_dealprice')
name = driver.find_element_by_css_selector('#productTitle')
except:
update.message.reply_text('Are you sure this is an Amazon product page? '+url)
driver.close()
data[name.text] = {'url': url, 'price' : price.text}
print(data.items())
driver.close()
print(chat_id)
context.job_queue.run_repeating(notification, 30, context=chat_id, name=str(chat_id))
update.message.reply_text("Good! We are now tracking one more Product!")
def notification(update, context):
#The Script
job = context.job
print('notification running')
print(type(job.context))
print(job.context)
print(context.user_data.items())
#Scrape
driver_options = webdriver.FirefoxOptions()
driver_options.add_argument('--headless')
driver_options.binary_location = bin_path.strip()
driver = webdriver.Firefox(executable_path=driver_path.strip(), options=driver_options)
for i in context.user_data.items():
driver.get(i[1]['url'])
try:
try:
new_price = driver.find_element_by_css_selector('#priceblock_ourprice')
except common.exceptions.NoSuchElementException:
try:
new_price = driver.find_element_by_css_selector('#priceblock_saleprice')
except:
new_price = driver.find_element_by_css_selector('#priceblock_dealprice')
except:
context.bot.send_message(job.context, text='Are you sure this is an Amazon product page?'+i['url'])
#Notification
if '-' in new_price.text and float(new_price.text[1:4]) < float(i[1]['price']):
context.bot.send_message(job.context, text=f"It's on your budget! \n{i[0]} \n {price.text} \n {i[1]['url']}")
driver.close()
elif float(new_price.text[1:9]) < float(i[1]['price']):
context.bot.send_message(job.context, text=f"It's on your budget! \n{i[0]} \n {price.text} \n {i[1]['url']}")
driver.close()
else:
driver.close()
#Main Function
def main():
updater = Updater(token.strip())
#Commands Instantiation
updater.dispatcher.add_handler(CommandHandler('track', track))
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, help))
#Main loop methods
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
这是我收到的错误消息:
raise ValueError('The following arguments have not been supplied: %s' %
ValueError: The following arguments have not been supplied: context
它表示值错误,但我已将上下文和所有内容粘贴到通知功能的正确位置,不是吗?我只是遵循 timerbot.py 的相同原则(至少我尝试过......)。
【问题讨论】:
标签: python telegram telegram-bot