上次我們講了Teams Bot開發的概述,講了Azure Bot Service,Bot Framework SDK和我們自己的bot服務的概念,這篇文章就帶大家看看Azure Bot Service和我們的bot是如何發生關系的。
我們自己開發的bot服務實際上就是一個api service,需要提供一個支持 POST 請求的endpoint。然后把這個endpoint的url注冊到Azure Bot Service,這樣當有人和你的bot進行聊天的時候,這個endpoint就會被Azure Bot Service所調用。
我們來看一個簡單例子:用戶在模擬器里對bot發送一個”hi”消息,然后bot回復一個”You said: hi”消息。整個流程如下:
?
- 當用戶準備開始和bot進行聊天的時候,azure bot service就會向我們的bot endpoint上 POST 一個關于 conversationUpdate 的HTTP請求,告訴我們的bot,有一個用戶準備要和bot聊天了。下面是Bot Emulator,相當于Azure Bot Service傳遞給我們bot endpoint的http request body
{"channelId": "emulator","conversation": {"id": "5d2a6fa0-a8c4-11ea-9482-fbaa8563d339|livechat"},"from": {"id": "f2dc881f-5c55-4f44-a50a-0ecdb4717950","name": "User","role": "user"},"id": "5d354510-a8c4-11ea-9482-fbaa8563d339","localTimestamp": "2020-06-07T23:39:51+10:00","locale": "en-US","membersAdded": [{"id": "5d2a4890-a8c4-11ea-9762-2d13acd3a3d5","name": "Bot"},{"id": "f2dc881f-5c55-4f44-a50a-0ecdb4717950","name": "User"}],"membersRemoved": [],"recipient": {"id": "5d2a4890-a8c4-11ea-9762-2d13acd3a3d5","name": "Bot","role": "bot"},"serviceUrl": "https://762eb0b511e7.ngrok.io","timestamp": "2020-06-07T13:39:51.905Z","type": "conversationUpdate"
}
可以看到channelId說明是用模擬器聊天平臺發出來的信息,from
用戶,type
是conversationUpdate
,具體內容是有兩個新增加的成員(membersAdded
),分別是User
和Bot
。大家需要注意一個conversation id,這個id是針對這次對話的,這次對話里可以你一句,我一句,每一句的id不同,但是他們的conversation id是一樣的。
- 當用戶發送一個 “hi” 文字內容給bot,這時候我們的bot endpoint會收到另一個POST 請求,內容如下:
{"channelData": {"clientActivityID": "1591537203426cjob1jjckwm","clientTimestamp": "2020-06-07T13:40:03.426Z"},"channelId": "emulator","conversation": {"id": "5d2a6fa0-a8c4-11ea-9482-fbaa8563d339|livechat"},"entities": [{"requiresBotState": true,"supportsListening": true,"supportsTts": true,"type": "ClientCapabilities"}],"from": {"id": "f2dc881f-5c55-4f44-a50a-0ecdb4717950","name": "User","role": "user"},"id": "6414c2c0-a8c4-11ea-9482-fbaa8563d339","localTimestamp": "2020-06-07T23:40:03+10:00","locale": "en-US","recipient": {"id": "5d2a4890-a8c4-11ea-9762-2d13acd3a3d5","name": "Bot","role": "bot"},"serviceUrl": "https://762eb0b511e7.ngrok.io","text": "hi","textFormat": "plain","timestamp": "2020-06-07T13:40:03.436Z","type": "message"
}
可以看到這此的type
是message
,說明是一條消息,消息文字格式是純文本plain
,文字內容text
是hi
。
對照我們上面的conversation id,是不是發現這條的conversation id是一樣的,說明這條消息和上一條是在同一個對話里進行的。
這里有一個serviceUrl
,這個是我們Azure bot service模擬器的url,如果你的bot要回復用戶一條消息,那這個消息就是發送到這個url上。
- 我們的bot收到上面這條用戶發來的消息后,回復用戶一條”You said: hi”的文字消息,bot就組建一個http request,POST 到上面這個service Url上,請求的body也是json格式,內容如下:
{"attachments": [],"channelId": "emulator","conversation": {"id": "5d2a6fa0-a8c4-11ea-9482-fbaa8563d339|livechat"},"entities": [],"from": {"id": "5d2a4890-a8c4-11ea-9762-2d13acd3a3d5","name": "Bot","role": "bot"},"id": "6504a100-a8c4-11ea-9482-fbaa8563d339","inputHint": "acceptingInput","localTimestamp": "2020-06-07T23:40:05+10:00","locale": "en-US","recipient": {"id": "f2dc881f-5c55-4f44-a50a-0ecdb4717950","role": "user"},"replyToId": "6414c2c0-a8c4-11ea-9482-fbaa8563d339","serviceUrl": "https://762eb0b511e7.ngrok.io","speak": "You said: hi","text": "You said: hi","timestamp": "2020-06-07T13:40:05.008Z","type": "message"
}
可以看到conversation id沒有變化,需要注意的是replyToId
,它表示bot回復的”You said: hi”,是針對用戶前面發的那條”hi”消息的回復。所以這個replyToId
的值就是前面那個消息的id
。
大家看了這個流程了對應的json,是不是對bot的運作有一個具體的感覺了? :D