使用microsoft bot fram创建python主动消息传递

2024-10-02 18:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python和microsoftbot框架创建推送通知/主动消息传递bot的步骤是什么?因为还没有正式文件,我真的不知道从哪里开始。在

我已导入以下内容:

from botbuilder.schema import Activity, ActivityTypes, ConversationReference

如何使用它?一个非常简单的例子是什么?在


Tags: 文件fromimport框架schemabot步骤activity
1条回答
网友
1楼 · 发布于 2024-10-02 18:26:41

我为您制作了一个基于state management sample的示例演示。 请按照setps操作: 1.将下面的代码添加到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修改为下面的代码

^{pr2}$
  1. 在Azure bot emulator上本地运行此示例,单击来自bot的消息并注意会话id和serviceUrl: enter image description here

使用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>"
  }

结果: enter image description hereenter image description here

相关问题 更多 >