Skip to content

Instantly share code, notes, and snippets.

@dpritchett
Created July 24, 2011 18:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpritchett/1102938 to your computer and use it in GitHub Desktop.
Save dpritchett/1102938 to your computer and use it in GitHub Desktop.
Sending a text message via Twilio using Python or Ruby
#!/usr/bin/env python
"""
REQUIREMENTS:
* A Twilio account
* Python with the twilio library (`pip install twilio`)
* A purchased phone number on twilio to serve as your "from" number ($1/mo)
* Some twilio credits to pay for the $.01/msg SMS fees
The python-twilio library uses OS-level environment variables
to store SID/Token info:
* $TWILIO_ACCOUNT_SID
* $TWILIO_AUTH_TOKEN
"""
from sys import argv
from twilio.rest import TwilioRestClient
sms_recipient = "+REDACTED" # My google voice number
sms_sender = "+REDACTED" # My paid twilio number
desired_message = ' '.join(argv[1:]) # Skip argv[0]; It's the name of this file
client = TwilioRestClient()
message = client.sms.messages.create(
to = sms_recipient,
from_ = sms_sender,
body = desired_message)
print("Text sent: " + desired_message)
#!/usr/bin/env ruby
require 'rubygems' # not necessary with ruby 1.9.2 but included for completeness
require 'twilio-ruby'
# put your own credentials here
@account_sid = ENV['TWILIO_ACCOUNT_SID']
@auth_token = ENV['TWILIO_AUTH_TOKEN']
SMS_RECIPIENT = "+REDACTED" # My google voice number
SMS_SENDER = "+REDACTED" # My paid twilio number
@body = ARGV.join ' '
# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new(@account_sid, @auth_token)
@client.account.sms.messages.create(
:from => SMS_SENDER,
:to => SMS_RECIPIENT,
:body => @body
)
puts "Text sent: #{@body}"
@dpritchett
Copy link
Author

I just looked under the hood of this sweet twilio library to see how it wraps the REST api to make SMS sending easy. Here' s the goods: https://github.com/twilio/twilio-python/blob/master/twilio/rest/resources.py#L900

@andrewmbenton
Copy link

aw come on, the ruby one's decent too ;)

@dpritchett
Copy link
Author

The ruby one's great, too! The pro-python commentary predates the addition of the Ruby snippet. I liked them both and I've actually moved on to play with the Ruby one signcificantly more than the Python library.

I hooked it up to Sinatra and sent myself a smiley face. I love smiley faces.

Edit: No fair, you should've mentioned that you are the primary contributor :P

@andrewmbenton
Copy link

yeah i should have mentioned that. check out this irb console i built a couple nights ago. makes playing with twilio really easy:

https://gist.github.com/1099050

$ ./twilio-console
irb> account.calls.sms.send :from => 'bleep', :to => 'bloop', :body => ':)'

send yourself an sms from the "command line".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment