简体   繁体   中英

How to send messages every 5 seconds by aiogram?

I need a telegram bot which can send "Hello" every 5 seconds. I tried, but my script does totally nothing. Help me, please

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import schedule

bot = Bot(token) #here is my token
dp = Dispatcher(bot)

async def send(message : types.Message):
    await message.answer('Hello')

schedule.every(5).seconds.do(send)

executor.start_polling(dp, skip_updates=True)

from time import sleep

This code calls the send function every 5th second 10 times

for _ in range(10):

   send()
   sleep(5)

This code calls the send function basically forever but still in a five-second interval.

while True:

   send()
   sleep(5)

have you try websockets?

import websockets
import asyncio
async def server(ws:str, path:int):
    while True:
        message = await ws.recv()  # or "Hello"
        print(f'Msg [{message}]')
        message_to_send = main(message)
        schedule.every(5).seconds.do(await ws.send(message_to_send))

server = websockets.serve(server, '127.0.0.1', 5678)
asyncio.get_event_loop().run_until_complete(server)
asyncio.get_event_loop().run_forever()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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