Cyzanfar
Cyzanfar

Reputation: 7146

handle sending sms between users over Twilio

I've read THIS Twilio post on how to enable text messaging between users. I am building using Ruby on Rails.

At the moment, this is what I have:

This is great because I'm giving the user several means of communication between each other (in app or email).

Now i'd like a similar but instead using the Twilio API.

Particularly i'd like to enable this type of communication: As per Twilio help center

I'm having trouble understanding the concept of having multiple numbers that can be used by different user but somehow you know who's the message for...( Wow, I'm even having trouble describing what i'm having trouble with!)

I'm using the Ruby wrapper for the Twilio API

To send a message to user it is fairly simple:

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

# send sms
@client.messages.create(
  from: 'twilio_number',
  to: 'a user number',
  body: 'Hey there!'
)

To receive a message:

First configure the your Twilio phone number request url to point to an endpoint in your application. In my case its 'yourappurl.com/twilio/create'.

class TwilioController < ApplicationController

  include Webhookable

  after_filter :set_header
  skip_before_action :verify_authenticity_token

  def create
    # inbound messages arrive here. Need to do something with the message, identify who's it for
    # the receiving user and where its coming from ( the user who sent the text message)
    render_twiml response
  end

end

However this is for my application sending a message to a user's phone number. How can I handle communication between two users where user X can communicate with user Y via text messages (all going through my application)

Upvotes: 1

Views: 290

Answers (1)

philnash
philnash

Reputation: 73100

Twilio developer evangelist here.

The help centre article on sending messages between users has a great explanation of how to implement this in theory, however you probably want to see some code.

Your best bet is to work through this tutorial on Masked numbers with Twilio which shows you practically how you would purchase a number for a user and then connect two users through that number.

In brief, you need to get a number that represents a relationship between two users. When you receive a message at that number and you get the webhook from Twilio then you can look up the relationship based on the Twilio number the message was sent to and the number it was sent from. You can then connect and send the message on to the number on the other side of the relationship.

But, as I said, I recommend going through the tutorial to really learn this in depth.

Let me know if this helps at all.

Upvotes: 2

Related Questions