-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpling
More file actions
executable file
·142 lines (121 loc) · 4.03 KB
/
pling
File metadata and controls
executable file
·142 lines (121 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
#
# pling — Send notifications to your Pling app
#
# Usage:
# pling "Deploy completed" # Simple notification
# pling -t "Alert" -m "Server is down" -p high # With priority
# echo "output" | pling -t "Build log" # Pipe stdin as body
# pling -t "Done" -c ci -u https://example.com # With channel and URL
#
# Environment:
# PLING_TOKEN Your API token (required)
# PLING_API_URL API endpoint (default: https://pling-api.jeanrobert-nino-layton.workers.dev)
#
# Install:
# curl -sL https://raw.githubusercontent.com/Jeramo/pling-cli/main/pling -o /usr/local/bin/pling
# chmod +x /usr/local/bin/pling
# export PLING_TOKEN="your-token-here"
set -euo pipefail
API_URL="${PLING_API_URL:-https://pling-api.jeanrobert-nino-layton.workers.dev}"
TOKEN="${PLING_TOKEN:-}"
# Defaults
TITLE=""
MESSAGE=""
BODY=""
PRIORITY="normal"
CHANNEL="default"
URL=""
usage() {
cat <<EOF
Usage: pling [OPTIONS] [TITLE]
Send a notification to your Pling app.
Options:
-t, --title TITLE Notification title (or pass as first positional arg)
-m, --message MESSAGE Short message
-b, --body BODY Long-form body text
-p, --priority PRIORITY Priority: low, normal, high, critical (default: normal)
-c, --channel CHANNEL Channel name (default: default)
-u, --url URL Action URL
-k, --token TOKEN API token (or set PLING_TOKEN env var)
-h, --help Show this help
Pipe mode:
command | pling -t "Title" # stdin becomes the body
Examples:
pling "Deploy finished"
pling -t "CPU Alert" -p critical -c monitoring
make build 2>&1 | pling -t "Build output" -c ci
pling -t "Check logs" -u https://grafana.example.com/dashboard
EOF
exit 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-t|--title) TITLE="$2"; shift 2 ;;
-m|--message) MESSAGE="$2"; shift 2 ;;
-b|--body) BODY="$2"; shift 2 ;;
-p|--priority) PRIORITY="$2"; shift 2 ;;
-c|--channel) CHANNEL="$2"; shift 2 ;;
-u|--url) URL="$2"; shift 2 ;;
-k|--token) TOKEN="$2"; shift 2 ;;
-h|--help) usage ;;
-*) echo "Unknown option: $1" >&2; exit 1 ;;
*) # Positional arg = title
if [[ -z "$TITLE" ]]; then
TITLE="$1"
else
MESSAGE="$1"
fi
shift ;;
esac
done
# Read from stdin if available (non-TTY)
if [[ ! -t 0 ]]; then
BODY="$(cat)"
fi
# Validate
if [[ -z "$TOKEN" ]]; then
echo "Error: PLING_TOKEN not set. Export it or pass -k TOKEN." >&2
exit 1
fi
if [[ -z "$TITLE" ]]; then
if [[ -n "$BODY" ]]; then
# Use first line of body as title
TITLE="$(echo "$BODY" | head -1)"
else
echo "Error: Title is required. Pass it as an argument or use -t." >&2
exit 1
fi
fi
# Build JSON payload
JSON=$(cat <<PAYLOAD
{
"token": $(printf '%s' "$TOKEN" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))"),
"title": $(printf '%s' "$TITLE" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))"),
"priority": "$PRIORITY",
"channel": "$CHANNEL"
PAYLOAD
)
if [[ -n "$MESSAGE" ]]; then
JSON="$JSON, \"message\": $(printf '%s' "$MESSAGE" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))")"
fi
if [[ -n "$BODY" ]]; then
JSON="$JSON, \"body\": $(printf '%s' "$BODY" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))")"
fi
if [[ -n "$URL" ]]; then
JSON="$JSON, \"url\": $(printf '%s' "$URL" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))")"
fi
JSON="$JSON}"
# Send
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API_URL/api/push" \
-H "Content-Type: application/json" \
-d "$JSON")
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then
echo "Sent: $TITLE"
else
echo "Error ($HTTP_CODE): $RESPONSE_BODY" >&2
exit 1
fi