How do I receive an incoming message for a user via Twilio Connect?

Viewed 190

I'm using Twilio Connect* to connect my users to Twilio. I can purchase numbers and send text messages, but I don't see how to receive messages for that Connect'ed account. They don't show up in my console, and I set a webhook URL for the main account that owns the Connect App, but I'm not receiving incoming requests for messages sent to my Connect'ed users' accounts.

* https://www.twilio.com/docs/iam/connect

I see how to do this with programmable SMS, but my Connected users' numbers don't show up here and I have no way of setting the webhook URL on the Connected user's account.

enter image description here

Whenever I go into the [test] Connected user's account (I won't be able to do this in production)

enter image description here

... and click on the number, I get an error.

enter image description here

So, how do I set up webhooks on my Connected user's number?

1 Answers

Being you are able to purchase numbers and send text messages I assume you have properly set up a Twilio App, and placed the snippet on your site allowing users to connect. Upon them authorizing your account, you now have permission to manage their account on their behalf. The actions you can perform on their account are not actually granted to "Your Twilio" account, but rather the App they approved (your app). Thus, you need to perform actions on their account from the App's API connection to twilio.

You should have stored the users "Account SID" upon them returning and hitting the "Authorize" URL you have set within your App Config at twilio. With that you can perform all account actions on their behalf, including setting the sms_url / Webhook URL you want hit upon receiving a text message at a specific phone number. You can NOT perform such task from the twilio user interface (that I am aware of) but you certainly can using the Twilio API.

When you create/update a phone number, you should also set the following.

  • sms_application_sid

(OR)

  • sms_url
  • sml_method

In Node.js it looks like this:

const accountSid = '<Your-Authorized-Users-Account-SID>';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.incomingPhoneNumbers
      .create({
      phoneNumber: '+17774445555',

      // add the following property
      sms_application_sid: '<Your-SMS-APP-SID>'

}).then(incoming_phone_number => console.log(incoming_phone_number.sid));

Note that you may set the following instead of "sms_application_sid", however should anything change on your end, it would require updating all phone numbers instead of the single SMS_App.

client.incomingPhoneNumbers
      .create({
      phoneNumber: '+17774445555',

      // add the following properties
      sms_method: 'POST',
      sms_url: 'https://app.yourcoolapp.com/on-received-sms' // This would be your "Hook" URL


}).then(incoming_phone_number => console.log(incoming_phone_number.sid));

Not only can you "create" but also update and delete numbers against a phone number as well.

Links and References in Twilio's API Docs


All in all, the new twilio phone number created using your app is now just a regular twilio number, however the way you manage it is a little different. You have complete control over all phone numbers, trunks, apps and everything else in their account. Just use their Account SID when making request.

Related