Public skills of SUSI.AI are stored in susi_skill_cms repository. A private skill is different from a public skill. It can be viewed, edited and deleted only by the user of who created it. The purpose of private skill is that this acts as a chatbot. All the information of the bot like design, configuration etc is stored within the private skill itself. In order to show the lists of chatbots a user has created to him/her, we need to fetch these lists from the server. The API for fetching private skills is in ListSkillService.java servlet. This servlet is used for both the purposes for fetching public skills and private skills.
How are bots fetched?
All the private skills are stored in data/chatbot/chatbot.json location in the SUSI server. Hence, we can fetch these skills from here.
The API call to fetch private skills is:
https://api.susi.ai/cms/getSkillList.json?private=1&access_token=accessTokenHere
The API call has two parameters:
- private = 1: This parameter tells the servlet that we’re asking for private skills because same API is used for fetching public skills as well.
- access_token : We have to pass the access-token in order to authenticate the user and access the chatbots of the specific user because the chatbots are stored according to user id of the users in chatbot.json. To get the user id, we need access-token.
For fetching the chatbots from chatbot.json, we firstly authenticate the user. Then we check if the user has any chatbots or not. If the user has chatbots then we loop through chatbot.json to find the chatbots of user by using user id. After getting chatbots details, we simply present it in json structure. You can easily understand this through the following code snippets. For authenticating:
String userId = null; String privateSkill = call.get("private", null); if (call.get("access_token", null) != null) { ClientCredential credential = new ClientCredential(ClientCredential.Type.access_token, call.get("access_token", null)); Authentication authentication = DAO.getAuthentication(credential); // check if access_token is valid if (authentication.getIdentity() != null) { ClientIdentity identity = authentication.getIdentity(); userId = identity.getUuid(); } }
You can see that the parameter private is stored in privateSkill. We check if privateSkill is not null followed by checking if the user id provided is valid or not. When both the checks are successful, we create a JSON object in which we store all the bots. To do this, we loop through the chatbot.json file to find the user id provided in API call. If the user id doesn’t exist in chatbot.json file then a message “User has no chatbots.” is displayed. If the user id is found then we again loop through all the chatbots and put their name as the value of key “name”, language as the value of key “language” and group as the value of key “group”. All these key value pairs for the chatbots are pushed in an array and finally that array is added to the JSON object. This JSON is then received as response of the API call. The following code will demonstrate this:
if(privateSkill != null) { if(userId != null) { JsonTray chatbot = DAO.chatbot; JSONObject result = new JSONObject(); JSONObject userObject = new JSONObject(); JSONArray botDetailsArray = new JSONArray(); JSONArray chatbotArray = new JSONArray(); for(String user_id : chatbot.keys()) { if(user_id.equals(userId)) { userObject = chatbot.getJSONObject(user_id); Iterator chatbotDetails = userObject.keys(); List<String> chatbotDetailsKeysList = new ArrayList<String>(); while(chatbotDetails.hasNext()) { String key = (String) chatbotDetails.next(); chatbotDetailsKeysList.add(key); } for(String chatbot_name : chatbotDetailsKeysList) { chatbotArray = userObject.getJSONArray(chatbot_name); for(int i=0; i<chatbotArray.length(); i++) { String name = chatbotArray.getJSONObject(i).get("name").toString(); String language = chatbotArray.getJSONObject(i).get("language").toString(); String group = chatbotArray.getJSONObject(i).get("group").toString(); JSONObject botDetails = new JSONObject(); botDetails.put("name", name); botDetails.put("language", language); botDetails.put("group", group); botDetailsArray.put(botDetails); result.put("chatbots", botDetailsArray); } } } } if(result.length()==0) { result.put("accepted", false); result.put("message", "User has no chatbots."); return new ServiceResponse(result); } result.put("accepted", true); result.put("message", "All chatbots of user fetched."); return new ServiceResponse(result); } }
So, if chatbot.json contains:
{"c9b58e182ce6466e413d5acafae906ad": {"chatbots": [ { "name": "testing111", "language": "en", "group": "Knowledge" }, { "name": "DNS_Bot", "language": "en", "group": "Knowledge" } ]}}
Then, the JSON received at http://api.susi.ai/cms/getSkillList.json?private=1&access_token=JwTlO8gdCJUns569hzC3ujdhzbiF6I is:
{ "session": {"identity": { "type": "email", "name": "test@whatever.com", "anonymous": false }}, "chatbots": [ { "name": "testing111", "language": "en", "group": "Knowledge" }, { "name": "DNS_Bot", "language": "en", "group": "Knowledge" } ], "accepted": true, "message": "All chatbots of user fetched." }
References:
- JSON with java – https://www.tutorialspoint.com/json/json_java_example.htm
- Making GET request using jQuery – https://api.jquery.com/jquery.get/