You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dialogflow V2如何获取Request对象?V1代码迁移至V2实现request.body调用

迁移Dialogflow V1 HTTP Webhook到V2(获取Request对象与request.body)

嘿,我来帮你把这段V1代码顺利迁移到V2版本。先梳理核心变化点,再直接上改造好的代码:

关键迁移要点

  • 依赖库更新:V1里的actions-on-google库在V2场景下,更通用的选择是使用dialogflow-fulfillment(如果是专门做Google Assistant集成,也可以用新版actions-on-google)
  • 客户端初始化变更:原来的App类被替换为WebhookClient(来自dialogflow-fulfillment)
  • request对象访问逻辑:request.body的读取方式和V1完全一致,不需要额外调整

改造后的V2代码

// 先确保安装依赖:npm install dialogflow-fulfillment firebase-functions
const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');

exports.example = functions.https.onRequest((request, response) => {
  // 初始化Dialogflow V2的Webhook客户端
  const agent = new WebhookClient({ request, response });

  // 和V1逻辑一致,直接访问request的headers与body
  console.log('Request headers: ' + JSON.stringify(request.headers));
  console.log('Request body: ' + JSON.stringify(request.body));

  // 后续如果需要处理意图逻辑,可以在这里添加agent.handleIntent()等相关代码
});

补充说明

如果你的场景是专门针对Google Assistant的集成,也可以用新版actions-on-google库,代码示例如下:

const functions = require('firebase-functions');
const { Dialogflow } = require('actions-on-google');

exports.example = functions.https.onRequest((request, response) => {
  const app = new Dialogflow({ request, response });

  console.log('Request headers: ' + JSON.stringify(request.headers));
  console.log('Request body: ' + JSON.stringify(request.body));

  // 意图处理逻辑...
});

两种方式下,你都可以像V1时期一样直接操作request对象,读取body里的意图参数、用户上下文等内容。

内容的提问来源于stack exchange,提问作者Yatheesha S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:58:49