Skip to content

Commit d06542c

Browse files
committed
initial version
1 parent 1339456 commit d06542c

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,83 @@
11
# ifttt-trigger
2+
23
Trigger ifttt recipes easily from command line
4+
5+
https://github.com/hossman/ifttt-trigger
6+
7+
## Pre-Reqs
8+
9+
* You must have perl installed, along with the LWP::UserAgent & JSON::XS packages
10+
* Create an IFTTT account if you don't already have one: http://ifttt.com/
11+
* Active the Maker channel: https://ifttt.com/maker
12+
* Make note of the "key" assigned to you
13+
* In the Usage examples below we'll assume it's YOUR_LONG_KEY
14+
* Create a Recipe that uses the Maker channel's "Recieve a web request" trigger
15+
* Chose an "Event Name" and make note of it
16+
* In the Usage examples below we'll assume it's EVENT_NAME
17+
18+
## Basic Usage
19+
20+
```bash
21+
$ ifttt-trigger.pl YOUR_LONG_KEY EVENT_NAME EVENT_NAME "This string will be the 'value1' input to your IFTTT Action"
22+
```
23+
24+
```bash
25+
$ ifttt-trigger.pl YOUR_LONG_KEY EVENT_NAME EVENT_NAME "You may have" "at most three" "input values"
26+
```
27+
28+
```bash
29+
# There's no requirement that any input values be specified if don't need them in your recipe
30+
$ ifttt-trigger.pl YOUR_LONG_KEY EVENT_NAME EVENT_NAME
31+
```
32+
33+
## Suggested Usage
34+
35+
If you have the "IF" App installed on your phone, and an IFTTT recipe that sends notifications to your phone using the message of `{{Value1}}` then you can add something like this to your `.bashrc`:
36+
37+
```bash
38+
# send alerts to phone via ifttt, or via notify-send if it fails for some reason
39+
function phone-alert {
40+
output=$($HOME/code/ifttt-trigger/ifttt-trigger.pl YOUR_LONG_KEY EVENT_NAME "$*");
41+
if [ "$?" -ne 0 ]; then
42+
notify-send -i computer-fail-symbolic "$output (phone alert failed)"
43+
fi
44+
}
45+
```
46+
47+
And then use it to make an alert pop up on your phone you when some long running commands finish:
48+
49+
```bash
50+
$ ant run-really-long-tests; phone-alert "Tests are done"
51+
```
52+
53+
Or to send diff messages based on the results:
54+
55+
```bash
56+
$ ant run-really-long-tests && phone-alert "Tests are done" || phone-alert "Tests failed"
57+
```
58+
59+
## Error Handling
60+
61+
If there is an error of any kind:
62+
63+
* Detailed failure information is written to STDERR
64+
* The input arguments are written to STDOUT
65+
* A Non-0 exit status is returned.
66+
67+
As shown in the "Suggested Usage" above, this allows you to chain this scipt with other fall back mechanisms for sending alerting you.
68+
69+
## License
70+
71+
Copyright 2016 Chris Hostetter
72+
73+
Licensed under the Apache License, Version 2.0 (the "License");
74+
you may not use this file except in compliance with the License.
75+
You may obtain a copy of the License at
76+
77+
http://www.apache.org/licenses/LICENSE-2.0
78+
79+
Unless required by applicable law or agreed to in writing, software
80+
distributed under the License is distributed on an "AS IS" BASIS,
81+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82+
See the License for the specific language governing permissions and
83+
limitations under the License.

ifttt-trigger.pl

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env perl
2+
3+
# Copyright 2016 Chris Hostetter
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
##################################################
18+
19+
# https://github.com/hossman/ifttt-trigger
20+
#
21+
# uses the ifttt Maker channel to trigger an event with some optional input
22+
# https://ifttt.com/maker
23+
#
24+
# If the HTTP request to fire the ifttt event fails for any reason, the original event input will be
25+
# written to STDOUT, and the script will die with the details of the HTTP error.
26+
#
27+
# this should (in theory) allow you to chain this script with something else as a fall back (ie: notify-send)
28+
# so you can still get *some* notification, while also accessing the full details of the error.
29+
30+
use strict;
31+
use warnings;
32+
use LWP::UserAgent;
33+
use JSON::XS qw(encode_json);
34+
35+
if (@ARGV < 2 or 5 < @ARGV) {
36+
die "Usage: $0 secret_key event [value1 [value2 [value3]]]\n";
37+
}
38+
39+
my $key = shift;
40+
my $event = shift;
41+
my %json_payload;
42+
my $var = 1;
43+
for my $value (@ARGV) {
44+
$json_payload{"value$var"} = $value;
45+
$var++;
46+
}
47+
48+
my $ua = new LWP::UserAgent();
49+
my $req = HTTP::Request->new( 'POST', "https://maker.ifttt.com/trigger/$event/with/key/$key" );
50+
$req->header( 'Content-Type' => 'application/json' );
51+
$req->content( encode_json(\%json_payload) );
52+
my $rsp = $ua->request( $req );
53+
54+
unless ($rsp->is_success) {
55+
print STDOUT @ARGV;
56+
die $rsp->as_string;
57+
}

0 commit comments

Comments
 (0)