我需要一个电报机器人,它可以发送"你好"每5秒。我试过了,但我的剧本完全没有效果。请帮帮我
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)
从时间导入睡眠
此代码每隔5秒调用发送功能10次
for _ in range(10):
send()
sleep(5)
这段代码基本上永远调用send函数,但仍以5秒为间隔。
while True:
send()
sleep(5)
你尝试过websocket吗?
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()