2

I'm using a node server with a webhook for handling receiving Twilio messages to one of my numbers. I use this number to forward communications between users, essentially anonymizing things for them (they communicate with our number so they don't have to give theirs to the other user).

In my handler, I currently basically do this:

var sendTo = /* other user's number */;
var sendFrom = /* sender number in received message */;
var body = /* body of message received */;

twimlResponse(body, {
    to: sendTo,
    from: sendFrom
});

response.send(twimlResponse.toString());

This works perfect for text messages. However, my debug log is showing errors for media messages with no body (also not forwarding the media in general, since I'm not currently telling it to).

So, I'm trying to update to send the media messages as well. This is not working well.

I found this documentation: https://www.twilio.com/docs/guides/how-to-receive-and-reply-in-node-js#respond-with-media-mms-message

However, it doesn't work. I get an error saying 'this' has no method 'To'.

I tried modifying my code a bit,

var sendTo = /* other user's number */;
var sendFrom = /* sender number in received message */;
var body = /* body of message received */;
var mediaUrl = /* mediaUrl0 of the received message */

if( body && mediaUrl ) {
    twimlResponse(body, {
        to: sendTo,
        from: sendFrom,
        mediaUrl: mediaUrl
    });
}
else if( body ) {
    twimlResponse(body, {
        to: sendTo,
        from: sendFrom
    });
}
else if( mediaUrl ) {
    twimlResponse(nil, {
        to: sendTo,
        from: sendFrom,
        mediaUrl: mediaUrl
    });
}

response.send(twimlResponse.toString());

I haven't yet verified that last one is correct for sending a message with no body, but my issue is that I can't figure out how to tell the twiml response to include the media. I've tried Media, media, MediaUrl, MediaUrl0, mediaUrl0, and mediaUrl. Each time I get a warning in my debugger saying it was an invalid noun. The Twiml Documentation says the nouns should be case sensitive and capitalized, though for some reason to and from are taken lower case. That part all works fine. I just can't figure out how to attach the media url.

Any tips are appreciated!

Edit -

Here is one attempt based on the linked documentation, which I thought wasn't working at all, but re-arranging the order of properties I set, I see I'm just not setting the to / from number appropriately. I know a twiml without those passed in automatically responds to the number that sent a message, from the number that received it. I need to specify both those values since I'm forwarding one user's message to another.

twimlResp.message(function() {
    this.body(body);
    this.media(media0);
    this.to(sendTo);
    this.from(sendFrom);
});

This snippet works fine for body and media, similar to the documentation, but crashes when setting to

Edit - I've tried changing this.to and this.from to this.To and this.From, but those don't work.

I've also tried this:

twimlResp.message(function() {
    this.body(body);
    this.media(media0);
});
twimlResp.to = sendTo;
twimlResp.from = sendFrom;

Which also does not work - .to and .from are ignored, causing the response to go back to the sending user from the number that received the original message.

Jake T.
  • 4,308
  • 2
  • 20
  • 48

1 Answers1

2

I think you've gotten confused between the 2.0 and the 3.0 SDK. You can change between SDK versions by clicking in the top right of the example screen.

With your example provided, using the 3.0 SDK you can achieve this by:

var MessagingResponse = require('twilio').twiml.MessagingResponse;
var twiml = new MessagingResponse();

// Place your constraints here

var message = twiml.message({ to: sendTo, from: sendFrom });

if( body && mediaUrl ) {
    message.body(body);
    message.media(mediaUrl);
}
else if( body ) {
    messsage.body(body);
}
else if( mediaUrl ) {
    messsage.media(mediaUrl);
}

response.send(twiml.toString());

If you need a more in-depth detail on how I formulated this code, check out the MessagingResposne.js source. I'm assuming you have no restrictions on switching SDK versions, the reason I gave an example with the 3.0 SDK is because Twilio will be ending support for the 2.0 SDK as of 8/31/2017.

Howto with SDK 2.0

Since some users will need to migrate a large part of their codebase, here is how you can set a different to and from numbers with the SDK 2.0.

twimlResp.message(function() {
    this.body(body);
    this.media(media0);
}, { 
    to: sendTo, 
    from: sendFrom 
});
Matt Shirley
  • 4,216
  • 1
  • 15
  • 19
  • Totally did not notice that SDK switch. Also was not aware of the SDK support ending for 2.x. Suppose I ought to get on updating that... Though, I currently have 2.11.1 as my version in package.json. So why would trying to reproduce their example not work for me, considering I was using the 2.x example? – Jake T. May 12 '17 at 18:36
  • Any chance I can do something similar in 2.x? Looks like I'll have to update a fair bit of code in order to make this 3.x ready, and I have users unknowingly not having their MMS sent right now, so would like to put out a hotfix ASAP. – Jake T. May 12 '17 at 18:45
  • @JakeT. Do you still have the code you used that threw the error: 'this' has no method 'To'? – Matt Shirley May 12 '17 at 19:14
  • I've just re-written it, but this time re-arranged the order so that it set body and media first, and those did not throw the error. Before, I had `to` as the first property I tried setting, but it seems I'm just not setting those correctly. I'll add a snippet to the end of my question with the current setup, and just need to know how to pass the to / from numbers into it. – Jake T. May 12 '17 at 19:24
  • I'm having a look now at the legacy source. If you wanted to look to see where I find the answer, https://github.com/twilio/twilio-node/blob/legacy/lib/TwimlResponse.js – Matt Shirley May 12 '17 at 19:49
  • I was digging through that but found it difficult to understand. I found the Verbs, and saw that 'Body' and "Message" were both "legalNodes" for "Message" nouns, but I couldn't find anything that set "To" or "From" or "Sender" or anything similar that I could tell was relevant. – Jake T. May 12 '17 at 19:56
  • It was slightly confusing, however, I think I found your answer. I've updated my answer to include an example for the 2.0 SDK. I have only checked that it outputs a valid TwiML - I will leave the testing up to you. – Matt Shirley May 12 '17 at 20:02