The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping

--

Chapters
00:00 Using Ifttt To Check Json Content And Send Email Or Text Message
00:59 Accepted Answer Score 4
02:53 Thank you

--

Full question
https://stackoverflow.com/questions/4851...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #json #ifttt

#avk47



ACCEPTED ANSWER

Score 4


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().