本文介绍了如何从Java中的DialogFlow获取参数(Android Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从DialogFlow到我的Android应用程序获取参数.
我尝试使用 getQueryResult().getParameters().getFieldsMap()
但结果如下.

I need to get parameters from DialogFlow to my Android app.
I tried using getQueryResult().getParameters().getFieldsMap()
but the result is the following.

{type=list_value {
      values {
        string_value: "pizza"
      }
    }
    , ristorante=string_value: ""
    }

我只想获取字符串值"pizza",而不是整个FieldMap.我已经看过主题,但是它没有帮助我,因为我不知道什么是protobuf,而且看起来有点复杂.

I would like to get just the string value "pizza" and not the entire FieldMap.I have already seen this topic, but it didn't help me, because I don't know what protobuf is and seems a bit complicated.

是否有一种简单的方法来获取参数的值?

Is there a simple way to get a parameter's value?

推荐答案

如果使用Dialogflowv2

If you use Dialogflowv2

public String getParameter(GoogleCloudDialogflowV2WebhookRequest request, String parameterName) {
  try {
        GoogleCloudDialogflowV2QueryResult queryResult = request.getQueryResult();
        Map<String, Object> parameters = queryResult.getParameters();
        String parameter = (String) parameters.get(parameterName);
        if(parameter != null && !parameter.equals("")) {
            return parameter;
        }
    } catch (ClassCastException  e) {
            logger.error("Error");
        }
    return null;
}

如果您使用GoogleActions

If you use GoogleActions

public String getParameter(ActionRequest request, String parameterName) {
        try {
            Map<String, Object> parameters =  request.getWebhookRequest().getQueryResult().getParameters();
            String parameter = (String) parameters.get(parameterName);
            if(parameter != null && !parameter.equals("")) {
                return parameter;
            }
        } catch (ClassCastException  e) {
            logger.error("Error");
        }
        return null;
    }

这篇关于如何从Java中的DialogFlow获取参数(Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:17