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

如何在.NET Core WebAPI的Dialogflow Webhook中使用Permission Helper请求位置权限

.NET Core WebAPI Dialogflow 位置权限请求 Webhook 示例

嘿,作为刚接触Dialogflow的开发者,处理权限请求确实得留意它特定的响应格式。下面是一个完整的示例,涵盖从触发权限请求到接收用户位置的全流程,你可以直接参考复用:

第一步:定义Dialogflow请求/响应模型类

首先我们需要创建与Dialogflow JSON结构对应的C#模型,这样才能自动绑定请求和构造响应:

using System.Collections.Generic;

namespace DialogflowWebhook.Models
{
    // 接收Dialogflow的请求模型
    public class WebhookRequest
    {
        public QueryResult QueryResult { get; set; }
    }

    public class QueryResult
    {
        public string IntentDisplayName { get; set; }
        public Dictionary<string, string> Parameters { get; set; }
        public bool AllRequiredParamsPresent { get; set; }
    }

    // 返回给Dialogflow的响应模型
    public class WebhookResponse
    {
        public FulfillmentMessages FulfillmentMessages { get; set; }
        public OutputContexts[] OutputContexts { get; set; }
    }

    public class FulfillmentMessages
    {
        public PermissionMessage Permission { get; set; }
    }

    public class PermissionMessage
    {
        public string TextToSpeech { get; set; }
        public PermissionPayload Payload { get; set; }
    }

    public class PermissionPayload
    {
        public string @Type { get; set; } = "google.actions.v2.PermissionValueSpec";
        public string OptContext { get; set; }
        public List<string> Permissions { get; set; }
    }

    public class OutputContexts
    {
        public string Name { get; set; }
        public int LifespanCount { get; set; }
        public Dictionary<string, string> Parameters { get; set; }
    }
}

第二步:编写Webhook控制器

接下来创建API控制器,处理Dialogflow的POST请求,分两种情况:触发权限请求、处理用户授权后的位置信息:

using Microsoft.AspNetCore.Mvc;
using DialogflowWebhook.Models;
using System.Collections.Generic;

namespace DialogflowWebhook.Controllers
{
    [ApiController]
    [Route("api/dialogflow")]
    public class DialogflowWebhookController : ControllerBase
    {
        [HttpPost]
        public IActionResult Post([FromBody] WebhookRequest request)
        {
            // 假设你的意图名称是"RequestLocationPermission"
            if (request.QueryResult.IntentDisplayName == "RequestLocationPermission")
            {
                // 构造权限请求响应
                var response = new WebhookResponse
                {
                    FulfillmentMessages = new FulfillmentMessages
                    {
                        Permission = new PermissionMessage
                        {
                            TextToSpeech = "请问可以获取你的位置信息吗?",
                            Payload = new PermissionPayload
                            {
                                OptContext = "为你提供附近的服务信息",
                                // 可选:DEVICE_PRECISE_LOCATION(精确位置)或 DEVICE_COARSE_LOCATION(粗略位置)
                                Permissions = new List<string> { "DEVICE_PRECISE_LOCATION" }
                            }
                        }
                    }
                };

                return Ok(response);
            }
            // 处理用户授权后的回调(假设意图是"HandleLocationPermissionGranted")
            else if (request.QueryResult.IntentDisplayName == "HandleLocationPermissionGranted")
            {
                // 从参数中提取位置信息
                if (request.QueryResult.Parameters.TryGetValue("geo-city", out string city) &&
                    request.QueryResult.Parameters.TryGetValue("geo-coordinates", out string coordinates))
                {
                    // 这里可以处理位置信息,比如调用业务逻辑
                    var responseMessage = $"已获取你的位置:{city},坐标:{coordinates}";

                    return Ok(new
                    {
                        fulfillmentText = responseMessage
                    });
                }
                else
                {
                    return Ok(new
                    {
                        fulfillmentText = "抱歉,未能获取到你的位置信息,请稍后重试。"
                    });
                }
            }

            // 默认响应
            return Ok(new
            {
                fulfillmentText = "抱歉,我暂时无法处理这个请求。"
            });
        }
    }
}

关键注意事项

  • 确保你在Dialogflow控制台的意图中,已经把google.assistant.permission事件添加到触发事件列表里
  • 权限类型要和你在代码中指定的一致,Dialogflow会根据这个类型向用户请求对应权限
  • 用户授权后,Dialogflow会自动把位置参数(比如geo-city、geo-coordinates)传递到Webhook,你可以直接从QueryResult.Parameters中提取
  • 记得在Dialogflow的Fulfillment设置里,把你的WebAPI地址配置为Webhook URL

内容的提问来源于stack exchange,提问作者Muhammad Zeeshan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:00:06