我需要一个电报机器人,它可以每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)发布于 2022-01-27 17:20:08
从时间开始进入睡眠
此代码每5秒钟调用发送函数10次。
for _ in range(10):
send()
sleep(5)这段代码基本上永远调用发送函数,但仍然在5秒钟的间隔内调用。
while True:
send()
sleep(5)发布于 2022-01-27 17:33:52
你试过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()https://stackoverflow.com/questions/70882699
复制相似问题