2

I am attempting to incorporate Twilio into my application that is running on my parse server deployed with Heroku and MongoLab. I am trying to configure by using this code in my cloud/main.js file

var twilio = require("twilio");
twilio.initialize("87se46bovanw4v5aiwy4o57","ia8o57awyov57yn875vyboe");


Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendSMS({
    From: "6543211234",
    To: 8065456703,
    Body: "Start using Parse and Twilio!"
  }, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
  });
});

however, I get this response

    UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} [NSDebugDescription: JSON text did not start with array or object and option to allow fragments not set.]

I have determined that the problem lies in the first two lines when trying to initialize Twilio. I figure this is because the Twilio Cloud Module isn't integrated into my parse server like it was with Parse Hosted cloud code, but I'm not sure. How can I fix this problem? Thanks for your time.

Baylor Mitchell
  • 1,029
  • 1
  • 11
  • 19
  • Have you installed the Twilio module into your application? `npm install twilio --save`. Also, the bug you are showing looks like it is from iOS, do you have any more information about where the error occurs in the server? – philnash Apr 07 '16 at 13:47
  • I get that error anytime something on the cloud side is incorrect. And I haven't installed the twilio module, however, my server is deployed through the web version of github with heroku (I didn't use the command line tool). Will installing this have any effect? – Baylor Mitchell Apr 07 '16 at 14:28

2 Answers2

2

Twilio developer evangelist here.

It sounds like, from our conversation in the comments, like you have not installed the Twilio npm module just yet. On Parse, you didn't have to install the module as it was included by default. To use the Twilio module using Parse server you need it installed.

To install the module, open up your application in the terminal and type:

$ npm install twilio --save

The --save flag is important as it saves the dependency to your package.json file. Check in the updated package.json and deploy your code again. Now, when deploying to Heroku the npm modules, including the Twilio module, will be installed.

Marin, who already answered as well, had a good point. I also recommend using twilio.sendMessage. It uses the newer and better featured Messages resource (rather than the deprecated SMS resource).

Let me know if this helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
1

I think this is problem for Twilio version.

you should use twilio.sendMessage.

var twilio = require("twilio");
twilio.initialize("87se46bovanw4v5aiwy4o57","ia8o57awyov57yn875vyboe");


Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendMessage({
    From: "6543211234",
    To: 8065456703,
    Body: "Start using Parse and Twilio!"
  }, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
  });
});

Hope to help you.

Thanks

///////////////////////////////

Here is my code work fine.

Parse.Cloud.define("sendSMS", function(request, response) {

    console.log(request);
    var twilio = require("twilio")("ACCOUNT_SID","AUTH_TOKEN");

    twilio.sendMessage({

        to: request.params.number,
        from: request.params.from,
        body: request.params.message

    }, function(err, responseData) { 

        if (err) {
            response.error(err);
        } else { 
            response.success("SMS sent.");
        }

    });

});