Search code examples
pythonjsonifttt

Using IFTTT to check JSON content and send Email or Text Message


I am trying to learn how to use the new WebHooks service on IFTTT, and I'm struggling to figure out how it is supposed to work. Most of the projects I can find online seem to refer to a deprecated "maker" service, and there are very little resources for interpreting the new channel.

Let's say I want to check the value of "online" every ten minutes in the following json file: https://lichess.org/api/users/status?ids=thibault

I can write a Python script that extracts this value like so:

response = urlopen('https://lichess.org/api/users/status?ids=thibault')
thibault = response.read()
data = json.loads(thibault)
status = data[0]['online']

If the status is equal to "true", I would like to receive a notification via email or text message. How do I integrate the python script and the webhooks service? Or do I even need to use this script at all? I assume I need some kind of cron job that regularly runs this Python script, but how do I connect this job with IFTTT?

When I create a new applet on IFTTT I am given the ability to create a trigger with a random event name, but it's unclear what that event name corresponds to.


Solution

  • I have a similar setup for my IFTTT webhook service. To the best of my understanding, the answer to your question is yes, you need this script (or similar) to scrap the online value, and you'll probably want to do a cron job (my approach) or keep the script running (wouldn't be my preference).

    IFTTT's webhooks a json of up to 3 values, which you can POST to a given event and key name.

    Below is a very simple excerpt of my webhook API:

    def push_notification(*values, **kwargs):
        # config is in json format        
        config = get_config()  
        report = {}
        IFTTT = {}
    
        # set default event/key if kwargs are not present
        for i in ['event', 'key']: 
            IFTTT[i] = kwargs[i] if kwargs and i in kwargs.keys() else config['IFTTT'][i]
    
        # unpack values received (up to 3 is accepted by IFTTT)
        for i, value in enumerate(values, 1): 
            report[f"value{i}"] = value
        if report:
            res = requests.post(f"https://maker.ifttt.com/trigger/{IFTTT['event']}/with/key/{IFTTT['key']}", data=report)
            # TODO: add try/except for status
            res.raise_for_status() 
            return res
        else:
            return None 
    

    You probably don't need all these, but my goal was to set up a versatile solution. At the end of the day, all you really need is this line here:

    requests.post(f"https://maker.ifttt.com/trigger/{event}/with/key/{key}", data={my_json_up_to_3_values})

    Where you will be placing your webhook event name and secret key value. I stored them in a config file. The secret key will be available once you sign up on IFTTT for the webhook service (go to your IFTTT webhook applet setting). You can find your key with a quick help link like this: https://maker.ifttt.com/use/{your_secret_key}. The event can be a default value you set on your applet, or user can choose their event name if you allow.

    In your case, you could do something like:

    if status:
        push_notification("Status is True", "From id thibault", event="PushStatus", key="MysEcR5tK3y")
    

    Note: I used f-strings with version 3.6+ (It's great!), but if you have a lower version, you should switch all the f-strings to str.format().