Search code examples
expressparse-platformparse-cloud-code

Calling Parse Cloud Code function from Express Get Method


I just have a super quick question for those knowledgeable in Parse, Cloud Code, Twilio and Express...

Basically I've set up a node.js Express function that handles the GET for a particular URL that gets called by Twilio whenever someone calls a phone number, and I want to call a Parse Cloud Code function from the GET handler function that notifies a user whose account is associated with that particular number.

Therefore my question is whether or not in the code example below the Parse Cloud function "notifyConferenceNumberOwner" will be called.

app.get('/conf', function(request, response) {

    var phoneNumber = request.query['To'];

    var twiml = new twilio.TwimlResponse();
    twiml.say('Your conference call will begin momentarily.',
    {
        voice:'woman',
        language:'en-gb'
    })
    .dial({
        action:'http://url-to-call-status',
        method:'GET'
    },
    function(node) {
        node.conference('MyConference', {
        waitUrl:'http://twimlets.com/holdmusic?Bucket=com.twilio.music.guitars',
        startConferenceOnEnter:'true',
        beep:'true'
            });
    });

    response.type('text/xml');
    response.send(twiml.toString());

    Parse.Cloud.run('notifyConferenceNumberOwner', { conferenceCallNumber: phoneNumber }, {
        success: function(ratings) {
            console.log('*** NOTIFICATION SUCCEEDED');
        },
        error: function(error) {
            console.error('*** NOTIFICATION FAILED: ' + error);
        }
    });

});

I would hope that this would work but it seems to be failing in my case... Let me know if I'm missing something.

Thanks!


Solution

  • In further testing, it appears that, in fact, that Parse Cloud Code function is getting called and push notifications are being sent.

    For some reason (perhaps throttling on Apple's part?) it appeared that they were not being sent and it also seemed like the function was not getting called when I looked at the logs, but I definitely am receiving some push notifications.

    UPDATE

    Actually what ended up working even better was just putting the push notification sending code in a plain old JavaScript function as is described here (not even with a callback):

    https://www.parse.com/questions/can-i-use-a-cloud-code-function-within-another-cloud-code-function

    So now it looks more like this:

    app.get('/conf', function(request, response) {
    
        var phoneNumber = request.query['To'];
    
        var twiml = new twilio.TwimlResponse();
        twiml.say('Your conference call will begin momentarily.',
        {
            voice:'woman',
            language:'en-gb'
        })
        .dial({
            action:'http://url-to-call-status',
            method:'GET'
        },
        function(node) {
            node.conference('MyConference', {
            waitUrl:'http://twimlets.com/holdmusic?Bucket=com.twilio.music.guitars',
            startConferenceOnEnter:'true',
            beep:'true'
                });
        });
    
        sendCallNotification(phoneNumber);
    
        response.type('text/xml');
        response.send(twiml.toString());
    });
    

    ...and now it's working every time and has been very reliable so far.

    The sendCallNotification just prepares a message for the push notification and then calls Parse.Push.send() -- and either it works or it doesn't. Either way there isn't really a consumer for that information in this scenario, so I just log the fact that it succeeded or failed.

    I'm satisfied and it's working great!

    Just wish Parse was going to be sticking around... but that's another issue.