I would like to touch API.AI from Python code. If you don't know what API.AI is, please refer to here. I would like to proceed with this sample based on the project created in here.
The SDK project is published on Github. It is recommended because it contains a simple sample.
It can also be installed with pip.
pip install apiai
You can check the Token by selecting any project on the console and clicking the gear mark. Make a copy of the Token.
Here I will send "Miso ramen please" to the Intent to order the ramen created.
Put the Token you got earlier in CLIENT ACCESS TOKEN.
send_text_example.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os.path
import sys
try:
import apiai
except ImportError:
sys.path.append(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)
)
import apiai
CLIENT_ACCESS_TOKEN = 'CLIENT ACCESS TOKEN'
def main():
ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)
request = ai.text_request()
request.lang = 'ja' # optional, default value equal 'en'
request.session_id = '<SESSION ID, UNIQUE FOR EACH USER>'
request.query = u'I'd like miso ramen.'
response = request.getresponse()
print (response.read())
if __name__ == '__main__':
main()
python send_text_example.py
response
{
"id": "",
"timestamp": "2017-02-23T07:37:13.582Z",
"lang": "ja",
"result": {
"source": "agent",
"resolvedQuery": "I'd like miso ramen.",
"action": "order",
"actionIncomplete": false,
"parameters": {
"RamenMenu": "miso_ramen"
},
"contexts": [],
"metadata": {
"intentId": "",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Order"
},
"fulfillment": {
"speech": "Understood.",
"messages": [
{
"type": 0,
"speech": "Understood."
}
]
},
"score": 1.0
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "<SESSION_ID,_UNIQUE_FOR_EACH_USER>"
}
Recommended Posts