]>
Commit | Line | Data |
---|---|---|
d74dfb1f | 1 | #!/usr/bin/env sh |
2 | ||
3 | #Support ifttt.com webhooks api | |
4 | ||
5 | #IFTTT_API_KEY="xxxx" | |
6 | #IFTTT_EVENT_NAME="yyyy" | |
7 | ||
8 | #IFTTT_SUBJECT_KEY="value1|value2|value3" #optional, use "value1" as default | |
9 | #IFTTT_CONTENT_KEY="value1|value2|value3" #optional, use "value2" as default | |
10 | ||
11 | _IFTTT_AVAIL_MSG_KEYS="value1,value2,value3" | |
12 | ||
13 | # subject content statusCode | |
14 | ifttt_send() { | |
15 | _subject="$1" | |
16 | _content="$2" | |
17 | _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped | |
18 | _debug "_subject" "$_subject" | |
19 | _debug "_content" "$_content" | |
20 | _debug "_statusCode" "$_statusCode" | |
21 | ||
22 | IFTTT_API_KEY="${IFTTT_API_KEY:-$(_readaccountconf_mutable IFTTT_API_KEY)}" | |
23 | if [ -z "$IFTTT_API_KEY" ]; then | |
24 | IFTTT_API_KEY="" | |
25 | _err "You didn't specify a ifttt webhooks api key IFTTT_API_KEY yet." | |
26 | _err "You can get yours from https://ifttt.com" | |
27 | return 1 | |
28 | fi | |
29 | _saveaccountconf_mutable IFTTT_API_KEY "$IFTTT_API_KEY" | |
30 | ||
31 | IFTTT_EVENT_NAME="${IFTTT_EVENT_NAME:-$(_readaccountconf_mutable IFTTT_EVENT_NAME)}" | |
32 | if [ -z "$IFTTT_EVENT_NAME" ]; then | |
33 | IFTTT_EVENT_NAME="" | |
34 | _err "You didn't specify a ifttt webhooks event name IFTTT_EVENT_NAME yet." | |
35 | return 1 | |
36 | fi | |
37 | _saveaccountconf_mutable IFTTT_EVENT_NAME "$IFTTT_EVENT_NAME" | |
38 | ||
39 | IFTTT_SUBJECT_KEY="${IFTTT_SUBJECT_KEY:-$(_readaccountconf_mutable IFTTT_SUBJECT_KEY)}" | |
40 | if [ -z "$IFTTT_SUBJECT_KEY" ]; then | |
41 | IFTTT_SUBJECT_KEY="value1" | |
42 | _info "The IFTTT_SUBJECT_KEY is not set, so use the default value1 as key." | |
43 | elif ! _hasfield "$_IFTTT_AVAIL_MSG_KEYS" "$IFTTT_SUBJECT_KEY"; then | |
44 | _err "The IFTTT_SUBJECT_KEY \"$IFTTT_SUBJECT_KEY\" is not available, should be one of $_IFTTT_AVAIL_MSG_KEYS" | |
45 | IFTTT_SUBJECT_KEY="" | |
46 | return 1 | |
47 | else | |
48 | _saveaccountconf_mutable IFTTT_SUBJECT_KEY "$IFTTT_SUBJECT_KEY" | |
49 | fi | |
50 | ||
51 | IFTTT_CONTENT_KEY="${IFTTT_CONTENT_KEY:-$(_readaccountconf_mutable IFTTT_CONTENT_KEY)}" | |
52 | if [ -z "$IFTTT_CONTENT_KEY" ]; then | |
53 | IFTTT_CONTENT_KEY="value2" | |
54 | _info "The IFTTT_CONTENT_KEY is not set, so use the default value2 as key." | |
55 | elif ! _hasfield "$_IFTTT_AVAIL_MSG_KEYS" "$IFTTT_CONTENT_KEY"; then | |
56 | _err "The IFTTT_CONTENT_KEY \"$IFTTT_CONTENT_KEY\" is not available, should be one of $_IFTTT_AVAIL_MSG_KEYS" | |
57 | IFTTT_CONTENT_KEY="" | |
58 | return 1 | |
59 | else | |
60 | _saveaccountconf_mutable IFTTT_CONTENT_KEY "$IFTTT_CONTENT_KEY" | |
61 | fi | |
62 | ||
63 | if [ "$IFTTT_SUBJECT_KEY" = "$IFTTT_CONTENT_KEY" ]; then | |
64 | IFTTT_SUBJECT_KEY="" | |
65 | IFTTT_CONTENT_KEY="" | |
66 | _err "The IFTTT_SUBJECT_KEY must not be same as IFTTT_CONTENT_KEY." | |
67 | return 1 | |
68 | fi | |
69 | ||
70 | IFTTT_API_URL="https://maker.ifttt.com/trigger/$IFTTT_EVENT_NAME/with/key/$IFTTT_API_KEY" | |
71 | ||
72 | _content=$(echo "$_content" | _json_encode) | |
73 | _subject=$(echo "$_subject" | _json_encode) | |
74 | _data="{\"$IFTTT_SUBJECT_KEY\": \"$_subject\", \"$IFTTT_CONTENT_KEY\": \"$_content\"}" | |
75 | ||
143eac09 | 76 | response="$(_post "$_data" "$IFTTT_API_URL" "" "POST" "application/json")" |
77 | ||
78 | if [ "$?" = "0" ] && _contains "$response" "Congratulations"; then | |
79 | _info "IFTTT webhooks event fired success." | |
80 | return 0 | |
d74dfb1f | 81 | fi |
143eac09 | 82 | |
d74dfb1f | 83 | _err "IFTTT webhooks event fired error." |
84 | _err "$response" | |
85 | return 1 | |
86 | } |