A
A
Almaz Akimov2021-11-06 18:44:16
Python
Almaz Akimov, 2021-11-06 18:44:16

How to run code at specific date and time?

There is a date in unixtime when to run

import datetime
timestamp = 1339521878

Found a code that works, but it executes every n times
import logging
import asyncio
from datetime import datetime

from aiogram import Bot, Dispatcher, executor, types

# Initalialization API token for work with Telegram Bot
API_TOKEN = "BOT TOKEN HERE"

# Configure logging
logging.basicConfig(level=logging.INFO)

# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)

@dp.message_handler()
async def echo(message: types.Message):
    await message.answer(message.text)

async def scheduled(wait_for):
  while True:
    await asyncio.sleep(wait_for)

    print('Время пришло!')

if __name__ == '__main__':
  loop = asyncio.get_event_loop()
  loop.create_task(scheduled(10)) # поставим 10 секунд, в качестве теста
  executor.start_polling(dp, skip_updates=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-06
@sergey-gornostaev

Just use cron and don't use crutches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question