我想在使用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客户端,但是编码这些听起来像是在重新发明轮子。
请指教。谢谢
我认为没有专门为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 DiscoveryDocument和ServiceGenerator来创建v2beta1的服务接口。然后,您可以像我之前提到的那样继续工作。
谢谢@Tlaquetzal-这非常有帮助。我在认证方面有些挣扎。我需要使用服务帐户,所有示例都使用用户身份代替。您能指出正确的方向吗?
@Dmitry我找不到使过程更容易的库;但是,请查看此答案和本教程。两者都在解释使用JWT进行身份验证的相同方法,这似乎是最简单的方法。
另外,我没有使用swift深入检查此选项,但是如果您觉得有用,我将在此处保留。