dialogflow-cpp-client icon indicating copy to clipboard operation
dialogflow-cpp-client copied to clipboard

text_request_example do not work

Open Sfinx opened this issue 8 years ago • 1 comments

a) sessionId is not set b) parsing contexts is buggy

diff --git a/apiai/src/query/QueryRequest.cpp b/apiai/src/query/QueryRequest.cpp
index a284416..d331f96 100644
--- a/apiai/src/query/QueryRequest.cpp
+++ b/apiai/src/query/QueryRequest.cpp
@@ -86,9 +86,9 @@ response::Response QueryRequest::serialize(const string &response) {
             }
 
             shared_ptr<Metadata> metadata_pointer(new Metadata());
-
+            
+            auto metadata_json = jsonObject(result_json, "metadata");
             try {
-                auto metadata_json = jsonObject(result_json, "metadata");
                 string intentId = jsonString(metadata_json, "intentId");
                 string intentName = jsonString(metadata_json, "intentName");
 
@@ -105,10 +105,17 @@ response::Response QueryRequest::serialize(const string &response) {
             vector <Context> contexts;
 
             try {
-                auto contexts_array_json = jsonArray(result_json, "contexts");
+               auto contexts_array_json = jsonArray(metadata_json, "contexts");
 
                 for (int i = 0; i < cJSON_GetArraySize(contexts_array_json); i++) {
                     auto context_json = cJSON_GetArrayItem(contexts_array_json, i);
+                    map<string, shared_ptr<Element>> params;
+
+                    if (context_json->type != cJSON_Object) {
+                      auto name = std::string(context_json->valuestring);
+                      contexts.push_back(Context(name, -1, params));
+                      continue;
+                    }
 
                     auto name = jsonString(context_json, "name");
                     auto lifespan = -1;
@@ -117,7 +124,6 @@ response::Response QueryRequest::serialize(const string &response) {
                         lifespan = jsonInt(context_json, "lifespan");
                     } catch(...) {}
 
-                    map<string, shared_ptr<Element>> params;
 
                     try {
                         auto json_params = jsonObject(context_json, "parameters");
diff --git a/apiai/src/query/TextQueryRequest.cpp b/apiai/src/query/TextQueryRequest.cpp
index 2a8108b..5bc5408 100644
--- a/apiai/src/query/TextQueryRequest.cpp
+++ b/apiai/src/query/TextQueryRequest.cpp
@@ -81,6 +81,7 @@ Response TextQueryRequest::perform() {
     }
 
     cJSON_AddItemToObject(root, "entities", json_entities);
+    cJSON_AddItemToObject(root, "sessionId", cJSON_CreateString(this>getParameters().getSessionId().c_str()));
 
     auto json = cJSON_Print(root);
 
diff --git a/apiai/src/query/cJSONUtils.cpp b/apiai/src/query/cJSONUtils.cpp
index 873d396..068d432 100644
--- a/apiai/src/query/cJSONUtils.cpp
+++ b/apiai/src/query/cJSONUtils.cpp
@@ -14,6 +14,9 @@ cJSON *jsonObject(cJSON *object, const char *key) {
 
 cJSON *jsonArray(cJSON *object, const char *key) {
     auto other = cJSON_GetObjectItem(object, key);
+    if (!other) {
+        throw ai::JSONException::MissingKey(key);
+    }
     if (other->type != cJSON_Array) {
         throw ai::JSONException::TypeMismatch(key, "Array");
     }

Sfinx avatar Jun 08 '17 08:06 Sfinx

sessionId doesn't seem to get set in the text_request_example, even after adding a unique sessionId in the example source code and compiling. If your C++ API is no longer supported, please say so in the README file so that people don't waste their time on it.

On a related note, I had to edit the submodule URL in my repo for git to find and clone the dependent JSON repo.

kaushik1618 avatar Jul 24 '17 15:07 kaushik1618