REST SMS API: Java Code Samples
- Text Messages
- Sending SMS Messages
- Receiving SMS Messages (Incoming API)
- Inbox & Folders
- Keywords
- Credits
- Contacts
- Groups
Text Messages
Sending SMS Messages
Sends SMS text messages to a single phone number or an array of phone numbers.
Code Samples
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class SendingSMSMessagesXML { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&PhoneNumbers[]=2123456785&PhoneNumbers[]=2123456786&Subject=From Winnie&Message=I am a Bear of Very Little Brain, and long words bother me&StampToSend=1305582245"; URL url = new URL("https://app.grouptexting.com/sending/messages?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Message ID: " + entry.getChildText("ID")); System.out.println("Subject: " + entry.getChildText("Subject")); System.out.println("Message: " + entry.getChildText("Message")); System.out.println("Total Recipients: " + entry.getChildText("RecipientsCount")); System.out.println("Credits Charged: " + entry.getChildText("Credits")); System.out.println("Time To Send: " + entry.getChildText("StampToSend")); System.out.println("Phone Numbers: " + implodeXML(entry.getChild("PhoneNumbers"), ", ")); System.out.println("Locally Opted Out Numbers: " + implodeXML(entry.getChild("LocalOptOuts"), ", ")); System.out.println("Globally Opted Out Numbers: " + implodeXML(entry.getChild("GlobalOptOuts"), ", ")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class SendingSMSMessagesJSON { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&PhoneNumbers[]=2123456785&PhoneNumbers[]=2123456786&Subject=From Winnie&Message=I am a Bear of Very Little Brain, and long words bother me&StampToSend=1305582245"; URL url = new URL("https://app.grouptexting.com/sending/messages?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Message ID: " + entry.getString("ID")); System.out.println("Subject: " + entry.getString("Subject")); System.out.println("Message: " + entry.getString("Message")); System.out.println("Total Recipients: " + entry.getString("RecipientsCount")); System.out.println("Credits Charged: " + entry.getString("Credits")); System.out.println("Time To Send: " + entry.getString("StampToSend")); System.out.println("Phone Numbers: " + entry.optString("PhoneNumbers", "")); System.out.println("Locally Opted Out Numbers: " + entry.optString("LocalOptOuts", "")); System.out.println("Globally Opted Out Numbers: " + entry.optString("GlobalOptOuts", "")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Inbox & Folders Back to top
Inbox & Folders
When working with the Inbox and Folder you can perform nine actions: Post (Add New Folder), Put (Update A Folder/Move A Message Between Folders), Delete (Delete A Message/Folder), Get (Get Inbox Message/Folder By ID), Index (Get All Messages/All Folders). The code samples below use the shared libraries:
· BaseObject.java
· Encoding.java
· SmsTextingConnection.java
· SmsTextingException.java
· Folder.java
· IncomingMessage.java
· XMLEncoding.java
· JSONEncoding.java
You can download them, along with the code samples here: JavaLib.zip.
Code Samples - Inbox Messages
Java - XMLimport com.SmsTexting.*; import java.util.ArrayList; import java.util.List; /** * Demonstration of IncomingMessage usage. */ public class IncomingMessageExamples { public static void main(String[] args) throws Exception { try { System.out.println("IncomingMessage XML example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.XML); List<BaseObject> messages = sms.getIncomingMessages(null, null, "Message", "asc", "10", "1"); System.out.println("Get incomingMessages: " + messages ); String messageId = messages.get(0).id; String messageId2 = messages.get(1).id; List<String> messageIds = new ArrayList<String>(); messageIds.add(messageId); messageIds.add(messageId2); System.out.println("Move messages to folder"); sms.moveMessagesToFolder(messageIds, "141"); IncomingMessage incomingMessage = sms.getIncomingMessage(messageId2); System.out.println("IncomingMessage get:" + incomingMessage); System.out.println("Delete message"); sms.delete(incomingMessage); //try to get exception - delete already deleted item System.out.println("Delete message2"); sms.delete(incomingMessage); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }Java - JSON
import com.SmsTexting.*; import java.util.ArrayList; import java.util.List; /** * Demonstration of IncomingMessage usage. */ public class IncomingMessageExamples { public static void main(String[] args) throws Exception { try { System.out.println("IncomingMessage JSON example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.JSON); List<BaseObject> messages = sms.getIncomingMessages(null, null, "Message", "asc", "10", "1"); System.out.println("Get incomingMessages: " + messages ); String messageId = messages.get(0).id; System.out.println("Move message to folder"); sms.moveMessageToFolder(messageId, "141"); IncomingMessage incomingMessage = sms.getIncomingMessage(messageId); System.out.println("IncomingMessage get:" + incomingMessage); System.out.println("Delete message"); sms.delete(incomingMessage); //try to get exception - delete already deleted item System.out.println("Delete message2"); sms.delete(incomingMessage); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }
Code Samples - Inbox Folders
Java - XMLimport com.SmsTexting.Encoding; import com.SmsTexting.Folder; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Folder usage. */ public class FolderExamples { public static void main(String[] args) throws Exception { try { System.out.println("Folder XML example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.XML); System.out.println("Get folders : " + sms.getFolders()); Folder folder = new Folder(null, "Customers"); folder = (Folder) sms.create(folder); System.out.println("Folder create:" + folder); String folderId = folder.id; folder = sms.getFolder(folderId); System.out.println("Folder get:" + folder); folder.id = folderId; folder.name = "test"; sms.update(folder); System.out.println("Folder update"); System.out.println("Folder delete"); sms.delete(folder); //try to get exception - delete already deleted item System.out.println("Folder delete2"); sms.delete(folder); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }Java - JSON
import com.SmsTexting.Encoding; import com.SmsTexting.Folder; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Folder usage. */ public class FolderExamples { public static void main(String[] args) throws Exception { try { System.out.println("Folder JSON example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.JSON); System.out.println("Get folders : " + sms.getFolders()); Folder folder = new Folder(null, "Customers"); folder = (Folder) sms.create(folder); System.out.println("Folder create:" + folder); String folderId = folder.id; folder = sms.getFolder(folderId); System.out.println("Folder get:" + folder); folder.id = folderId; folder.name = "test"; sms.update(folder); System.out.println("Folder update"); System.out.println("Folder delete"); sms.delete(folder); //try to get exception - delete already deleted item System.out.println("Folder delete2"); sms.delete(folder); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }
Keywords Back to top
Check Keyword Availability
Check whether a Keyword is available to rent on Group Texting.
Code Samples
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class CheckKeywordAvailabilityXML { public static void main(String[] args) throws Exception { URL url = new URL("https://app.grouptexting.com/keywords/new?format=xml&Keyword=honey&User=winnie&Password=the-pooh"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Keyword: " + entry.getChildText("Keyword")); System.out.println("Availability: " + entry.getChildText("Available")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class CheckKeywordAvailabilityJSON { public static void main(String[] args) throws Exception { URL url = new URL("https://app.grouptexting.com/keywords/new?format=json&Keyword=honey&User=winnie&Password=the-pooh"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Keyword: " + entry.getString("Keyword")); System.out.println("Availability: " + entry.getString("Available")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } } }
Rent Keyword
Rents a Keyword for use on Group Texting. You may rent a Keyword using a credit card you have stored in your Group Texting account, or you may pass credit card details when you call the API.
Code Samples - Stored Card
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class RentKeywordXML_saved { public static void main(String[] args) throws Exception { String data = "User=demo&Password=texting121212&Keyword=honey&StoredCreditCard=1111"; URL url = new URL("https://app.grouptexting.com/keywords?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Keyword ID: " + entry.getChildText("ID")); System.out.println("Keyword: " + entry.getChildText("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getChildText("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getChildText("ConfirmMessage")); System.out.println("Join message: " + entry.getChildText("JoinMessage")); System.out.println("Forward email: " + entry.getChildText("ForwardEmail")); System.out.println("Forward url: " + entry.getChildText("ForwardUrl")); System.out.println("Groups: " + implodeXML(entry.getChild("ContactGroupIDs"), ", ")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class BuyCreditsJSON_saved { public static void main(String[] args) throws Exception { String data = "User=demo&Password=texting121212&NumberOfCredits=1000&StoredCreditCard=1111"; URL url = new URL("https://app.grouptexting.com/billing/credits?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Credits purchased: " + entry.getString("BoughtCredits")); System.out.println("Amount charged, $: " + entry.getString("Amount")); System.out.println("Discount, $: " + entry.getString("Discount")); System.out.println("Plan credits: " + entry.getString("PlanCredits")); System.out.println("Anytime credits: " + entry.getString("AnytimeCredits")); System.out.println("Total: " + entry.getString("TotalCredits")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Code Samples - Non-Stored Card
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class RentKeywordXML { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&Keyword=honey&FirstName=Winnie&LastName=The Pooh&Street=Hollow tree, under the name of Mr. Sanders&City=Hundred Acre Woods&State=New York&Zip=12345&Country=US&CreditCardTypeID=Visa&Number=4111111111111111&expm=11&SecurityCode=123&ExpirationMonth=10&ExpirationYear=2017"; URL url = new URL("https://app.grouptexting.com/keywords?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Keyword ID: " + entry.getChildText("ID")); System.out.println("Keyword: " + entry.getChildText("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getChildText("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getChildText("ConfirmMessage")); System.out.println("Join message: " + entry.getChildText("JoinMessage")); System.out.println("Forward email: " + entry.getChildText("ForwardEmail")); System.out.println("Forward url: " + entry.getChildText("ForwardUrl")); System.out.println("Groups: " + implodeXML(entry.getChild("ContactGroupIDs"), ", ")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class RentKeywordJSON { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&Keyword=honey&FirstName=Winnie&LastName=The Pooh&Street=Hollow tree, under the name of Mr. Sanders&City=Hundred Acre Woods&State=New York&Zip=12345&Country=US&CreditCardTypeID=Visa&Number=4111111111111111&expm=11&SecurityCode=123&ExpirationMonth=10&ExpirationYear=2017"; URL url = new URL("https://app.grouptexting.com/keywords?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Keyword ID: " + entry.getString("ID")); System.out.println("Keyword: " + entry.getString("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getString("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getString("ConfirmMessage")); System.out.println("Join message: " + entry.getString("JoinMessage")); System.out.println("Forward email: " + entry.getString("ForwardEmail")); System.out.println("Forward url: " + entry.getString("ForwardUrl")); System.out.println("Groups: " + entry.optString("ContactGroupIDs", "")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Setup A Keyword
Configures an active Keyword for use on Group Texting.
Code Samples
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class SetupAKeywordXML { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&EnableDoubleOptIn=1&ConfirmMessage=Reply Y to join our sweetest list&JoinMessage=The only reason for being a bee that I know of, is to make honey. And the only reason for making honey, is so as I can eat it.&[email protected]&ForwardUrl=http://bear-alliance.co.uk/honey-donations/&ContactGroupIDs[]=honey lovers"; URL url = new URL("https://app.grouptexting.com/keywords/honey?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Keyword ID: " + entry.getChildText("ID")); System.out.println("Keyword: " + entry.getChildText("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getChildText("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getChildText("ConfirmMessage")); System.out.println("Join message: " + entry.getChildText("JoinMessage")); System.out.println("Forward email: " + entry.getChildText("ForwardEmail")); System.out.println("Forward url: " + entry.getChildText("ForwardUrl")); System.out.println("Groups: " + implodeXML(entry.getChild("ContactGroupIDs"), ", ")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class SetupAKeywordJSON { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&EnableDoubleOptIn=1&ConfirmMessage=Reply Y to join our sweetest list&JoinMessage=The only reason for being a bee that I know of, is to make honey. And the only reason for making honey, is so as I can eat it.&[email protected]&ForwardUrl=http://bear-alliance.co.uk/honey-donations/&ContactGroupIDs[]=honey lovers"; URL url = new URL("https://app.grouptexting.com/keywords/honey?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Keyword ID: " + entry.getString("ID")); System.out.println("Keyword: " + entry.getString("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getString("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getString("ConfirmMessage")); System.out.println("Join message: " + entry.getString("JoinMessage")); System.out.println("Forward email: " + entry.getString("ForwardEmail")); System.out.println("Forward url: " + entry.getString("ForwardUrl")); System.out.println("Groups: " + entry.optString("ContactGroupIDs", "")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Cancel A Keyword
Cancels an active Keyword on TellMyCell's short code.
Code Samples
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class CancelAKeywordXML { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh"; URL url = new URL("https://app.grouptexting.com/keywords/honey?format=xml&_method=DELETE"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; if (!isSuccesResponse) { InputStream responseStream = conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class CancelAKeywordJSON { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh"; URL url = new URL("https://app.grouptexting.com/keywords/honey?format=json&_method=DELETE"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; if (!isSuccesResponse) { InputStream responseStream = conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Credits Back to top
Check Credit Balance
Checks credit balances on your account.
Code Samples
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class CheckCreditBalanceXML { public static void main(String[] args) throws Exception { URL url = new URL("https://app.grouptexting.com/billing/credits/get?format=xml&User=winnie&Password=the-pooh"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Plan credits: " + entry.getChildText("PlanCredits")); System.out.println("Anytime credits: " + entry.getChildText("AnytimeCredits")); System.out.println("Total: " + entry.getChildText("TotalCredits")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class CheckCreditBalanceJSON { public static void main(String[] args) throws Exception { URL url = new URL("https://app.grouptexting.com/billing/credits/get?format=json&User=winnie&Password=the-pooh"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Plan credits: " + entry.getString("PlanCredits")); System.out.println("Anytime credits: " + entry.getString("AnytimeCredits")); System.out.println("Total: " + entry.getString("TotalCredits")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } } }
Buy Credits
Buys more credits for your account. You may purchase credits using a credit card you have stored in your TellMyCell account, or you may pass credit card details when you call the API.
Code Samples - Stored Card
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class BuyCreditsXML_saved { public static void main(String[] args) throws Exception { String data = "User=demo&Password=texting121212&NumberOfCredits=1000&StoredCreditCard=1111"; URL url = new URL("https://app.grouptexting.com/billing/credits?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Credits purchased: " + entry.getChildText("BoughtCredits")); System.out.println("Amount charged, $: " + entry.getChildText("Amount")); System.out.println("Discount, $: " + entry.getChildText("Discount")); System.out.println("Plan credits: " + entry.getChildText("PlanCredits")); System.out.println("Anytime credits: " + entry.getChildText("AnytimeCredits")); System.out.println("Total: " + entry.getChildText("TotalCredits")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); } wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class RentKeywordJSON_saved { public static void main(String[] args) throws Exception { String data = "User=demo&Password=texting121212&Keyword=honey&StoredCreditCard=1111"; URL url = new URL("https://app.grouptexting.com/keywords?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Keyword ID: " + entry.getString("ID")); System.out.println("Keyword: " + entry.getString("Keyword")); System.out.println("Is double opt-in enabled: " + entry.getString("EnableDoubleOptIn")); System.out.println("Confirm message: " + entry.getString("ConfirmMessage")); System.out.println("Join message: " + entry.getString("JoinMessage")); System.out.println("Forward email: " + entry.getString("ForwardEmail")); System.out.println("Forward url: " + entry.getString("ForwardUrl")); System.out.println("Groups: " + entry.optString("ContactGroupIDs", "")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Code Samples - Non-Stored Card
Java - XMLimport java.io.*; import java.net.*; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.*; public class BuyCreditsXML { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&NumberOfCredits=1000&CouponCode=honey2011&FirstName=Winnie&LastName=The Pooh&Street=Hollow tree, under the name of Mr. Sanders&City=Hundred Acre Woods&State=New York&Zip=12345&Country=US&CreditCardTypeID=Visa&Number=4111111111111111&SecurityCode=123&ExpirationMonth=10&ExpirationYear=2017"; URL url = new URL("https://app.grouptexting.com/billing/credits?format=xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { //Use JDOM (http://www.jdom.org) for xml response handling Element response = new SAXBuilder().build(responseStream).getRootElement(); System.out.println("Status: " + response.getChildText("Status")); System.out.println("Code: " + response.getChildText("Code")); if (isSuccesResponse) { Element entry = response.getChild("Entry"); System.out.println("Credits purchased: " + entry.getChildText("BoughtCredits")); System.out.println("Amount charged, $: " + entry.getChildText("Amount")); System.out.println("Discount, $: " + entry.getChildText("Discount")); System.out.println("Plan credits: " + entry.getChildText("PlanCredits")); System.out.println("Anytime credits: " + entry.getChildText("AnytimeCredits")); System.out.println("Total: " + entry.getChildText("TotalCredits")); } else { System.out.println("Errors: " + implodeXML(response.getChild("Errors"), "\n")); } responseStream.close(); } wr.close(); } public static String implodeXML(Element container, String delim) { if (container == null) return ""; List objs = container.getChildren(); StringBuffer buf = new StringBuffer(); int size = objs.size(); for (int i=0; i<size - 1; i++) { buf.append(((Element)(objs.get(i))).getText() + delim); } if (size != 0) { buf.append(((Element)(objs.get(size - 1))).getText()); } return buf.toString(); } }Java - JSON
import java.io.*; import java.net.*; import net.sf.json.*; import org.apache.commons.io.IOUtils; public class BuyCreditsJSON { public static void main(String[] args) throws Exception { String data = "User=winnie&Password=the-pooh&NumberOfCredits=1000&CouponCode=honey2011&FirstName=Winnie&LastName=The Pooh&Street=Hollow tree, under the name of Mr. Sanders&City=Hundred Acre Woods&State=New York&Zip=12345&Country=US&CreditCardTypeID=Visa&Number=4111111111111111&SecurityCode=123&ExpirationMonth=10&ExpirationYear=2017"; URL url = new URL("https://app.grouptexting.com/billing/credits?format=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); int responseCode = conn.getResponseCode(); System.out.println("Response code: " + responseCode); boolean isSuccesResponse = responseCode < 400; InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream(); if (responseStream != null) { String responseString = IOUtils.toString(responseStream); responseStream.close(); //Use json-lib (http://json-lib.sourceforge.net/) for response processing JSONObject response1 = (JSONObject) JSONSerializer.toJSON(responseString); JSONObject response = response1.getJSONObject("Response"); System.out.println("Status: " + response.getString("Status")); System.out.println("Code: " + response.getString("Code")); if (isSuccesResponse) { JSONObject entry = response.getJSONObject("Entry"); System.out.println("Credits purchased: " + entry.getString("BoughtCredits")); System.out.println("Amount charged, $: " + entry.getString("Amount")); System.out.println("Discount, $: " + entry.getString("Discount")); System.out.println("Plan credits: " + entry.getString("PlanCredits")); System.out.println("Anytime credits: " + entry.getString("AnytimeCredits")); System.out.println("Total: " + entry.getString("TotalCredits")); } else { Object ErrorMessage[] = (Object[]) JSONArray.toArray(response.getJSONArray("Errors")); for (int i = 0; i < ErrorMessage.length; i++) { System.out.println("Error: " + ErrorMessage[i]); } } } wr.close(); } }
Contacts Back to top
Contacts
When working with Contacts you can perform five actions: Post (Add New Contact), Put (Update/Edit A Contact), Delete (Delete A Contact), Get (Get Contact By ID), Index (Get All Contacts). The code samples below use shared libraries:
· BaseObject.java
· Encoding.java
· SmsTextingConnection.java
· SmsTextingException.java
· Group.java
· Contact.java
· XMLEncoding.java
· JSONEncoding.java
You can download them, along with the code samples here: JavaLib.zip.
Code Samples
Java - XMLimport com.SmsTexting.Contact; import com.SmsTexting.Encoding; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Contacts usage. */ public class ContactExamples { public static void main(String[] args) throws Exception { try { System.out.println("Contact XML example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.XML); System.out.println("Get contacts from group Honey Lovers: " + sms.getContacts(null, null, null, "Honey Lovers", null, null, null, null)); Contact contact = new Contact("2123456896", "Piglet", "P.", "[email protected]", "It is hard to be brave, when you are only a Very Small Animal.", null); contact = (Contact) sms.create(contact); System.out.println("Contact create:" + contact); contact = sms.getContact(contact.id); System.out.println("Contact get:" + contact); contact.groups.add("Friends"); contact.groups.add("Neighbors"); contact = (Contact) sms.update(contact); System.out.println("Contact update:" + contact); sms.delete(contact); //try to get exception - delete already deleted item sms.delete(contact); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }Java - JSON
import com.SmsTexting.Contact; import com.SmsTexting.Encoding; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Contacts usage. */ public class ContactExamples { public static void main(String[] args) throws Exception { try { System.out.println("Contact JSON example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.JSON); System.out.println("Get contacts from group Honey Lovers: " + sms.getContacts(null, null, null, "Honey Lovers", null, null, null, null)); Contact contact = new Contact("2123456896", "Piglet", "P.", "[email protected]", "It is hard to be brave, when you are only a Very Small Animal.", null); contact = (Contact) sms.create(contact); System.out.println("Contact create:" + contact); contact = sms.getContact(contact.id); System.out.println("Contact get:" + contact); contact.groups.add("Friends"); contact.groups.add("Neighbors"); contact = (Contact) sms.update(contact); System.out.println("Contact update:" + contact); sms.delete(contact); //try to get exception - delete already deleted item sms.delete(contact); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }
Groups Back to top
Groups
When working with Groups you can perform five actions: Post (Add New Group), Put (Update/Edit A Group), Delete (Delete A Group), Get (Get Group By ID), Index (Get All Groups). The code samples below use shared libraries:
· BaseObject.java
· Encoding.java
· SmsTextingConnection.java
· SmsTextingException.java
· Group.java
· Contact.java
· XMLEncoding.java
· JSONEncoding.java
You can download them, along with the code samples here: JavaLib.zip.
Code Samples
Java - XMLimport com.SmsTexting.Group; import com.SmsTexting.Encoding; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Group usage. */ public class GroupExamples { public static void main(String[] args) throws Exception { try { System.out.println("Group XML example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.XML); System.out.println("Get groups from group Honey Lovers: " + sms.getGroups("Name", "asc", "10", "1")); Group group = new Group("Tubby Bears", "A bear, however hard he tries, grows tubby without exercise"); group = (Group) sms.create(group); System.out.println("Group create:" + group); group = sms.getGroup(group.id); System.out.println("Group get:" + group); group = (Group) sms.update(group); System.out.println("Group update:" + group); sms.delete(group); //try to get exception - delete already deleted item sms.delete(group); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }Java - JSON
import com.SmsTexting.Group; import com.SmsTexting.Encoding; import com.SmsTexting.SmsTextingConnection; import com.SmsTexting.SmsTextingException; /** * Demonstration of Group usage. */ public class GroupExamples { public static void main(String[] args) throws Exception { try { System.out.println("Group JSON example"); SmsTextingConnection sms = new SmsTextingConnection("demouser", "password", Encoding.JSON); System.out.println("Get groups from group Honey Lovers: " + sms.getGroups("Name", "asc", "10", "1")); Group group = new Group("Tubby Bears", "A bear, however hard he tries, grows tubby without exercise"); group = (Group) sms.create(group); System.out.println("Group create:" + group); group = sms.getGroup(group.id); System.out.println("Group get:" + group); group = (Group) sms.update(group); System.out.println("Group update:" + group); sms.delete(group); //try to get exception - delete already deleted item sms.delete(group); } catch (SmsTextingException e) { System.out.println("SmsTexting error code:" + e.getResponseCode() + "; messages:" + e.getMessage()); } } }