Created
July 24, 2011 18:54
-
-
Save dpritchett/1102938 to your computer and use it in GitHub Desktop.
Sending a text message via Twilio using Python or Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
aw come on, the ruby one's decent too ;)
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
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
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