Skip to content

Instantly share code, notes, and snippets.

@khaled-11
Last active February 13, 2021 18:50
Show Gist options
  • Save khaled-11/0a56d02938e26a4a7482b5972c6b0576 to your computer and use it in GitHub Desktop.
Save khaled-11/0a56d02938e26a4a7482b5972c6b0576 to your computer and use it in GitHub Desktop.
Botai API endpoint sample.
// This is POST endpoint in Node.js (Express) server.
app.post(`/botai_end`, async function(request, response) {
// Prepare template response items.
var elements = []
elements[elements.length]={"title": "test 1" ,"image_url":"https://e55321204fac.ngrok.io/s_3.jpg", "subtitle":"test", "default_action": {"type": "web_url","url": `https://botai.me`,"messenger_extensions": "true","webview_height_ratio": "full"},"buttons":[{"type":"web_url","url":"https://botai.me","title":"See More"}]}
elements[elements.length]={"title": "Test 2" ,"image_url":"https://e55321204fac.ngrok.io/s_3.jpg", "subtitle":"test", "default_action": {"type": "web_url","url": `https://botai.me`,"messenger_extensions": "true","webview_height_ratio": "full"},"buttons":[{"type":"web_url","url":"https://botai.me","title":"See More"}]}
elements[elements.length]={"title": "test 3" ,"image_url":"https://e55321204fac.ngrok.io/s_3.jpg", "subtitle":"test", "default_action": {"type": "web_url","url": `https://botai.me`,"messenger_extensions": "true","webview_height_ratio": "full"},"buttons":[{"type":"web_url","url":"https://botai.me","title":"See More"}]}
// Template response
template = {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements": elements
}
},
"quick_replies":[{"content_type":"text","title":"Get Help","payload":"HELP"},{"content_type":"text","title":"Start Again","payload":"GET_STARTED"}]
}
// Attachment response
att = {
"attachment":{
"type":"image",
"payload":{
"url":"https://e55321204fac.ngrok.io/s_3.jpg",
}
},
"quick_replies":[{"content_type":"text","title":"Get Help","payload":"HELP"},{"content_type":"text","title":"Start Again","payload":"GET_STARTED"}]
}
// Quick replies response
qr1 = {"text": "Welcome {{user_first_name}}, We are excited to meet you!", "quick_replies": [
{
"content_type":"text",
"title":"Get Help",
"payload":"HELP"
}, {
"content_type":"text",
"title":"See Products",
"payload":"PRODUCTS"
}
]}
qr2 = {"text": "These are few options", "quick_replies": [
{
"content_type":"text",
"title":"Customer Service Chat",
"payload":"CS"
}, {
"content_type":"text",
"title":"Phone Call",
"payload":"CALL"
}
]}
qr3 = {"text": "Welcome {{user_first_name}}, Thanks for your comment!", "quick_replies": [
{
"content_type":"text",
"title":"Get Help",
"payload":"HELP"
}, {
"content_type":"text",
"title":"See Products",
"payload":"PRODUCTS"
}
]}
// Text response
text1 = {"text": "{{user_first_name}}, How can we help you?"}
text2 = {"text": "Ok, let's start again. How are you doing today?"}
text3 = {"text": "{{user_first_name}}, can you repeat this please?!"}
text4 = {"text": "Backup Response"}
text5 = {"text": "Backup Response"}
// Print the request body
console.log(request.body)
// If it is Messenger message
if (request.body.new_msg){
// If it is text
if (request.body.eventType === "message_text"){
// The NLP for this text
console.log(JSON.parse(request.body.nlp))
// Check the intent and choose response
if (JSON.parse(request.body.nlp).intents[0]){
// Check fot the intent name. You may build logic with combinations.
if (JSON.parse(request.body.nlp).intents[0].name === "intent_name"){
// Here the App will reply with text then attachment then quick reply. To replace failing attachments, add backup message in the secondary response (same order).
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[text2,att,qr]}, secondaryResponse:["", text2, ""]});
} else {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[text2,att,qr]}, secondaryResponse:["", text2, ""]});
}
// If no intents, you may check for entities and build logic.
} else {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[text2,att]}, secondaryResponse:["", text1]});
}
// If it is attachment
} else if (request.body.eventType === "message_attachment"){
console.log(JSON.parse(request.body.value))
// Here the app will reply with template and replace failing attachment with quick reply.
response.send({token: "asdasdas", response:{"persona_id":"none","responses":[template]}, secondaryResponse:[qr1]});
}
// If it is postback
else {
console.log(request.body.value)
// Check for the payload name
if (request.body.value === "HELP"){
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[att]}, secondaryResponse:[text4]});
} else if (request.body.value === "GET_STARTED") {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[att]}, secondaryResponse:[text2]});
}
// Fallback
else {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","responses":[att]}, secondaryResponse:[text3]});
}
}
}
// If it is comment
else if (request.body.new_comment){
// This is the NLP for this comment if any text found
console.log(JSON.parse(request.body.nlp))
// Check the intent and choose response
if (JSON.parse(request.body.nlp).intents[0]){
if (JSON.parse(request.body.nlp).intents[0].name === "intent_name"){
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","response":att}, secondaryResponse:text5});
} else {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","response":att}, secondaryResponse:text5});
}
} else {
response.send({token: "asdasdas", response:{"persona_id":"1011315279345693","response":att}, secondaryResponse:text5});
}
}
// send 200 status (DON'T IGNORE)
else {
response.sendStatus(200);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment