微软bot framework简介
TRANSCRIPT
![Page 1: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/1.jpg)
简介•微软 Bot Framework是一套用于开发和部署 bot服务的框架,通过该框架可以开发对话型机器人服务,并接入到多种应用中。• Bot Framework框架包括五个组成部分: - Bot Builder SDK (开发 bot服务,目前提供 .Net & Node) - Bot Connector (连接 bot服务和最终用户) - Developer Portal (供开发者注册、发布和管理 bot服务) - Bot Directory (已发布的所有 bot服务目录) - Emulator (供开发者本地调试 bot服务的模拟器)
![Page 2: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/2.jpg)
简介
Bot
Bot Connector
User
Channels
![Page 3: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/3.jpg)
Hello world (Node version)
var restify = require('restify');var builder = require('botbuilder');
var server = restify.createServer();server.listen(process.env.port || process.env.PORT || 3978, function () { console.log('%s listening to %s', server.name, server.url); });var connector = new builder.ChatConnector({ appId: process.env.MICROSOFT_APP_ID, appPassword: process.env.MICROSOFT_APP_PASSWORD});var bot = new builder.UniversalBot(connector);server.post('/api/messages', connector.listen());
bot.dialog('/', function (session) { session.send("Hello World");});
Connector
建立同 Bot Connector的连接,实现鉴权以及用户和 Bot之间的消息传递,目前 SDK提供三种:ConsoleConnector、 ChatConnector和CallConnector。BotBot服务的大脑,负责管理用户和 Bot之间的所有会话,需要使用 connector初始化,目前 SDK提供两种:UniversalBot和 UniversalCallBot。Dialog实现 Bot的具体会话逻辑,类似于网站的路由,可以定义多个分别实现不同的会话逻辑
![Page 4: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/4.jpg)
鉴权第一步 : 向MSA登录服务发起请求POST /d6d49420-f39b-4df7-a1dc-d59a935871db/oauth2/v2.0/token HTTP/1.1grant_type=client_credentialsclient_id=<YOUR MICROSOFT APP ID>client_secret=<YOUR MICROSOFT APP PASSWORD>scope=https://api.botframework.com/.default第二步 : 获取 tokenHTTP/1.1 200 OK{"token_type":"Bearer","expires_in":3600,"ext_expires_in":3600,"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJZb3UgZm91bmQgdGhlIG1hcmJsZSBpbiB0aGUgb2F0bWVhbCEiLCJuYW1lIjoiQm90IEZyYW1ld29yayJ9.JPyDDC5yKmHfOS7Gz2jjEhOPvZ6iStYFu9XlkZDc7wc"}
第三步 : 向 Bot Connector发送消息POST /v3/conversations/12345/activities HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJZb3UgZm91bmQgdGhlIG1hcmJsZSBpbiB0aGUgb2F0bWVhbCEiLCJuYW1lIjoiQm90IEZyYW1ld29yayJ9.JPyDDC5yKmHfOS7Gz2jjEhOPvZ6iStYFu9XlkZDc7wc... (JSON-serialized activity message follows)
![Page 5: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/5.jpg)
鉴权第一步 : 下载 OpenId元数据文档GET /v1/.well-known/openidconfiguration HTTP/1.1{ "issuer":"https://api.botframework.com","authorization_endpoint":"https://invalid.botframework.com/","jwks_uri":"https://api.aps.skype.com/v1/keys","id_token_signing_alg_values_supported":["RSA256"],"token_endpoint_auth_methods_supported":["private_key_jwt"]}
第二步 : 下载有效的签名秘钥列表GET /v1/keys HTTP/1.1
第三步 : 验证 JWT token
![Page 6: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/6.jpg)
开启对话Bot向 Bot Connector发起创建会话请求POST https://api.botframework.com/v3/conversations HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1Ni...Content-Type: application/json{ "bot": { "id": "12345678", "name": "bot's name" }, "isGroup": false, "members": [ { "id": "1234abcd", "name": "recipient's name" } ], "topicName": "News Alert"}
会话创建成功之后 Bot Connector返回会话 id{ "id": "abcd1234"}
通常会话由用户发起, Bot回复用户的消息即可,但某种场景下可能需要Bot主动发起会话。会话可以有多种形式,既可以是一对一的私有回话,也可以是多对多的群组会话。
![Page 7: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/7.jpg)
收发消息• 会话就是在用户与 Bot之间发送的消息序列,在 Bot
Framework中每条消息都是一个 Activity对象。
消息类型 描述contactRelationUpdate 用户将 bot添加到联系人列表或者
移除出联系人列表conversationUpdate 用户加入或离开会话,会话名称修
改deleteUserData 用户要求 bot删除自己的相关信息message 普通的消息ping 发给 bot用来验证服务可接入typing 表示对方正在输入属性 属性说明
conversation 消息所属会话的信息,包括会话 id和会话名称from 发送消息方的信息locale 消息的语言环境recipient 接收消息方的信息replyToId 消息回复的 activity idtype 消息类型
![Page 8: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/8.jpg)
POST https://api.botframework.com/v3/conversations/abcd1234/activities/5d5cdc723 HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1Ni...Content-Type: application/json
{ "type": "message", "from": { "id": "12345678", "name": "Pepper's News Feed" }, "conversation": { "id": "abcd1234", "name": "Convo1" }, "recipient": { "id": "1234abcd", "name": "SteveW" }, "text": "My bot's reply", "replyToId": "5d5cdc723"}
收发消息
回复消息发送的 URL路径为 https://api.botframework.com/v3/conversations/{conversationId}/activities/{activityId}
发起消息发送的 URL路径为 https://api.botframework.com/v3/conversations/{conversationId}/activities
![Page 9: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/9.jpg)
消息中携带附件POST https://api.botframework.com/v3/conversations/abcd1234/activities/5d5cdc723 HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1Ni...Content-Type: application/json{ "type": "message", "from": { "id": "12345678", "name": "sender's name" }, "conversation": { "id": "abcd1234", "name": "conversation's name" }, "recipient": { "id": "1234abcd", "name": "recipient's name" }, "text": "Here's a pic of the duck I was telling you about.", "attachments": [ { "contentType": "image/png", "contentUrl": http://aka.ms/Fo983c } ], "replyToId": "5d5cdc723"}
除了文本文字,消息也可以携带其他类型的附件,如图片、音频、视频和 Rich Card。
![Page 10: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/10.jpg)
消息中携带应用数据{ "type": "message", "from": { "id": "[email protected]", "name": "My bot" }, "conversation": { "id": "abcd1234", "name": "conversation's name" }, "recipient": { "id": "[email protected]", "name": "John Doe" }, "channelData": { "htmlBody" : "<html><body style=\"font-family: Calibri; font-size: 11pt;\">email message goes here</body></html>", "subject":"email subject goes here", "importance":"high" }, "replyToId": "5d5cdc723"}
某些对接应用无法直接使用 text和附件,此时需要携带应用可使用的数据
![Page 11: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/11.jpg)
保存用户状态数据• Bot Framework提供了三种存储来保存用户的状态数据 - User Data Store (与会话无关的用户数据) - Conversation Store (与用户无关的会话数据) - Private Conversation Store (会话中的用户数据)• 每种存储可用的最大空间均为 32KB,使用 ETag实现对数据修改的并发控制User Data Store
https://api.botframework.com/v3/botstate/{channelId}/users/{userId}Conversation Storehttps://api.botframework.com/v3/botstate/{channelId}/conversations/{conversationId}Private Conversation Storehttps://api.botframework.com/v3/botstate/{channelId}/conversations/{conversationId}/users/{userId}
![Page 12: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/12.jpg)
保存用户状态数据POST https://api.botframework.com/v3/botstate/abcd1234/users/12345678 HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1Ni...Content-Type: application/json
{ "data": [ { "trail": "Lake Serene", "miles": 8.2, "difficulty": "Difficult", }, { "trail": "Rainbow Falls", "miles": 6.3, "difficulty": "Moderate", } ], "eTag": "a1b2c3d4"}
GET https://api.botframework.com/v3/botstate/abcd1234/users/12345678 HTTP/1.1Authorization: Bearer eyJhbGciOiJIUzI1Ni...Content-Type: application/json
{ "data": [ { "trail": "Lake Serene", "miles": 8.2, "difficulty": "Difficult", } ], "eTag": "xyz12345"}
![Page 13: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/13.jpg)
Dialog组织方式• Waterfall: Bot驱动会话的常用组织方式, bot始终处于一种为用户提供信息或者问用户问题等待回答的状态。 - 为 dialog处理器提供一组函数,框架对于每个会话维护一个 dialog栈, bot根据栈中的内容来判断用户的回复内容路由到哪个 dialog。• Closure:只为 dialog处理器提供一个函数,类似于单步的
waterfall。• Dialog Object:为 dialog处理提供一个 IntentDialog,用于判断用户意图,然后根据不同的意图执行不同的处理逻辑。• SimpleDialog:作为 waterfall的补充,用来处理 waterfall无法覆盖的场景。
![Page 14: 微软Bot framework简介](https://reader035.vdocuments.pub/reader035/viewer/2022072110/58e543d91a28ab3a468b4da5/html5/thumbnails/14.jpg)
IntentDialog
•意图对话提供两种意图匹配方法:正则表达式和意图识别器,前者在 Bot本地实现意图匹配,后者基于云端实现意图识别和实体抽取。•微软提供的云端意图识别的服务为 LUIS(https://www.luis.ai/)。•目前 Bot Framework提供的智能服务涉及:
Vision、 Speech、 Language、 Knowledge和 Search。