botmaker API post JSON google sheet script

Viewed 51

I have google sheet with data that I need to send using WhatsApp, so I have the API of Botmaker. They give a cURL and JSON data but I do not know how to use the cURL and JSON to do the post usen a google scrip function.

This is the cURL:

curl
- X POST
  --header 'Content-Type: multipart/form-data' 
  --header 'Accept: application/json' 
  --header 'access-token: myAccessToken'
-F chatPlatform=whatsapp
-F chatChannelNumber=############
-F platformContactId=############
-F mediaType=document

This is the Request URL of the API: 'https://go.botmaker.com/api/v1.0/message/binary/v3'

This is the Response Body: no content

This the Response Code: 401

Response Headers:

{
  "accept": "[application/json, application/xml, text/plain]",
  "access-control-allow-credentials": "true",
  "access-control-allow-headers": "bearer-token,access-token,Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin",
  "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD",
  "access-control-allow-origin": "https://go.botmaker.com",
  "alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
  "cache-control": "must-revalidate,no-cache,no-store",
  "content-length": "0",
  "content-security-policy": "default-src 'self'",
  "date": "Sat, 11 Jun 2022 22:52:02 GMT",
  "permissions-policy": "geolocation=(self \"https://go.botmaker.com\"), microphone=()",
  "reason-phrase": "Cannot authenticate user with received tokens after applying [access-token] and accessToken [null]",
  "referrer-policy": "no-referrer",
  "server": "Botmaker",
  "strict-transport-security": "max-age=31536000; includeSubDomains",
  "via": "1.1 google",
  "x-content-type-options": "nosniff",
  "x-frame-options": "DENY"
}

I understand that I need to use the UrlFetchApp.fetch

Someone can help me to create

1 Answers

I found the way, here is how it works for me:

var userNumber = '123456789012' // to: cellular phone number 

const waNumber = '123456789012'; // from: cellular phone 
const accessToken = 'faketoken'; // use you token

// send a template
var url = 'https://go.botmaker.com/api/v1.0/intent/v2';

var headers = {
    'Content-Type': 'application/json' ,
    'Accept': 'application/json' ,
    'access-token': accessToken
};

var dataJson = { 
    "chatPlatform": "whatsapp",
    "chatChannelNumber": waNumber,
    "platformContactId": userNumber,
    "ruleNameOrId": "template_name" // the name of the template
};

var payload = JSON.stringify(dataJson);

var options = {
    'method': 'POST',
    'headers': headers,
    'payload': payload
};

var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
Related