如何在webhook上获取会话ID

时间:2017-09-22 15:36:40

标签: session bots actions-on-google api-ai

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

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

在我的具体情况下,我想从webhook中制作userEntity,这需要会话ID,但我无法在webhook上获取会话ID

根据api.ai document它将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并且它返回一个带有一堆方法的对象,如askaskWithCarousel,{{1等等(documented here

问题在于没有方法来记录会话ID 那我怎么得到那个会话ID:

我的源代码供参考:

/index.ts

askWithList

/actions/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

1 个答案:

答案 0 :(得分:4)

支持人员说:

  

您好,

     

感谢您对Google上的操作感兴趣。如果你正在使用   您的webhook的nodejs,您应该能够访问sessionId   与request.body.sessionId。您可以将该值存储在变量中   并在函数的后期使用它。

     

亲切的问候

     

Jean-Charles,Google支持小组的行动

enter image description here