import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import lombok.Data; import org.codehaus.jackson.map.ObjectMapper; import javax.ws.rs.core.MediaType; import java.io.IOException; /** * A easy to use class to facilitate calling the IFTTT Maker API (https://ifttt.com/maker) to trigger IFTTT recipes. * Requires Jersey and Jackson (I implemented this with versions 2.22.1 and 1.9.13 respectively) * @author Fergal Hanley - fergalhanley@gmail.com (http://fergalhanley.com) */ public class IFTTT { private static final String IFTTT_TRIGGER_ENDPOINT = "https://maker.ifttt.com/trigger/%s/with/key/%s"; /** * Trigger the IFTTT API. * More details at https://maker.ifttt.com/use/ * @param event The unique event identifier you setup for the trigger * @param key The app key generated by IFTTT * @param values Up to three values you can pass to the API request */ public void trigger(String event, String key, Object ...values){ Client client = Client.create(); String resourceUrl = String.format(IFTTT_TRIGGER_ENDPOINT, event, key); WebResource webResource = client.resource(resourceUrl); IftttReq iftttReq = new IftttReq(values); webResource.type(MediaType.APPLICATION_JSON).post(iftttReq.toJson()); } @Data private class IftttReq { private final String value1; private final String value2; private final String value3; public IftttReq(Object... values) { this.value1 = values.length > 0 ? String.valueOf(values[0]) : null; this.value2 = values.length > 1 ? String.valueOf(values[1]) : null; this.value3 = values.length > 2 ? String.valueOf(values[2]) : null; } public String toJson(){ ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(this); } catch (IOException e) { e.printStackTrace(); return null; } } } }