question

Sebastien Saintomer avatar image
Sebastien Saintomer asked

How to use External Services

Hello Everyone !

I am currently trying to integrate Twilio with CloudScript and have not had that much success. This question stands for Twilio but also all future services I plan on implementing.

I understood we weren't able to call libraries nor install anything so I can't install npms and use existing functions provided. I, therefore, went with the POST request side of things but faced numerous issues (I am not very used to doing it that way, more with NPMs ^^) like the request not reaching the service, returning undefined, or not returning at all.

Here is the docs in which they're telling me to do the following :

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.messages
      .create({
         body: 'This will be the body of the new message!',
         from: '+15017122661',
         to: '+15558675310'
       })
      .then(message => console.log(message.sid));


This isn't really possible since require() isn't usable on CloudScript. Another way they proposed was using cURL but this isn't possible as well in CloudScript :

curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json \
--data-urlencode "Body=This will be the body of the new message$EXCLAMATION_MARK" \
--data-urlencode "From=+15017122661" \
--data-urlencode "To=+15558675310" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN


So am I coming to you guys to know if anyone found or knows a way to go around all those limitations and just send a basic POST request from CloudScript to any service. If you could just provide a template, video or URL that shows what final form should my code have it would be great to help me understand the process I need to replicate to get this to work not only for Twilio but for any future service.

Deep and sincere thanks in advance !

apisCloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Seth Du avatar image
Seth Du answered

Cloud Script is based on standard Javascript while we also provide a http request sample in revision 1:

// This is a simple example of making a web request to an external HTTP API.
handlers.makeHTTPRequest = function (args, context) {
    var headers = {
        "X-MyCustomHeader": "Some Value"
    };
    
    var body = {
        input: args,
        userId: currentPlayerId,
        mode: "foobar"
    };


    var url = http://httpbin.org/status/200;
    var content = JSON.stringify(body);
    var httpMethod = "post";
    var contentType = "application/json";


    // The pre-defined http object makes synchronous HTTP requests
    var response = http.request(url, httpMethod, content, contentType, headers);
    return { responseContent: response };
};


Meanwhile, we also recommend to use Azure function, which supports Nuget package installation or external import. You may double check if the external service you are using supports Nuget installation.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.