Skip to content

Instantly share code, notes, and snippets.

@fizerkhan
Created October 4, 2014 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizerkhan/443064d7d26b290b870f to your computer and use it in GitHub Desktop.
Save fizerkhan/443064d7d26b290b870f to your computer and use it in GitHub Desktop.
Send Plivo SMS from Google App Engine
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
import org.json.JSONObject;
import com.plivo.helper.api.client.RestAPI;
import com.plivo.helper.api.response.message.MessageResponse;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class SMSSender {
private static final Logger logger = Logger.getLogger(SMSSender.class
.getName());
public static final String PLIVO_AUTH_ID = "YOUR_AUTH_ID";
public static final String PLIVO_AUTH_TOKEN = "YOUR_AUTH_TOKEN";
public static final String PLIVO_NUMBER = "YOUR_PLIVO_NUMBER";
public static void sendSMS(String dst, String text) {
Client client = Client.create();
// client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client
.resource("https://api.plivo.com/v1/Account/" + PLIVO_AUTH_ID
+ "/Message/");
client.addFilter(new HTTPBasicAuthFilter(PLIVO_AUTH_ID,
PLIVO_AUTH_TOKEN));
JSONObject object = new JSONObject();
object.put("src", PLIVO_NUMBER);
object.put("dst", dst);
object.put("text", text);
object.put("type", "sms");
ClientResponse clientResponse = webResource
.type(MediaType.APPLICATION_JSON)
.header("User-Agent",
"my_bot_version_1.0")
.post(ClientResponse.class, object.toString());
int status = clientResponse.getStatus();
if (status >= 400) {
logger.severe(clientResponse.getEntity(String.class));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment