Created
July 26, 2011 14:29
-
-
Save axeda/1106894 to your computer and use it in GitHub Desktop.
Send a SMS notification through Twilio
This file contains hidden or 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
| 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