Skip to content

Instantly share code, notes, and snippets.

@mplacona
Created August 19, 2016 18:16
Show Gist options
  • Save mplacona/f254d15483ec01ace4dd942e822e05f8 to your computer and use it in GitHub Desktop.
Save mplacona/f254d15483ec01ace4dd942e822e05f8 to your computer and use it in GitHub Desktop.
SMSBackend.java
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.resource.instance.Sms;
import java.util.HashMap;
import java.util.Map;
import static spark.Spark.*;
public class SMSBackend {
public static void main(String[] args) {
get("/", (req, res) -> "Hello, World");
TwilioRestClient client = new TwilioRestClient("YOUR_TWILIO_ACCOUNT_SID", "YOUR_TWILIO_AUTH_TOKEN");
post("/sms", (req, res) -> {
String body = req.queryParams("Body");
String to = req.queryParams("To");
String from = "YOUR_TWILIO_PHONE_NUMBER";
Map<String, String> callParams = new HashMap<>();
callParams.put("To", to);
callParams.put("From", from);
callParams.put("Body", body);
Sms message = client.getAccount().getSmsFactory().create(callParams);
return message.getSid();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment