Recently i worked on one of the HYPE topic of the moment called ChatBot or interactive agent.
So i decided to realise this serie of posts, to share this experience by using a practical end-to-end scenario.
I will use SAP Cloud Platform and especially HANA MDC as Back-End technology that expose data through an HTTP REST service.
Using a chatbot tool that will consume this service and integrate it with FaceBook Messenger.
The article will be divided in 3 parts :
So let's start.
schema_name="SCHATBOTSM";
namespace ChatBotProject.data;
@Schema: 'SCHATBOTSM'
context chatbotsm
{
type CString : String(5);
type SString : String(40);
type tt_error
{
HTTP_STATUS_CODE: Integer;
ERROR_MESSAGE: String(100);
DETAIL: String(200);
};
@Catalog.tableType : #COLUMN
Entity Agent
{
KEY AGENT_ID: CString;
FIRST_NAME: SString;
LAST_NAME: SString;
};
};
service namespace "ChatBotProject.services.ChatBotSM"
{
"ChatBotProject.data::chatbotsm.Agent" as "Agents";
}
The exercise will be so easy if i consume my OData directly from the external side. So to add some tricky stuff i will add an XSJS file that consume the ChatBotSM.xsodata service and apply some logic to transform the OData result.
try {
var v_dest = "DEST_BOT_ODATA";
var v_pack = "ChatBotProject.services";
var v_query = "/Agents?$format=json";
switch ($.request.method)
{
case $.net.http.GET:
case $.net.http.POST:
case $.net.http.PUT:
//Reading the destination properties
var odestination = $.net.http.readDestination(v_pack, v_dest);
//Creating HTTP Client
var oclient = new $.net.http.Client();
//Creating Request
var orequest = new $.web.WebRequest($.net.http.GET, v_query);
//Add Header param
orequest.contentType = "application/json";
//Call the OData service
oclient.request(orequest, odestination);
//Receiving OData service response
var odata_Response = oclient.getResponse().body.asString();
var JSONObj = JSON.parse(odata_Response);
var botResponse;
if (JSONObj.d.results.length > 0)
{
botResponse = "Agents List: ";
for (var i = 0; i < JSONObj.d.results.length; i++)
{
botResponse += " ";
// Concatenate First & Last Agents names
botResponse += JSONObj.d.results[i].FIRST_NAME
+ JSONObj.d.results[i].LAST_NAME;
}
}
else
{
botResponse = "No data Found";
}
$.response.status = $.net.http.OK;
$.response.contentType = "application/json";
$.response.setBody(JSON.stringify({
"speech": botResponse,
"displayText": botResponse
}));
break;
default:
$.response.status = $.net.http.METHOD_NOT_ALLOWED;
$.response.setBody("Request method not allowed");
break;
}
}
catch (e)
{
$.response.setBody("Execution error: " + e.toString());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |