Last active
December 27, 2015 00:39
-
-
Save ttscoff/7238660 to your computer and use it in GitHub Desktop.
Google voice SMS from command line. I've lost track of versions...
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 -Ku | |
require 'net/http' | |
require 'net/https' | |
require 'open-uri' | |
require 'cgi' | |
require 'optparse' | |
ACCOUNT = 'USERNAME' # Set to Google Voice account email for default account | |
PASSWORD = 'PASSWORD' # Set to Google Voice account password for default account | |
NUMBERS = ['+15554525515'] # Set one or more numbers for default destination(s) | |
options = {} | |
optparse = OptionParser.new do|opts| | |
opts.banner = "Usage: voicesms.rb -n +15554443333[,+15554442222] -m \"Message to send\" [-u Username:Password]" | |
options[:numbers] = NUMBERS | |
opts.on( '-n', '--numbers NUM[,NUM]', 'Phone numbers to SMS (separate multiples with comma)' ) do|numbers| | |
options[:numbers] = numbers.split(',') | |
end | |
options[:message] = false | |
opts.on( '-m', '--message MESSAGE', 'Message to send' ) do|msg| | |
options[:message] = msg | |
end | |
options[:username] = ACCOUNT | |
options[:password] = PASSWORD | |
opts.on( '-u', '--user USERNAME:PASSWORD', 'Google Voice username and password' ) do|creds| | |
parts = creds.split(':') | |
options[:username] = parts[0] | |
options[:password] = parts[1] | |
end | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
puts opts | |
exit | |
end | |
end | |
optparse.parse! | |
unless options[:message] | |
puts "Message required. Use -m \"MESSAGE\"." | |
puts "Enter `voicesms.rb -h` for help." | |
exit | |
end | |
def postit(uri_str, data, header = nil, limit = 3) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
# url = URI.parse(uri_str) | |
http = Net::HTTP.new('www.google.com',443) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
response,content = http.post(uri_str,data,header) | |
case response | |
when Net::HTTPSuccess then content | |
when Net::HTTPRedirection then postit(response['location'],data,header, limit - 1) | |
else | |
puts "Error authenticating." | |
end | |
end | |
def getit(uri_str, header, limit = 3) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
# url = URI.parse(uri_str) | |
http = Net::HTTP.new('www.google.com',443) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
response,content = http.get(uri_str,header) | |
case response | |
when Net::HTTPSuccess then content | |
when Net::HTTPRedirection then getit(response['location'],header, limit - 1) | |
else | |
puts response.inspect | |
end | |
end | |
data = "accountType=GOOGLE&Email=#{options[:username]}&Passwd=#{options[:password]}&service=grandcentral&source=BTCLI-2.0" | |
res = postit('/accounts/ClientLogin',data) | |
if res | |
authcode = res.match(/Auth=(.+)/)[1] | |
header = {'Authorization' => "GoogleLogin auth=#{authcode.strip}",'Content-Length' => '0'} | |
newres = getit('/voice',header) | |
if newres | |
rnrse = newres.match(/'_rnr_se': '([^']+)'/)[1] | |
options[:numbers].each {|num| | |
data = "_rnr_se=#{rnrse}&phoneNumber=#{num.strip}&text=#{CGI.escape(options[:message])}&id=" | |
finalres = postit('/voice/sms/send/',data,header) | |
if finalres["ok"] | |
puts "Message sent to #{num}" | |
else | |
puts "Error sending to #{num}" | |
end | |
} | |
else | |
puts "Error." | |
end | |
else | |
puts "Error." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment