温馨提示:本文翻译自stackoverflow.com,查看原文请点击:objective c - Dialogflow V2 (beta1) SDK for Obj-C
dialogflow objective-c

objective c - 适用于Obj-C的Dialogflow V2(beta1)SDK

发布于 2020-04-09 23:41:54

我想在使用obj-c编写的应用程序中使用dialogflow服务。一段时间以来一直在使用api.ai库,但似乎找不到用于dialogflow v2(beta1)api的obj-c库。我的代理已经升级到v2,但是api.ai内部正在使用/ v1 /端点,因此我需要使用v2beta1特定功能,例如访问知识库。https://cloud.google.com/dialogflow/docs/reference/rpc/google.cloud.dialogflow.v2beta1#queryparameters-Knowledge_base_names)。dialogflow API是标准的REST API,因此我所需要的只是OAuth2.0和REST客户端,但是编码这些听起来像是在重新发明轮子。

请指教。谢谢

查看更多

提问者
Dmitry
被浏览
143
Tlaquetzal 2020-02-09 06:42

我认为没有专门为Dialogflow v2编写的库;但是,该库google-api-objectivec-client-for-rest是Google提供的通用库,可简化使用其Rest API的代码。

该库已更新为可与Dialogflow V2一起使用为了使用它,您需要将Rest API与库中的“查询”(API方法)和“对象”(API类型)进行匹配,这并不困难,因为名称基本相同。

例如,detectIntent方法的全名是:

projects.agent.sessions.detectIntent

在库中,它等效于查询:

GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent

这是一个detectIntent请求的例子:

// Create the service
GTLRDialogflowService *service = [[GTLRDialogflowService alloc] init];

// Create the request object (The JSON payload)
GTLRDialogflow_GoogleCloudDialogflowV2DetectIntentRequest *request =
                     [GTLRDialogflow_GoogleCloudDialogflowV2DetectIntentRequest object];

// Set the information in the request object
request.inputAudio = myInputAudio;
request.outputAudioConfig = myOutputAudioConfig;
request.queryInput = myQueryInput;
request.queryParams = myQueryParams;

// Create a query with session (Path parameter) and the request object
GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent *query =
    [GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent queryWithObject:request
                                                            session:@"session"];

// Create a ticket with a callback to fetch the result
GTLRServiceTicket *ticket =
    [service executeQuery:query
        completionHandler:^(GTLRServiceTicket *callbackTicket,
                            GTLRDialogflow_GoogleCloudDialogflowV2DetectIntentResponse *detectIntentResponse,
                            NSError *callbackError) {
    // This callback block is run when the fetch completes.
    if (callbackError != nil) {
      NSLog(@"Fetch failed: %@", callbackError);
    } else {
      // The response from the agent
      NSLog(@"%@", detectIntentResponse.queryResult.fulfillmentText);
    }
}];

您可以在Wiki库中找到更多信息和样本最后,该库还有一个使用Google Cloud Storage示例代码,说明了其与GCP服务的结合使用。

我认为,如果没有用于Dialogflow V2的特定库,则可能是从头开始实现它之前要尝试的下一件事。

编辑

糟糕,我缺少Dialogflow生成的服务不包含v2beta1的事实。

在这种情况下,还需要执行第一步,即使用Dialogflow v2beta1 DiscoveryDocumentServiceGenerator来创建v2beta1的服务接口。然后,您可以像我之前提到的那样继续工作。