Last active
August 16, 2018 18:35
-
-
Save padma14/2376768ffe5db8111ef7ad081a3b3bde to your computer and use it in GitHub Desktop.
sap chatbot integration node code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* importing packages */ | |
var rfc = require('node-rfc'); | |
var express = require('express'); | |
var app = express(); | |
var bodyParser = require('body-parser'); | |
var cors = require('cors'); | |
let Assistant = require('actions-on-google').DialogflowApp; | |
var connParams = | |
{ | |
//give your SAP system credentials | |
user: 'username', | |
passwd: 'password', | |
ashost: '---.--.--.--', | |
sysnr: '00', | |
client: '400', | |
}; | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
/* to redirect the html page */ | |
app.use(express.static(__dirname + "/public")); | |
/* to parse the post body request */ | |
app.use(bodyParser.json()); | |
app.use(cors()); | |
/*ACTION DECLARATION */ | |
const purchase = "get.purchase.order"; | |
/*PARAMETER DECLARATION */ | |
const PLANT = "plant"; | |
/* Connect SAP system */ | |
var client = new rfc.Client(connParams, true); | |
client.connect(function (err) { | |
if (err) { | |
return console.error('Server cannot be connected. Stay calm and try again', err); | |
} | |
/*Create a post request */ | |
app.post('/rfqlist', function (req, res) { | |
/*Dialogflow assistant */ | |
const assistant = new Assistant({ request: req, response: res }); | |
function replyIntent(assistant) { | |
let plantID = assistant.getArgument(PLANT); | |
/*Invoking the SAP BAPI by passing required parameters */ | |
client.invoke('BAPI_REQUISITION_GETITEMS', { PLANT: plantID }, | |
function (err, doc) { | |
if (err) { | |
return console.error('Error invoking RFC', err); | |
} | |
/*Return a response to the chatbot */ | |
assistant.tell("The requisition number is " + doc["REQUISITION_ITEMS"][0].PREQ_NO + " "); | |
}); | |
} | |
/* Mapping actions to respective logic */ | |
let actionMap = new Map(); | |
actionMap.set(purchase, replyIntent); | |
assistant.handleRequest(actionMap); | |
}); | |
}); | |
a/*Listening port */ | |
app.listen(3001); | |
console.log("Server running on port 3001"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment