我正在建立一个食品订购机器人。我已经在火堆中保存了我的食品类别(即起子,餐,鱼,肉,意大利面等),每种食品类别都可以有多种食品。
这是我在firebase中的食物项目结构
{
"-KauXk2E4ma1u70X-h4g" : {
"food_category" : "fish",
"food_desc" : "Zander filets in pastry made with beer, tartar sauce",
"food_like" : true,
"food_name" : "Homemade fish sticks",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/FISH/Homemade fish sticks.jpg"
},
"-KauY4B0pQDgZ_sF92Df" : {
"food_category" : "meat",
"food_desc" : "Rindsfiletmedaillons (160 g), Balsamicosauce, fried potatoes",
"food_like" : true,
"food_name" : "Medallion of beef with balsamic-sauce",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/MEAT/Medallion of beef with balsamic-sauce.jpg"
},
"-KauYG4BcYT5EY36TQM7" : {
"food_category" : "fish",
"food_desc" : "",
"food_like" : true,
"food_name" : "Gilt head fillets",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/FISH/Gilt head fillets.jpg"
},
"-KauYHAaRDF6ZgAFy7U_" : {
"food_category" : "meat",
"food_desc" : "",
"food_like" : true,
"food_name" : "Münsterhof Food - Burger",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/MEAT/Münsterhof Food - Burger.jpg"
},
"-KaucBQaWncdZOc5kat2" : {
"food_category" : "pasta",
"food_desc" : "Rigatoni (type of pasta), tomato, pepper, garlic",
"food_like" : true,
"food_name" : "Arrabbiata",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PASTA/Arrabbiata.jpg"
},
"-KaucGDalMgMRwfCGbvy" : {
"food_category" : "pasta",
"food_desc" : "Tagliatelle (type of pasta), tomato, beef",
"food_like" : true,
"food_name" : "Bolognese",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PASTA/Bolognese.jpg"
},
"-KaucisapOUvgKF2xcjg" : {
"food_category" : "pasta",
"food_desc" : "Rigatoni (type of pasta), tomato, basil",
"food_like" : true,
"food_name" : "Napoli",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PASTA/Napoli.jpg"
},
"-KaucsqbjLVKL3oAv4y3" : {
"food_category" : "pizza",
"food_desc" : "Homemade pizza dough, tomato sauce, mozzarella cheese, ham, free-range egg, pesto",
"food_like" : true,
"food_name" : "Calzone",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PIZZA/Calzone.jpg"
},
"-KaucwQb6NNgkUCg8bZD" : {
"food_category" : "pizza",
"food_desc" : "Homemade pizza dough, tomato sauce, mozzarella cheese, basil",
"food_like" : true,
"food_name" : "Margherita",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PIZZA/Margherita.jpg"
},
"-Kaud7xE6JmQtqhLiLQP" : {
"food_category" : "pizza",
"food_desc" : "Homemade pizza dough, tomato sauce, mozzarella cheese, artichokes, hot pepper, mushrooms, ham, olives, oregano",
"food_like" : true,
"food_name" : "Quattro stagioni",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/PIZZA/Quattro stagioni.jpg"
},
"-Kaue7Wzluk-wy9xys5n" : {
"food_category" : "starters",
"food_desc" : "beef-carpaccio, parmesan, olive oil",
"food_like" : true,
"food_name" : "Beef carpaccio",
"food_price" : 45,
"food_url" : "https://i.scaley.io/768x687/lebijou.io/_jamesapp/food/airhomeCHZH3/STARTERS/Beef carpaccio.jpg"
}
}
我想要实现的是将我的食物项目(在食物类别下)从firebase同步到api.ai实体。
我在api.ai手动完成,如下所示 http://prntscr.com/fhpme6
现在想通过代码(nodejs)完成所有这些工作 我知道api.ai提供了与实体一起玩的api https://docs.api.ai/docs/entities(我用过它们) 我可以从firebase中提取食物项目并将数据同步到api.ai实体。但有一个问题。我无法覆盖和/或删除我的意图中已经使用过的实体。
以下是我为FoodSync.js
var util = require('util');
var exec = require('child_process').exec;
var apiai_developer_access_code = "e764e24fa51e4d93bdc1e6bde92d07d8"
class FoodSync {
syncFood(firebaseRef, apiAiRef, callback){
//Retrieves a list of all entities for the agent.
var all_entities = 'curl -k -H "Authorization: Bearer '+apiai_developer_access_code+'" "https://api.api.ai/v1/entities?v=20150910"'
var food_category_id = "";
var all_entities_array = [];
var all_food_cat_names = [];
var all_food_cat_entities_id = [];
var that = this;
that.execCommand(all_entities, function(error, stdout, stderr){
var jsonResponse = JSON.parse(stdout)
all_entities_array = jsonResponse;
for(var i = 0; i < jsonResponse.length; i++) {
var entity = jsonResponse[i];
if(entity.name == "food-categories"){
food_category_id = entity.id;
break;
}
}
console.log('food_category_id: ' + food_category_id);
if(food_category_id == ""){
return;
}
var food_category_entity_url = that.getEntityUrlWithID(food_category_id);
console.log('food_category_entity_url: ' + food_category_entity_url);
that.execCommand(food_category_entity_url, function(error, stdout, stderr){
var jsonResponse = JSON.parse(stdout)
console.log('food_category_entity: ' + JSON.stringify(jsonResponse));
var food_cat_all_entities = jsonResponse.entries;
console.log("food_cat_all_entities : "+ food_cat_all_entities.length);
for(var i = 0; i < food_cat_all_entities.length; i++) {
var food_cat_entity = food_cat_all_entities[i];
var food_cat_entity_name = food_cat_entity.value.replace('@', '')
console.log("food_cat_entity_name : "+ food_cat_entity_name);
console.log("all_entities_array.length : "+ all_entities_array.length);
for(var j = 0; j < all_entities_array.length; j++){
var entity_check = all_entities_array[j];
if(entity_check.name == food_cat_entity_name){
all_food_cat_entities_id.push(entity_check.id);
}
}
// Here I got all food-entities id.
console.log('all_food_cat_entities_ids: ' + all_food_cat_entities_id);
// Here I am calling the DELETE entity first (then Recreate), Problem is my entity is used in My some of Intents, So upon delete I am getting the error,
// {
// "id": "09e2c21d-b4e3-4a34-a774-eab781c957e1",
// "timestamp": "2017-06-09T10:01:19.294Z",
// "lang": "en",
// "status": {
// "code": 400,
// "errorType": "bad_request",
// "errorDetails": "Some entity names are in use: food-cat-fish"
// }
// }
}
})
// if(error !== null){
// console.log('exec error: ' + error);
// callback()
// }
})
}
getEntityUrlWithID(entitiy_id){
// Retrieves the specified entity.
var entities = 'curl -k -H "Authorization: Bearer '+apiai_developer_access_code+'" "https://api.api.ai/v1/entities/'+entitiy_id+'?v=20150910"'
return entities;
}
execCommand(command, callback){
console.log('execCommand: called');
exec(command, function(error, stdout, stderr){
//console.log('stdout: ' + stdout);
//console.log('stderr: ' + stderr);
if(error !== null){
console.log('exec error: ' + error);
}
callback(error, stdout, stderr)
});
}
}
module.exports = FoodSync