4

我正在为 google home 和 Android 移动助手制作助手应用程序

我在 webhook上使用action-on-google库,这是推荐且方便的

在我的具体情况下,我想从需要 Session Id 的 webhook制作userEntity但我无法在 webhook 上获取 sessionid

根据api.ai 文档,它将 Json 发送到 webhook,如下所示:

{
    "lang": "en", 
    "status": {
        "errorType": "success", 
        "code": 200
    }, 
    "timestamp": "2017-02-09T16:06:01.908Z", 
    "sessionId": "1486656220806"              <<<<<<<<======here is session id
    "result": {
        "parameters": {
            "city": "Rome", 
            "name": "Ana"
        }, 
        "contexts": [], 
        "resolvedQuery": "my name is Ana and I live in Rome", 
        "source": "agent", 
        "score": 1.0, 
        "speech": "", 
        "fulfillment": {
            "messages": [
                {
                    "speech": "Hi Ana! Nice to meet you!", 
                    "type": 0
                }
            ], 
            "speech": "Hi Ana! Nice to meet you!"
        }, 
        "actionIncomplete": false, 
        "action": "greetings", 
        "metadata": {
            "intentId": "9f41ef7c-82fa-42a7-9a30-49a93e2c14d0", 
            "webhookForSlotFillingUsed": "false", 
            "intentName": "greetings", 
            "webhookUsed": "true"
        }
    }, 
    "id": "ab30d214-f4bb-4cdd-ae36-31caac7a6693", 
    "originalRequest": {
        "source": "google", 
        "data": {
            "inputs": [
                {
                    "raw_inputs": [
                        {
                            "query": "my name is Ana and I live in Rome", 
                            "input_type": 2
                        }
                    ], 
                    "intent": "assistant.intent.action.TEXT", 
                    "arguments": [
                        {
                            "text_value": "my name is Ana and I live in Rome", 
                            "raw_text": "my name is Ana and I live in Rome", 
                            "name": "text"
                        }
                    ]
                }
            ], 
            "user": {
                "user_id": "PuQndWs1OMjUYwVJMYqwJv0/KT8satJHAUQGiGPDQ7A="
            }, 
            "conversation": {
                "conversation_id": "1486656220806", 
                "type": 2, 
                "conversation_token": "[]"
            }
        }
    }
}

当然它是正确发送的,但是在 webhook 上,我们将请求对象移交给action-on-google它,它返回一个带有一堆方法的对象,例如,ask等(在此处记录askWithCarouselaskWithList

问题是没有记录对话ID的方法,那么我如何获取该会话ID:

我的源代码供参考:

/index.ts

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import { Request, Response } from "express"; //interfaces
const ActionsSdkApp = require('actions-on-google').ApiAiAssistant;

import db from '../db';

// API.AI Action names
import {
    inputWelcome
} from './actions'

const WELCOME_INTENT = 'input.welcome';

export const webhook = functions.https.onRequest(async (request: Request, response: Response) => {

    console.log("request.body: ", request.body);
    console.log("request.body.sessionId: ", request.body.sessionId);

    const app = new ActionsSdkApp({ request: request, response: response });

    let actionMap = new Map();
    actionMap.set(WELCOME_INTENT, inputWelcome);
    app.handleRequest(actionMap);
})//end of webhook http trigger

/actions/index.ts

import * as request from 'request';

export function inputWelcome(app: any) {

    //I WANT SESSION ID HERE

    console.log("app.conversation(): ", app.conversation());
    console.log("app.AppRequest: ", app.AppRequest);
    console.log("app.AppRequest.conversation: ", app.AppRequest.conversation);

    console.log("app.AppRequest(): ", app.AppRequest());
    console.log("app.AppRequest().conversation: ", app.AppRequest().conversation);


    console.log("app.getUser().accessToken;: ", app.getUser().accessToken)
    const accessToken = app.getUser().accessToken;

// MAKE USER ENTITY WITH THESE DATA:
//    {
//     "sessionId": "current conversation id here",
//     "entities": [
//         {
//             "name": "option1",
//             "entries": [
//                 {
//                     "value": "option1",
//                     "synonyms": [
//                         "first",
//                         "option one"
//                     ]
//                 }
//             ]
//         },
//         {
//             "name": "option2",
//             "entries": [
//                 {
//                     "value": "option2",
//                     "synonyms": [
//                         "second one",
//                         "second option"
//                     ]
//                 }
//             ]
//         },
//         {
//             "name": "option3",
//             "entries": [
//                 {
//                     "value": "option3",
//                     "synonyms": [
//                         "third one",
//                         "third option"
//                     ]
//                 }
//             ]
//         },
//         {
//             "name": "help",
//             "entries": [
//                 {
//                     "value": "help",
//                     "synonyms": [
//                         "help",
//                         "need help",
//                         "ditn't get",
//                         "need support"
//                     ]
//                 }
//             ]
//         }
//     ]
// }
//
// AND THEN ASK THE USER WITH SUGGESTION CHIPS    

    app.ask(app.buildRichResponse()
       .addSimpleResponse({
            speech: `Hi you can start with these things`,
            displayText: `Hi you can start with these things`
        })
        .addSuggestions(['option1', 'option2', 'option3', 'Help'])                        
    )
}
4

1 回答 1

5

支持者说:

你好,

感谢您对 Actions on Google 的关注。如果您将 nodejs 用于您的 webhook,您应该能够使用 request.body.sessionId 访问 sessionId。您可以将该值存储在变量中,并在函数的稍后阶段使用它。

亲切的问候

Jean-Charles,Actions on Google 支持团队

在此处输入图像描述

于 2017-09-27T11:39:16.837 回答