-
Notifications
You must be signed in to change notification settings - Fork 0
/
typetalk
executable file
路140 lines (115 loc) 路 2.68 KB
/
typetalk
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
#!/bin/sh
# Usage: typetalk [-m MESSAGE] [-t TALK] [MESSAGE_FILE...]
#
# Options:
# -m MESSAGE a message to post
# -t TALK talk id to attach
# TYPETALK_TOPIC=123456
# TYPETALK_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: ${ACCESSTOKEN_PATH:="${HOME}/.typetalk_config"}
MAX_MESSAGE_LENGTH="4000"
eecho() { echo "$*" > /dev/stderr; }
error() { eecho "Error: $*"; exit 1; }
command_exists() { command -v "$1" 1>/dev/null 2>&1; }
## Arguments:
## commands... : required commands
##
## Return:
## returns 0 if all commands are found, exit 1 if not
##
require_commands() {
(
missing=0
for cmd; do
if ! command_exists "$cmd"; then
missing=1
eecho "command missing - '$cmd'"
fi
done
if [ "$missing" -gt 0 ]; then
eecho
eecho "To run this script, please install above commands."
return 1
fi
) || exit 1
}
print_help() {
sed -n '2,/^$/s/^# //p' "$0"
}
# If there are configured environment variables, exit with 0
config_exist() {
[ -n "${TYPETALK_TOPIC}" -a -n "${TYPETALK_TOKEN}" ]
}
format_json() {
if command_exists jq; then
jq .
else
cat
fi
}
load_config() {
if config_exist; then
return
fi
if ! [ -f "${ACCESSTOKEN_PATH}" ]; then
error "No token specified. Configure an API token by configuration file or environment variables"
fi
if [ -z "`find "${ACCESSTOKEN_PATH}" -type f -perm 600 -print`" ]; then
error "A configuration SHOULD NOT be readable or writable by other user. Execute \`chmod 600 \"${ACCESSTOKEN_PATH}\"\`."
fi
. "${ACCESSTOKEN_PATH}"
}
obtain_message() {
if [ "$#" -ge 1 ]; then
cat -- "$@"
else
cat
fi
}
###
require_commands find sed grep cat wc printf curl
while getopts "hm:f:t:" option; do
case "${option}" in
h)
print_help
exit 1
;;
m)
message="$OPTARG"
;;
t)
talk="$OPTARG"
if ! echo "$talk" | grep -q '^[0-9]\+$'; then
error "Invalid format for talk: ${talk}"
fi
;;
f)
# Reserved for file upload
eecho "currently not supported, ignored."
;;
\?)
exit 1
;;
esac
done
shift $(($OPTIND - 1))
load_config
if [ -z "${message+x}" ]; then
message="`obtain_message | head -c"$(($MAX_MESSAGE_LENGTH * 4))"`"
fi
message_length="`printf -- "%s" "${message}" | wc -m`"
if ! [ "$message_length" -gt 0 ]; then
error "message is empty."
elif ! [ "$message_length" -le "$MAX_MESSAGE_LENGTH" ]; then
error "Too long message. A length of message should be <= $MAX_MESSAGE_LENGTH."
fi
params=""
if [ -n "$talk" ]; then
params="$params --data-urlencode talkIds[]=$talk"
fi
curl --silent \
--data-urlencode 'message='"$message" \
--data-urlencode 'typetalkToken='"${TYPETALK_TOKEN}" \
$params \
'https://typetalk.com/api/v1/topics/'"${TYPETALK_TOPIC}" |
format_json