适用于 Obj-C 的 Dialogflow V2 (beta1) SDK
Dialogflow V2 (beta1) SDK for Obj-C
我想在使用 obj-c 编写的应用程序中使用 dialogflow 服务。已经使用 api.ai 库一段时间了,但似乎找不到用于 dialogflow v2(beta1) apis 的 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 APIs 的代码。
此库已更新,可与 Dialogflow V2 一起使用。为了使用它,您需要将 Rest API、"Queries"(API 方法)和 "Objects"(API 类型)匹配在图书馆,这并不难,因为名字基本相同。
例如detectIntent方法全称是:
projects.agent.sessions.detectIntent
在库中,相当于Query:
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. Finally, the library also has a sample code using Google Cloud Storage 中找到更多信息和示例,其中说明了它与 GCP 服务的用法。
我认为如果没有 Dialogflow V2 的特定库,这可能是从头开始实施之前要尝试的下一步。
编辑
糟糕,我错过了为 Dialogflow 生成的服务不包含 v2beta1 的事实。
在这种情况下,需要额外的第一步,即使用 Dialogflow v2beta1 DiscoveryDocument and the ServiceGenerator,为 v2beta1 创建服务接口。然后你就可以像我之前提到的那样继续工作了。
按照@Tlaquetzal 的例子,我最终做了如下的事情
在 pod 文件中
pod 'GoogleAPIClientForREST'
pod 'JWT'
使用 ServiceGenerator and Discovery Document 如上所述,生成了一组 DialogFlow v2beta1 类。命令行
./ServiceGenerator --outputDir . --verbose --gtlrFrameworkName GTLR --addServiceNameDir yes --guessFormattedNames https://dialogflow.googleapis.com/$discovery/rest?version=v2beta1
并将它们添加到项目中。
#import "DialogflowV2Beta1/GTLRDialogflow.h"
下一步是生成 JWT 令牌。这个库我用过JSON Web Token implementation in Objective-C。我想使用服务帐户连接。
NSInteger unixtime = [[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] integerValue];
NSInteger expires = unixtime + 3600; //expire in one hour
NSString *iat = [NSString stringWithFormat:@"%ld", unixtime];
NSString *exp = [NSString stringWithFormat:@"%ld", expires];
NSDictionary *payload = @{
@"iss": @"<YOUR-SERVICE-ACCOUNT-EMAIL>",
@"sub": @"<YOUR-SERVICE-ACCOUNT-EMAIL>",
@"aud": @"https://dialogflow.googleapis.com/",
@"iat": iat,
@"exp": exp
};
NSDictionary *headers = @{
@"alg" : @"RS256",
@"typ" : @"JWT",
@"kid" : @"<KID FROM YOUR SERVICE ACCOUNT FILE>"
};
NSString *algorithmName = @"RS256";
NSData *privateKeySecretData = [[[NSDataAsset alloc] initWithName:@"<IOS-ASSET-NAME-JSON-SERVICE-ACCOUNT-FILE>"] data];
NSString *passphraseForPrivateKey = @"<PASSWORD-FOR-PRIVATE-KEY-IN-CERT-JSON>";
JWTBuilder *builder = [JWTBuilder encodePayload:payload].headers(headers).secretData(privateKeySecretData).privateKeyCertificatePassphrase(passphraseForPrivateKey).algorithmName(algorithmName);
NSString *token = builder.encode;
// check error
if (builder.jwtError == nil) {
JwtToken *jwtToken = [[JwtToken alloc] initWithToken:token expires:expires];
success(jwtToken);
}
else {
// error occurred.
MSLog(@"ERROR. jwtError = %@", builder.jwtError);
failure(builder.jwtError);
}
令牌生成后,可以使用一个小时(或您上面指定的时间)。
要调用 dialogflow,您需要定义项目路径。要为调用创建项目路径,请将代码附加到您的唯一会话标识符下方。 Session就像是dialogflow的对话,所以不同的用户应该使用不同的session id
#define PROJECTPATH @"projects/<YOUR-PROJECT-NAME>/agent/sessions/"
进行 dialogflow 调用
// Create the service
GTLRDialogflowService *service = [[GTLRDialogflowService alloc] init];
//authorise with token
service.additionalHTTPHeaders = @{
@"Authorization" : [NSString stringWithFormat:@"Bearer %@", self.getToken.token]
};
// Create the request object (The JSON payload)
GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentRequest *request = [GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentRequest object];
//create query
GTLRDialogflow_GoogleCloudDialogflowV2beta1QueryInput *queryInput = [GTLRDialogflow_GoogleCloudDialogflowV2beta1QueryInput object];
//text query
GTLRDialogflow_GoogleCloudDialogflowV2beta1TextInput *userText = [GTLRDialogflow_GoogleCloudDialogflowV2beta1TextInput object];
userText.text = question;
userText.languageCode = LANGUAGE;
queryInput.text = @"YOUR QUESTION TO dialogflow agent"; //userText;
// Set the information in the request object
//request.inputAudio = myInputAudio;
//request.outputAudioConfig = myOutputAudioConfig;
request.queryInput = queryInput;
//request.queryParams = myQueryParams;
//Create API project path with session
NSString *pathAndSession = [NSString stringWithFormat:@"%@%@", PROJECTPATH, [self getSession]];
// Create a query with session (Path parameter) and the request object
GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent *query = [GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent queryWithObject:request session:pathAndSession];
// Create a ticket with a callback to fetch the result
// GTLRServiceTicket *ticket =
[service executeQuery:query
completionHandler:^(GTLRServiceTicket *callbackTicket, GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentResponse *detectIntentResponse, NSError *callbackError) {
// This callback block is run when the fetch completes.
if (callbackError != nil) {
NSLog(@"error");
NSLog(@"Fetch failed: %@", callbackError);
//TODO: Register failure with analytics
failure( callbackError );
}
else {
// NSLog(@"Success");
// The response from the agent
// NSLog(@"%@", detectIntentResponse.queryResult.fulfillmentText);
NSString *response = detectIntentResponse.queryResult.fulfillmentText;
success( response );
}
}];
这是一个基本实现,但可以正常工作并且适合演示。
祝你好运
我想在使用 obj-c 编写的应用程序中使用 dialogflow 服务。已经使用 api.ai 库一段时间了,但似乎找不到用于 dialogflow v2(beta1) apis 的 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 APIs 的代码。
此库已更新,可与 Dialogflow V2 一起使用。为了使用它,您需要将 Rest API、"Queries"(API 方法)和 "Objects"(API 类型)匹配在图书馆,这并不难,因为名字基本相同。
例如detectIntent方法全称是:
projects.agent.sessions.detectIntent
在库中,相当于Query:
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. Finally, the library also has a sample code using Google Cloud Storage 中找到更多信息和示例,其中说明了它与 GCP 服务的用法。
我认为如果没有 Dialogflow V2 的特定库,这可能是从头开始实施之前要尝试的下一步。
编辑
糟糕,我错过了为 Dialogflow 生成的服务不包含 v2beta1 的事实。
在这种情况下,需要额外的第一步,即使用 Dialogflow v2beta1 DiscoveryDocument and the ServiceGenerator,为 v2beta1 创建服务接口。然后你就可以像我之前提到的那样继续工作了。
按照@Tlaquetzal 的例子,我最终做了如下的事情
在 pod 文件中
pod 'GoogleAPIClientForREST'
pod 'JWT'
使用 ServiceGenerator and Discovery Document 如上所述,生成了一组 DialogFlow v2beta1 类。命令行
./ServiceGenerator --outputDir . --verbose --gtlrFrameworkName GTLR --addServiceNameDir yes --guessFormattedNames https://dialogflow.googleapis.com/$discovery/rest?version=v2beta1
并将它们添加到项目中。
#import "DialogflowV2Beta1/GTLRDialogflow.h"
下一步是生成 JWT 令牌。这个库我用过JSON Web Token implementation in Objective-C。我想使用服务帐户连接。
NSInteger unixtime = [[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] integerValue];
NSInteger expires = unixtime + 3600; //expire in one hour
NSString *iat = [NSString stringWithFormat:@"%ld", unixtime];
NSString *exp = [NSString stringWithFormat:@"%ld", expires];
NSDictionary *payload = @{
@"iss": @"<YOUR-SERVICE-ACCOUNT-EMAIL>",
@"sub": @"<YOUR-SERVICE-ACCOUNT-EMAIL>",
@"aud": @"https://dialogflow.googleapis.com/",
@"iat": iat,
@"exp": exp
};
NSDictionary *headers = @{
@"alg" : @"RS256",
@"typ" : @"JWT",
@"kid" : @"<KID FROM YOUR SERVICE ACCOUNT FILE>"
};
NSString *algorithmName = @"RS256";
NSData *privateKeySecretData = [[[NSDataAsset alloc] initWithName:@"<IOS-ASSET-NAME-JSON-SERVICE-ACCOUNT-FILE>"] data];
NSString *passphraseForPrivateKey = @"<PASSWORD-FOR-PRIVATE-KEY-IN-CERT-JSON>";
JWTBuilder *builder = [JWTBuilder encodePayload:payload].headers(headers).secretData(privateKeySecretData).privateKeyCertificatePassphrase(passphraseForPrivateKey).algorithmName(algorithmName);
NSString *token = builder.encode;
// check error
if (builder.jwtError == nil) {
JwtToken *jwtToken = [[JwtToken alloc] initWithToken:token expires:expires];
success(jwtToken);
}
else {
// error occurred.
MSLog(@"ERROR. jwtError = %@", builder.jwtError);
failure(builder.jwtError);
}
令牌生成后,可以使用一个小时(或您上面指定的时间)。
要调用 dialogflow,您需要定义项目路径。要为调用创建项目路径,请将代码附加到您的唯一会话标识符下方。 Session就像是dialogflow的对话,所以不同的用户应该使用不同的session id
#define PROJECTPATH @"projects/<YOUR-PROJECT-NAME>/agent/sessions/"
进行 dialogflow 调用
// Create the service
GTLRDialogflowService *service = [[GTLRDialogflowService alloc] init];
//authorise with token
service.additionalHTTPHeaders = @{
@"Authorization" : [NSString stringWithFormat:@"Bearer %@", self.getToken.token]
};
// Create the request object (The JSON payload)
GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentRequest *request = [GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentRequest object];
//create query
GTLRDialogflow_GoogleCloudDialogflowV2beta1QueryInput *queryInput = [GTLRDialogflow_GoogleCloudDialogflowV2beta1QueryInput object];
//text query
GTLRDialogflow_GoogleCloudDialogflowV2beta1TextInput *userText = [GTLRDialogflow_GoogleCloudDialogflowV2beta1TextInput object];
userText.text = question;
userText.languageCode = LANGUAGE;
queryInput.text = @"YOUR QUESTION TO dialogflow agent"; //userText;
// Set the information in the request object
//request.inputAudio = myInputAudio;
//request.outputAudioConfig = myOutputAudioConfig;
request.queryInput = queryInput;
//request.queryParams = myQueryParams;
//Create API project path with session
NSString *pathAndSession = [NSString stringWithFormat:@"%@%@", PROJECTPATH, [self getSession]];
// Create a query with session (Path parameter) and the request object
GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent *query = [GTLRDialogflowQuery_ProjectsAgentSessionsDetectIntent queryWithObject:request session:pathAndSession];
// Create a ticket with a callback to fetch the result
// GTLRServiceTicket *ticket =
[service executeQuery:query
completionHandler:^(GTLRServiceTicket *callbackTicket, GTLRDialogflow_GoogleCloudDialogflowV2beta1DetectIntentResponse *detectIntentResponse, NSError *callbackError) {
// This callback block is run when the fetch completes.
if (callbackError != nil) {
NSLog(@"error");
NSLog(@"Fetch failed: %@", callbackError);
//TODO: Register failure with analytics
failure( callbackError );
}
else {
// NSLog(@"Success");
// The response from the agent
// NSLog(@"%@", detectIntentResponse.queryResult.fulfillmentText);
NSString *response = detectIntentResponse.queryResult.fulfillmentText;
success( response );
}
}];
这是一个基本实现,但可以正常工作并且适合演示。 祝你好运