How to send an IFTTT event
In this page I provide you with script codes in order to send an IFTTT event.
Please take note of the following requirements:
- here is how to configure IFTTT so that you can use it with bttn HTTP action, or HTTP Short/Long action.
- Start by taking into use the IFTTT Maker Webhook channel:
- Connect to your IFTTT account.
- Go to "https://ifttt.com/maker_webhooks"
- Select "Documentation" button. This page shows an URL like "https://maker.ifttt.com/use/xxxxxyyyyzzz".
- Copy your key, it will be used in all scripts as name "apikey".
- Create your IFTTT maker webhook applet:
- Go to "https://ifttt.com/create".
- Select "This".
- Search for "Webhooks" and select it.
- Select "Receive a web request".
- In "Event Name" field we insert the event name, in all the scripts we use "button_pressed" as example. This event name is used in all scripts as name "event".
- Select whatever you like as the "That" part and save your applet, for example search for "Notifications" and select "Send a notification from the IFTTT app".
Be sure to keep the webhook URLs to yourself, do not share them on social networks, as anyone can use the URL to launch your IFTTT applet.
Bash language
- we need some Entware's packages, so refer to this page on how to install Entware on a router;
- we need an Entware's package, curl, to install it we must use the command line "opkg install curl";
#!/bin/bash
apikey=""
event="button_pressed"
value1="My value 1"
value2="My value 2"
value3="My value 3"
request_body=$(cat <<EOF
{
"value1": "$value1",
"value2": "$value2",
"value3": "$value3"
}
EOF
)
echo $request_body
header="Content-Type: application/json"
/opt/bin/curl -v -i -X POST -H "$header" -d "$request_body" "https://maker.ifttt.com/trigger/$event/with/key/$apikey"
PHP language
- to execute these scripts on a router, we need some Entware's packages, so refer to this page on how to install Entware on a router - these scripts have been tested using PHP release 7.2.9;
execute this command line "opkg install php7-cli php7-mod-curl php7-mod-json". We must execute this script using the command "php-cli";
- to execute these scripts on Microsoft Windows, we need to pass some additional libraries through the command line (or eventually changing the PHP settings) - these scripts have been tested using PHP release 5.6.36;
we have to change the file "PHP.INI" in line ";extension=php_curl.dll" (remove the semi-colon
to make the library available); otherwise we must call the PHP interpreter via command line using the parameter "-d extension=php_curl.dll";
<?php
$apikey = "";
$event = "button_pressed";
$value1 = "My value 1";
$value2 = "My value 2";
$value3 = "My value 3";
$ch = curl_init();
$postdata = json_encode([
"value1" => $value1,
"value2" => $value2,
"value3" => $value3,
]);
$header = array();
$header[] = "Content-Type: application/json";
curl_setopt($ch,CURLOPT_URL, "https://maker.ifttt.com/trigger/$event/with/key/$apikey");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
print_r($result);
print_r("\r\n\r\n");
curl_close($ch);
?>
Python language
- to execute these scripts on a router, we need the Python interpreter, so
refer to this page on how to install the Python interpreter on a router - these scripts have been tested using Python release 3.7.0;
- to execute these scripts on Microsoft Windows, firstly we need to install Python (I suggest to install release 3.x) - these scripts have been tested using Python release 3.7.1;
import requests
apikey = ""
event = "button_pressed"
value1 = "Test001-value1"
value2 = "Test001-value2"
value3 = "Test001-value3"
url = "https://maker.ifttt.com/trigger/%s/with/key/%s" % (event, apikey)
payload = { "value1" : value1, "value2" : value2, "value3" : value3 }
headers = {}
res = requests.post(url, data=payload, headers=headers)