Answer the question
In order to leave comments, you need to log in
How to perform a certain action every few hours in a telegram bot?
I am writing a bot in python aiogram (asynchronous bot). It is necessary that the bot updates the price every 2 hours.
I'm thinking of making my own loop and passing it when the bot starts, after somehow adding a task to it, and putting an endless loop with a timer in it.
But then, probably, there may be problems with access to data to which a simultaneous request for reading and writing will follow. Yes, I've made up my mind))
Answer the question
In order to leave comments, you need to log in
import asyncio
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
DELAY = 7200
bot = Bot(token='BOT TOKEN HERE')
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
async def update_price():
...
def repeat(coro, loop):
asyncio.ensure_future(coro(), loop=loop)
loop.call_later(DELAY, repeat, coro, loop)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.call_later(DELAY, repeat, update_price, loop)
executor.start_polling(dp, loop=loop)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question