使用带Microsoft bot框架的Python创建推送通知/主动消息传递bot的步骤是什么?既然还没有官方文档,我真的不知道从哪里开始。
我已导入以下内容:
from botbuilder.schema import Activity, ActivityTypes, ConversationReference
如何使用它,这是一个非常简单的示例?
答案 0 :(得分:0)
我为您制作了一个基于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。将on_message_activity
中的函数state_management_bot.py
修改为以下代码
async def on_message_activity(self, turn_context: TurnContext):
# Get the state properties from the turn context.
if(turn_context.activity.channel_id != 'notify'):
await turn_context.send_activity("You asid:" + turn_context.activity.text);
else:
await turn_context.send_activity("You get a notify : "+ turn_context.activity.text);
使用postman或restclient进行post调用,以使用json内容触发通知端点:
{
"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>"
}