2024-10-02 18:26:41 发布
网友
使用Python和microsoftbot框架创建推送通知/主动消息传递bot的步骤是什么?因为还没有正式文件,我真的不知道从哪里开始。在
我已导入以下内容:
from botbuilder.schema import Activity, ActivityTypes, ConversationReference
如何使用它?一个非常简单的例子是什么?在
我为您制作了一个基于state management sample的示例演示。 请按照setps操作: 1.将下面的代码添加到app.py:
app.py
@APP.route("/api/notify", methods=["POST"]) def notify(): if request.headers["Content-Type"] == "application/json": body = request.json else: return Response(status=415) activity = Activity().deserialize(body) auth_header = ( request.headers["Authorization"] if "Authorization" in request.headers else "" ) async def aux_func(turn_context): await BOT.on_turn(turn_context) try: task = LOOP.create_task( ADAPTER.process_activity(activity, auth_header, aux_func) ) LOOP.run_until_complete(task) return Response(status=201) except Exception as exception: raise exception
2.将state_management_bot.py中的函数on_message_activity修改为下面的代码
state_management_bot.py
on_message_activity
使用postman或restclient执行post调用以触发具有json内容的notify端点:
{ "text": "this is a notify sent from outside ", "textFormat": "plain", "type": "message", "channelId": "notify", "from": { "id": "backend", "name": "xxxxx", "role": "xxxxxx" }, "conversation": { "id": "<conversation id>" }, "recipient": { "id": "", "name": "bot", "role": "bot" }, "serviceUrl": "<service URL>" }
结果:
我为您制作了一个基于state management sample的示例演示。 请按照setps操作: 1.将下面的代码添加到
app.py
:2.将
^{pr2}$state_management_bot.py
中的函数on_message_activity
修改为下面的代码使用postman或restclient执行post调用以触发具有json内容的notify端点:
结果:
相关问题 更多 >
编程相关推荐