gpt4 book ai didi

python - Microsoft Teams - 机器人主程序中的无限循环

转载 作者:行者123 更新时间:2023-12-03 03:52:06 25 4
gpt4 key购买 nike

我开发了一个 Teams 机器人,它在启动时运行无限循环,以便向用户发送主动消息。

async def job():
i = 60
await asyncio.sleep(i)
await _create_file_of_the_day()
await _send_question_of_the_day()
await job()

if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
t1 = loop.create_task(job())
t2 = loop.create_task((web.run_app(APP, host="localhost", port=CONFIG.PORT)))
asyncio.gather(t1,t2)
loop.run_forever()
except Exception as error:
raise error

这在本地使用python app.py工作,但是当我将机器人上传到azure并在线测试时,无限循环没有启动,所以我发现不可能主动向用户发送消息。这两种方法称为工作。第一个在 azure 上创建一个文件,第二个使用该文件的内容创建两个问题,这些问题应主动发送给 channel 的所有成员。有谁知道如何帮助我?我需要根据时间延迟向用户发送消息,而不是响应他们的操作。这种安排不是恒定的,例如我只想在工作日而不是假期发送消息。

感谢大家

更新

我只是在评论中尝试了第二种解决方案,但结果总是相同的。在本地,应用程序行为正确,但在 Azure 云上,应该循环的例程似乎没有被触发。

async def job():
i = 60
await asyncio.sleep(i)
await _create_file_of_the_day()
await _send_question_of_the_day()
await job()

async def main():

runner = aiohttp.web.AppRunner(APP)
await runner.setup()
site = web.TCPSite(runner, host='localhost', port=CONFIG.PORT)
await site.start()
asyncio.create_task(job())
while True:
await asyncio.sleep(3600)


if __name__ == "__main__":
try:
asyncio.run(main())

except Exception as error:
raise error

最佳答案

由于无法使用循环来安排消息,因此通过使用 Azure Function 类型计时器触发器解决了该问题。该函数调用机器人内部创建的端点,每次调用该端点时都会执行 job() 方法。这两个链接可能有助于了解如何创建端点以及如何查询它。在代码示例中,查询是通过单击链接来给出的,该链接可以轻松地替换为代码中的 GET 请求。

Proactive message sample with endpoint creation

Article with explanation of proactive messages

示例代码采用 python 编写,但通过浏览 git 文件夹,您可以找到其他编程语言的代码。

对于 Azure Function 的开发,我发现 YouTube 上的这三个视频系列非常有用。

Useful video on development of timer trigger function in python

关于python - Microsoft Teams - 机器人主程序中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66285428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com