Try using Dialogflow (formerly API.AI) Python SDK #dialogflow

Try using API.AI Python SDK

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.

Install API.AI Python SDK

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

Get CLIENT ACCESS TOKEN

You can check the Token by selecting any project on the console and clicking the gear mark. Make a copy of the Token.

Screen Shot 0029-02-23 at 16.31.50.png

Try out

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.

Python code

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()

Run

python send_text_example.py

response

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>"
}

Reference link

API.AI Docs

Recommended Posts

Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Try using Tweepy [Python2.7]
[Python] Try using Tkinter's canvas
Try using Kubernetes Client -Python-
Try a similar search for Image Search using the Python SDK [Search]
Try the Python LINE Pay SDK
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using Python argparse's action API
Try using the Python Cmd module
Try using Leap Motion in Python
Try using Amazon DynamoDB from Python
Try python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using Python with Google Cloud Functions
Try using Junos On-box Python # 2 Commit Script
Using Azure ML Python SDK 5: Pipeline Basics
Working with OpenStack using the Python SDK
Try to operate Excel using Python (Xlwings)
Try using Junos On-box Python # 1 Op Script
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using the BitFlyer Ligntning API in Python
Python: Try using the UI on Pythonista 3 on iPad
Start using Python
Try using geopandas
Try using Selenium
Try using scipy
Python> try: / except:
Try using the Python web framework Tornado Part 1
Get note information using Evernote SDK for Python 3
Try using pandas.DataFrame
Try using the collections module (ChainMap) of python3
Try using tensorflow ① Build python environment and introduce tensorflow
Try using django-swiftbrowser
Try using matplotlib
Try using the Python web framework Tornado Part 2
Try using tf.metrics
Try using PyODE
Scraping using Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
[Unity (C #), Python] Try running Python code in Unity using IronPython
Try creating a compressed file using Python and zlib
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
(Python) Try to develop a web application using Django
Try projective transformation of images using OpenCV with Python
Try to extract high frequency words using NLTK (python)
Try scraping with Python.
Operate Redmine using Python Redmine
Fibonacci sequence using Python
python try ~ except ~ else
Data analysis using Python 0
Try using virtualenv (virtualenvwrapper)
Data cleaning using Python
[Azure] Try using Azure Functions
Try Debian + Python 3.4 + django1.7 ...
Using Python #external packages