【问题标题】:how to get session id on webhook如何在 webhook 上获取会话 ID
【发布时间】:2023-03-07 16:44:01
【问题描述】:

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

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

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

根据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,它返回一个带有一堆方法的对象,例如 askaskWithCarouselaskWithList 等 (documented here )

问题是没有方法可以记录对话 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'])                        
    )
}

【问题讨论】:

  • 你在使用 actions-on-google node js 客户端库吗?
  • 是的,使用 Google 上的操作 npmjs.com/package/actions-on-google
  • 您可能遇到API.AI sessionID来自GAC会话ID的问题,可能您是通过API.AI UI而不是GAC模拟器进行测试。
  • 我正在使用 GAC 模拟器进行测试
  • 目前使用客户端库时无法获取sesssionId

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


【解决方案1】:

支持者说:

嗨,

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

亲切的问候

Jean-Charles,Google 支持团队的 Actions

【讨论】:

  • 是的,但你使用的是 TypeScript 而不是通用的 Javascript 库
  • 我已经做到了,它在打字稿中为我工作
  • 但仍在寻找干净的解决方案
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2022-10-02
  • 2012-09-04
  • 2013-05-24
  • 1970-01-01
相关资源
最近更新 更多