На что заменить MemoryStorage?
import datetime
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
API_TOKEN = ' '
ID_CHANNEL = ' '
# Create a bot instance
bot = Bot(token=API_TOKEN)
# Create a dispatcher instance
storage = MemoryStorage()
dp = Dispatcher(storage=storage)
# Define the channel ID where you want to post the messages
channel_id = ID_CHANNEL
# Define the times of the day when you want to post the messages
post_times = [
datetime.time(15, 55), # 9:00 AM
datetime.time(14, 0), # 2:00 PM
datetime.time(19, 0) # 7:00 PM
]
# Define the message you want to post
message_text = "Hello, this is a scheduled post!"
# Define a function to send the message to the channel
async def send_message_to_channel():
await bot.send_message(channel_id, message_text)
# Define a function to schedule the message posting
async def schedule_message_posting():
while True:
current_time = datetime.datetime.now().time()
if current_time in post_times:
await send_message_to_channel()
await asyncio.sleep(60) # Check every minute
# Start the bot and schedule the message posting
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(schedule_message_posting())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.close()
Ответы (1 шт):
Автор решения: Samikague
→ Ссылка
Можно использовать встроенное в Aiogram 3.x хранилище Redis:
from aiogram.fsm.storage.redis import RedisStorage
from redis import Redis
redis = Redis()
dp = Dispatcher(storage=RedisStorage(redis))