-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathout
executable file
·62 lines (52 loc) · 1.66 KB
/
out
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
#!/bin/bash
set -e
cd "${1}"
# Colors
RED='\033[1;31m'
NC='\033[0m' # No Color
exec 3>&1
exec 1>&2
# for jq
PATH=/usr/local/bin:$PATH
#
# Get parameters from the pipeline
#
payload=$(mktemp /tmp/resource-in.XXXXXX)
cat > "$payload" <&0
BOT_TOKEN=$(jq -r '(.source.bot_token // "")' < "$payload")
text="$(jq -r '(.params.text // "text is empty")' < "${payload}")"
CHAT_ID="$(jq -r '(.params.chat_id // "")' < "${payload}")"
text_file_path="$(jq -r '(.params.text_file // "")' < "${payload}")"
PARSE_MODE="$(jq -r '(.params.parse_mode // "Markdown")' < "${payload}")"
#
#Select a message, text or text_file. text_file have higher priority.
#
if [ -n "$text_file_path" ]; then
text=$(cat $text_file_path)
fi
#
# Check mandatory parameters
#
if [ "$PARSE_MODE" != "HTML" ] && [ "$PARSE_MODE" != "Markdown" ]; then
printf "\n ${RED} Error! Incorrect parse mode. Please use one of HTML or Markdown in params.parse_mode.${NC}\n"
exit 1
fi
if [ -z "$BOT_TOKEN" ]; then
printf "\n ${RED} Error! The Telegram bot token is not specified! Please, add the 'source.bot_token' property to your resource declaration. ${NC}\n"
exit 1
fi
if [ -z "$CHAT_ID" ]; then
printf "\n ${RED} Error! The Telegram chat ID is not specified! Please, add the 'params.chat_id' property to your resource parameters. ${NC}\n"
exit 1
fi
#
# Send the message to the Telegram
#
text=$(echo "$text" | envsubst)
URL="https://api.telegram.org/bot${BOT_TOKEN}/sendMessage"
curl -X POST --data chat_id=${CHAT_ID//\"/} --data text="${text}" --data parse_mode="$PARSE_MODE" "$URL"
#
# print the output
#
timestamp="$(jq -n "{version:{timestamp:\"$(date +%s)\"}}")"
echo "$timestamp" | jq -s add >&3