Home > OS >  Using google app script to send a message with my PMS's API
Using google app script to send a message with my PMS's API

Time:11-30

Im using google app script to hopefully sent a message to my guests using my PMS (Smoobu). I think I have an issue with my syntax.

var smoobuPostOptions = {
"method": 'post',
"muteHttpExceptions": false,
"headers": {
"Api-Key": "???????????????????????????????????",
"cache-control": "no-cache",
"ContentType": "application/json",
},
"data": {
"subject": "Test Subject",
"messageBody": "Test Message"
}
}

var txTresponse = UrlFetchApp.fetch("https://login.smoobu.com/api/reservations/31418467/messages/send-message-to-guest", smoobuPostOptions);
var txtData = JSON.parse(txTresponse.getContentText());

The error I am getting is: Exception: Request failed for https://login.smoobu.com returned code 500. Truncated server response: {"status":500,"title":"Internal Server Error","detail":"Zend\Form\Form::setData expects an array or Traversable argument; received "NULL""} (use muteHttpExceptions option to examine full response)

API Docs - https://docs.smoobu.com/#send-message-to-guest

I do believe I just need an example and I will be able to work it out!

CodePudding user response:

From your provided document, it seems that you wanted to convert the following curl command to Google Apps Script.

curl -X POST \
  'https://login.smoobu.com/api/reservations/1/messages/send-message-to-guest' \
  -H 'Api-Key: secretApiKey' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{"subject" : "Test subject","messageBody" : "test"}'

In this case, how about the following modification?

From:

var smoobuPostOptions = {
"method": 'post',
"muteHttpExceptions": false,
"headers": {
"Api-Key": "???????????????????????????????????",
"cache-control": "no-cache",
"ContentType": "application/json",
},
"data": {
"subject": "Test Subject",
"messageBody": "Test Message"
}
}

To:

var smoobuPostOptions = {
  "method": 'post',
  "headers": {
    "Api-Key": "???????????????????????????????????",
    "cache-control": "no-cache",
  },
  "contentType": "application/json",
  "payload": JSON.stringify({
    "subject": "Test Subject",
    "messageBody": "Test Message"
  })
};

Note:

  • I think that the request of the above-modified script is the same as the sample curl command. But, if an error occurs, please confirm your endpoint, "Api-Key" and the request body again.

Reference:

  • Related