skip to Main Content

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)

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. 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()
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search