Messages

For more information, see the Message REST Resource documentation.

Sending a Text Message

Send a text message in only a few lines of code.

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@message = @client.messages.create(
  to: "+13216851234",
  from: "+15555555555",
  body: "Hello!"
)

If you want to send a message from a short code on Twilio, just set from to your short code’s number.

Sending a Picture Message

To send a picture, set media_url to the url of the picture you wish to send.

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@message = @client.messages.create(
  to: "+15558676309",
  from: "+15555555555",
  body: "Jenny I need you!",
  media_url: "http://twilio.com/heart.jpg"
)

You can send multiple pictures in the same message by setting media_url to an array of urls.

@message = @client.messages.create(
  to: "+15558676309",
  from: "+15555555555",
  body: "Jenny I need you!",
  media_url: [
    "http://twilio.com/heart.jpg",
    "http://twilio.com/rose.jpg"
  ]
)

Retrieving Sent Messages

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@client.messages.list.each do |message|
  puts message.body
end

Redacting or Deleting Messages

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token
@msg_sid = 'MM123'
@msg = @client.messages.get('MM123')
# Deletes the Body field contents
@msg.redact

# Removes the entire Message record
@msg.delete

Filtering Your Messages

The list() methods supports filtering on to, from, and date_sent. The following will only show messages to “+5466758723” on January 1st, 2011.

require 'twilio-ruby'

# To find these visit https://www.twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"

@client = Twilio::REST::Client.new account_sid, auth_token

@messages = @client.messages.list(
  to: "+5466758723",
  date_sent: "2011-01-01"
)

@messages.each do |message|
  puts message.body
end