1

Here I have a code, it works. But I want it to work only only one time on friday:

import logging
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types.message import ContentType
from aiogram.types import ReplyKeyboardRemove, \
    ReplyKeyboardMarkup, KeyboardButton, \
    InlineKeyboardMarkup, InlineKeyboardButton
import json
from oop import Shop
import requests
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from datetime import datetime, date, time






@dp.message_handler(text_contains='To get prize')
async def get_prize(message: types.Message):
    if datetime.today().weekday() == 4:
        await bot.send_message(message.from_user.id, 'You successfuly got your prize, come next friday to get one more✅')
    else:
        await bot.send_message(message.from_user.id, 'You can get your prize only on fridays.\nToday is no friday :)')

It doesn't work on other days and it works only on friday but it also works a lot of time on friday but I want to make it work only one time on friday. Thanks in advance!

CallMeStag
  • 5,467
  • 1
  • 7
  • 22

1 Answers1

1

@dp.message_handler only works when a new message is sent to the bot.

I recommend some task scheduling tool. I use apscheduler for task scheduling in telegram bots.

...
from apscheduler.schedulers.asyncio import AsyncIOScheduler
...


async def sched():
    for user in user_list: # you can replace user_list with the information in the database

        ...
        await bot.send_message(chat_id=user.user_id, text="some text")
        ...



scheduler = AsyncIOScheduler()
scheduler.add_job(sched, 'interval', weeks=5)
scheduler.start()

I recommend this article and bot to learn apscheduler

You must use the database to use the function only once.