简体   繁体   English

具有计划任务的 Telegram Bot

[英]Telegram Bot with schedule tasks

I'm just starting to discover how to build a bot with python.我刚刚开始发现如何用 python 构建一个机器人。 I'm trying to send a message at certain time.我正在尝试在特定时间发送消息。 I read a lot of example, I read the documentation regarding modul_schedule function but I can't fix this issue...我阅读了很多示例,阅读了有关 modul_schedule 函数的文档,但我无法解决此问题...

import config
import telebot
import requests
import schedule
import time
from my_parser import parse
from bs4 import BeautifulSoup as BS
bot = telebot.TeleBot(config.token)

r = requests.get('https://example')
html = BS(r.content, 'html.parser')

for el in html.select('#content'):
    t_min = el.select('.temperature .min')[0].text
    t_max = el.select('.temperature .max')[0].text
    min_text = el.select('.wDescription .description')[0].text
    t_test = el.select('.wDescription .description')[0].text

response = requests.get(url='https://example')
data = response.json()
btc_price = f"B: {round(data.get('btc_usd').get('last'), 2)}$"

@bot.message_handler(commands=['start', 'help'])
def main(message):
    bot.send_message(
        message.chat.id, t_min + ', ' + t_max + '\n' + min_text + '\n' + parse() + '\n' + btc_price)


if __name__ == '__main__':
    bot.polling(none_stop=True, interval=0)
    schedule.every(1).seconds.do(main)

    while True:
        schedule.run_pending()
        time.sleep(1)

I would like the bot send message every morning with temperature on to a channel.我希望机器人每天早上都会向频道发送带有温度的消息。 I did not find any clues on how to use the function correctly.我没有找到有关如何正确使用该功能的任何线索。

I use this library .我使用这个库

Example of my code.我的代码示例。

import aioschedule as schedule

async def some_fun():
    pass

async def scheduler():
    schedule.every().day.at("09:00").do(some_fun())
    while True:
        await schedule.run_pending()
        await asyncio.sleep(2)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(scheduler())
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM