Last active
January 19, 2019 16:43
-
-
Save fergalhanley/c723ef8052f397a43d459ce8096c4b64 to your computer and use it in GitHub Desktop.
This file contains 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 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 - [email protected] (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/<your api key here> | |
* @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; | |
} | |
} | |
} | |
} |
This file contains 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.junit.Test; | |
/** | |
* Test usage example. This call could do something like trigger IFTTT when a new premium user signs up | |
* which in turn you could use to send a notification to you mobile, send a welcome email, tweet, etc. | |
*/ | |
public class IFTTT_Test { | |
@Test | |
public void notifyNewUser(){ | |
final int PAYMENT_PLAN_TIER = 5; | |
final int NUMBER_OF_USERS_NOW = 1234; | |
final String event = "new_user_signup"; | |
final String key = "b2QVdqYs3PXo6aL7OyLgKb"; // your own key here | |
final String myValue1 = "[email protected]"; | |
final String myValue2 = PAYMENT_PLAN_TIER; | |
final String myValue3 = NUMBER_OF_USERS_NOW; | |
new IFTTT().trigger(event, key, myValue1, myValue2, myValue3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment