Miles Chen
Miles Chen

Reputation: 1

Actions on Google Fulfillment Endpoint for python

How do I create functions for fulfillment using python?

The documentation for building fulfillments uses Node.js with Firebase Cloud Function for fulfillment hosting. Actions SDK is used here.

The sample code below simply invokes the app and mimics the first speech said by the user.

'use strict';

const ActionsSdkApp = require('actions-on-google').ActionsSdkApp;
const functions = require('firebase-functions');

exports.analizeInput= (req, res) => {
    const app = new ActionsSdkApp({request: req, response: res});

    // Create functions to handle requests here

    function handleMainIntent() {
        let inputPrompt = app.buildInputPrompt(false, 'This is <app-name>');
        app.ask(inputPrompt);
    }
    function handleTextIntent() {
        app.tell("you said, " + app.getRawInput());
    }

    let actionMap = new Map();
    actionMap.set(app.StandardIntents.MAIN, handleMainIntent);
    actionMap.set(app.StandardIntents.TEXT, handleTextIntent);
    app.handleRequest(actionMap);
} 

The code uses Node.js language in the fullments endpoint. Other languages can be used as well. However, there are no available resources on how to setup a fulfillment endpoint using other languages. I would like to know how to create a simple one like above using python.

from flask import Flask
from flask import jsonify
app = Flask(__name__)

@app.route('/')
def hello_world():
//I do not know where to use this JSON (this is the app.ask() in Node.js)
data = {
    "conversationToken": "",
    "expectUserResponse": true,
    "expectedInputs": [{
        "inputPrompt": {
            "richInitialPrompt": {
                "items": [{
                    "simpleResponse": {
                        "textToSpeech": "Howdy! I can tell you fun facts about almost any number, like 42. What do you have in mind?",
                        "displayText": "Howdy! I can tell you fun facts about almost any number. What do you have in mind?"
                    }
                }],
                "suggestions": []
            }
        },
        "possibleIntents": [{
            "intent": "actions.intent.TEXT"
        }]
    }]
}
return jsonify(data)

if __name__ == '__main__':
    app.run()

This is the simple REST endpoint that I set up and deployed using Heroku. As far as I understand, JSON is used if the language used is not Node.js as seen in the "Sample Code" section here. However, I'm stuck on what to do such as handling requests, giving responses, and setting intents relating to Google Assistant. I also have no starting idea where to put the JSON suggested in the documentation. I don't even know how to start in the first place. I would appreciate so much if you can give a head start.

Thank you

Upvotes: 0

Views: 1049

Answers (4)

Kirk
Kirk

Reputation: 480

I just found a working solution: just follow this documentation: Conversational Actions - JSON webhook payload - Request and Response, this is the JSON fulfillment API.

Example:

{
    "prompt": {
        "firstSimple": {
            "speech": "Hello, this works!",
            "text": "Hello, this works!"
        }
    }
}

I guess the JSONs in Actions SDK documentation are used to interact with the SDK? Anyway, those just don't work.
The dialogflow fulfillment API doesn't work, either. I used dialogflow API before. They are just different.
This documentation is kinda hard to find, taking me 3 hours to find, but still better than to replicate the SDK myself.

Upvotes: 1

ldemay
ldemay

Reputation: 518

https://dialogflow.com/docs/fulfillment

You need to send back a JSON like

{
    "speech": "Hello!",
    "displayText": "Hello!"
}

Upvotes: 0

Nick Felker
Nick Felker

Reputation: 11970

Basically you are building an API which receives values in a JSON request. You will then need to parse that and return with a JSON response. As William stated, creating a version in Node would let you see the requests. You can also use the Actions on Google console simulator to see requests.

Checking out the documentation of Actions SDK would let you see the raw requests and responses. There's not a good sample for Python that I know of, so you may want to build a sample that already exists for Node.JS and then port it.

Upvotes: 0

William DePalo
William DePalo

Reputation: 625

Go to console.actions.google.com to create a project. For now choose the SDK option rather than Dialog Flow or the other NLU whose name I forget.

Assuming you are familiar with Node you can go here

https://github.com/unclewill/parrot

and get my "parrot" sample. It is a trivial Action which simply repeats - or parrots - what you speak. Its only saving grace is that it is about 50 lines. Install it with the package manager - npm.

Then download ngrok

https://ngrok.com/download

this tool will create a "tunnel" by means of a reverse proxy from the web to your development machine. Once downloaded, at the command line type

ngrok http 8080

This will in effect get your machine a publicly addressable and secure-ish (i.e. https) address which the Actions on Google platform will use to communicate with your Python endpoint eventually and my parrot sample now.

Look at the address it assigned to your machine and then edit the action package (file action.json) so that the https address you find there is replaced with ngrok's.

Look at the files update.cmd and test.cmd. The former is used to inform Google of the new address of your Action project. The latter to put it into test mode. Edit the project name in both to match the project name you got at the Actions console.

Run the file update.cmd. On Windows you can just run the file. On 'nix first change the mode to executable with chmod +x or you can just copy the text and paste into the command line. Go to the Actions console and tap the 'Test' button to open the simulator. Go back to the command line and run the file test.cmd.

Run the sample in Node with

node app

If all goes well, you have an Action running locally and a tunnel to it from the web. Type some text in the simulator. The action should "parrot" it back to you.

Now look at the shell/terminal/command window where you have ngrok running. It will list an endpoint on localhost (or maybe 127.0.0.1 I forget). Type that address in the browser and you will see the JSON payloads from and to the Action.

Your mission is to replicate that in Python.

(I don't do Python but I am surprised if no one has ported the client library)

Upvotes: 0

Related Questions