
const request = require('request');
module.exports = {
handler: function (event, context) {
var myJSONObject = {"status": event.data };
var opts = {
url: 'https://api.openconnectors.ext.hanatrial.ondemand.com/elements/api-v2/statuses',
method: 'POST',
body: JSON.stringify(myJSONObject),
headers: {
'Content-Type': 'application/json',
'Authorization': 'User xxxxx, Organization xxxx, Element xx'
}
};
request(opts, (err, res, body) => {
if (err) { return console.log(err); }
console.log(res.headers);
console.log(res.body);
});
}
}
const request = require('request');
module.exports = {
handler: function (event, context) {
//get the contact number of priority customers from the backend
const result = context.callFunction("connectbackend", { type: "text/plain", data: 'Priority' });
console.log('contactno '+ result.data);
//WhatsApp the customers
var contactno = result.data;
var myJSONObject = {
"Body": event.data.ptext + " " + event.data.pname,
"From": "whatsapp:+14155238886",
"To": "whatsapp:"+contactno
};
var opts = {
url: 'https://api.openconnectors.ext.hanatrial.ondemand.com/elements/api-v2/messages',
method: 'POST',
body: JSON.stringify(myJSONObject),
headers: {
'Content-Type': 'application/json',
'Authorization': 'User xxxxx, Organization xxxxxx, Element xxxx'
}
};
request(opts, (err, res, body) => {
if (err) { return console.log(err); }
console.log(res.headers);
console.log(res.body);
//If the customer group is chosen as 'All', then post a status to the company's //twitter account by calling the other function
if(event.data.cgrp == "All"){
console.log("all customers..");
const result = context.callFunction("tweetpromotion", { type: "text/plain", data: event.data.ptext });
}
});
}
}
{
"oa2": {
"client": "XXX",
"secret": "XXX",
"endpoint": "https://com.authentication.eu10.hana.ondemand.com/oauth/token"
"uri": "wss://enterprise-messaging-messaging-gateway.cfapps.eu10.hana.ondemand.com:443/protocols/amqp10ws"
}
private String getoauthToken() {
String token = "";
//Get the token end point, client id and secret from the service key of Enterprise messaging service
String tokenEndpoint = "https://aa5c9b38trial.authentication.eu10.hana.ondemand.com/oauth/token?grant_type=client_credentials";
String client_id = "xxxx";
String client_secret = "xxx";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(tokenEndpoint);
String base64Credentials = Base64.getEncoder().encodeToString((client_id + ":" + client_secret).getBytes());
//1. Prepare the request headers
httpPost.addHeader("Authorization", "Basic " + base64Credentials);
//2. Post the request
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
return "2.." +base64Credentials + "/n" + e.getMessage();
}
//3. Retrieve the token from the response
try {
JSONObject tokenjson = new JSONObject(IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
token = tokenjson.getString("access_token");
} catch (IOException e) {
e.printStackTrace();
return "3.." + e.getMessage();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
return "4.." + e.getMessage();
} catch (JSONException e) {
e.printStackTrace();
return "5.." + e.getMessage();
}
return token;
}
/**
* This method is used to call the messaging service
* @return
*/
private String callService(HttpServletRequest http_request){
//1. Get the oAuth token
String token = getoauthToken();
//2. Prepare the request headers
String url = "https://enterprise-messaging-pubsub.cfapps.eu10.hana.ondemand.com/messagingrest/v1/queues/promotions/messages";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
request.addHeader("Authorization", "Bearer "+token);
request.addHeader("Content-Type", "application/json");
request.addHeader("x-qos", "0");
String pname = http_request.getParameter("pname");
String pid = http_request.getParameter("pid");
String range = http_request.getParameter("range");
String cgrp = http_request.getParameter("cgrp");
String details = http_request.getParameter("ptext");
//3. Prepare the body of the request
String json = "{ "
+ "\"pname\": \"" + pname + "\", "
+ "\"pid\": \"" + pid + "\", "
+ "\"range\": \"" + range + "\", "
+ "\"cgrp\": \"" + cgrp + "\" ,"
+ "\"ptext\": \"" + details + "\" "
+ "}";
StringEntity input = null;
try
{
input = new StringEntity(json);
}catch(UnsupportedEncodingException e)
{
e.printStackTrace();
return "3.." + e.getMessage();
}
//6. Call the service and get he result
request.setEntity(input);
HttpResponse response = null;
try
{
response = httpClient.execute(request);
}catch(IOException e)
{
e.printStackTrace();
return "4.." + e.getMessage();
}
return "promotion" +pname + "published to "+ cgrp +" Customer group(s)";
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
7 | |
7 | |
6 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 |