基本上MS的Bot framework 本來就提供了許多Channel 讓我們的bot 可以自由地選擇想要串接的平台
像是 Skype, Facebook messager, Slake 等等
但若是想自己開發一個client端的應用程式去串接Bot
那實際上我們可以透過DirectLine chenel 來達成
目前DirectLine 已經提供完整的REST API 讓我們呼叫
所以要實作一個能夠與Bot對話的client 應用程式其實不難
首先:
需要一組Secret key 用來當作每個conversation的鑰匙
第一步: 開啟對話
使用底下的url
API_ENDPOINT = "https://directline.botframework.com/v3/directline/conversations" |
並且在 Header 裡面加入你的secret key
headers = {'Authorization': 'Bearer '+API_KEY} |
然後再POST
# sending post request and saving response as response object
responds = requests.post(url=API_ENDPOINT, headers=headers)
|
如果有成功開啟一個 conversation的話
就會得到一個 consersation id(當我們收送訊息時都會用到他)
第二步: 送訊息給Bot
使用底下的 url
# defining the api-endpoint
send_api_endpoint = "https://directline.botframework.com/v3/directline/conversations/"+ conversation_id + "/activities"
|
Header 裡面加入你的secret key,
headers = {'Authorization': 'Bearer '+API_KEY, 'Content-Type': 'application/json'} |
以及在body 裡面填入你要送的訊息
data = {
"type": "message",
"from": {
"id": "user1"
},
"text": message
}
|
最後再POST出去
# sending post request and saving response as response object
responds = requests.post(url=send_api_endpoint, data=json.dumps(data), headers=headers)
print "The status code is:%s"%responds.status_code
|
成功的話你會得到JSON的結果如下
{
"id": "4onHfZ1hPGnI0Yk1YjmSdY|0000001"
}
"id": "4onHfZ1hPGnI0Yk1YjmSdY|0000001"
}
第三步:接受訊息
使用底下的 url並指定conversation id
# defining the api-endpoint
receive_api_endpoint = "https://directline.botframework.com/v3/directline/conversations/"
+ conversation_id + "/activities
|
headers = {'Authorization':'Bearer '+API_KEY} |
然後再使用GET
# sending get request and saving response as response object
responds = requests.get(url=receive_api_endpoint, headers=headers)
print "The status code is:%s"%responds.status_code
|
這邊需要注意的是如果url 沒有特別指定watermark的話
get下來的結果會是全部的對話紀錄,
由於每次Bot回傳的結果都會帶一個watermark
{
"activities": [
{
"type": "message",
"channelId": "directline",
"conversation": {
"id": "abc123"
},
"id": "abc123|0000",
"from": {
"id": "user1"
},
"text": "hello"
},
{
"type": "message",
"channelId": "directline",
"conversation": {
"id": "abc123"
},
"id": "abc123|0001",
"from": {
"id": "bot1"
},
"text": "Nice to see you, user1!"
}
],
"watermark": "2"
}
"activities": [
{
"type": "message",
"channelId": "directline",
"conversation": {
"id": "abc123"
},
"id": "abc123|0000",
"from": {
"id": "user1"
},
"text": "hello"
},
{
"type": "message",
"channelId": "directline",
"conversation": {
"id": "abc123"
},
"id": "abc123|0001",
"from": {
"id": "bot1"
},
"text": "Nice to see you, user1!"
}
],
"watermark": "2"
}
而這個watermark簡單來就是一個訊息編號, 會不斷地被更新
所以若我要取得訊息編號2之後的對話紀錄
我們可以在原本url後面加入watermark=2的query string
# defining the api-endpoint
receive_api_endpoint = "https://directline.botframework.com/v3/directline/conversations/"
+ conversation_id + "/activities?watermark="+"2"
|
最後再解析JSON 結果的時候
可以使用BotId 過濾訊息
ACTIVITIES = JSON_MESSAGE["activities"]
BOT_MESSAGES = list()
for active in ACTIVITIES:
if active["from"]["id"] == BOT_ID:
BOT_MESSAGES.append(active)
for bot_message in BOT_MESSAGES:
print bot_message["text"]
|
留言
張貼留言