Skip to content

Instantly share code, notes, and snippets.

@axeda
Created July 26, 2011 14:29
Show Gist options
  • Select an option

  • Save axeda/1106894 to your computer and use it in GitHub Desktop.

Select an option

Save axeda/1106894 to your computer and use it in GitHub Desktop.
Send a SMS notification through Twilio
import org.apache.commons.httpclient.Credentials
import org.apache.commons.httpclient.HostConfiguration
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.UsernamePasswordCredentials
import org.apache.commons.httpclient.auth.AuthScope
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.NameValuePair
//logger.info "Calling ${parameters.phoneNumber}"
String twilioHost = 'api.twilio.com'
String apiID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
String aipPass = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
HostConfiguration hc = new HostConfiguration()
hc.setHost(twilioHost, 443, "https")
def url = "/2008-08-01/Accounts/$apiID/SMS/Messages"
def client = new HttpClient()
Credentials defaultcreds = new UsernamePasswordCredentials(apiID,
aipPass)
client.getState().setCredentials(null, null, defaultcreds)
PostMethod post = new PostMethod(url);
post.addParameter 'IfMachine', 'Continue'
post.addParameter 'Method', 'POST'
post.addParameter 'From', '[YourNumber]'
post.addParameter 'To', parameters.phoneNumber
post.addParameter 'Body', 'This is an SMS from Axeda'
client.executeMethod(hc, post);
//logger.info message = "Status:" + post.getStatusText()
//logger.info post.getResponseBodyAsString()
post.releaseConnection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment