aboutsummaryrefslogtreecommitdiffstats
path: root/tools/send_twilio.sh
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-11 18:50:37 +0800
committerGitHub <noreply@github.com>2024-07-11 18:50:37 +0800
commitb56159903bb16dbabec1a8767702c3c0865ef256 (patch)
tree22519258ef7bcd179c9afc75c440cf69dd01b4f4 /tools/send_twilio.sh
parent1f784e3db509676e825c443cd1a8c63747069103 (diff)
downloadllm-functions-docker-b56159903bb16dbabec1a8767702c3c0865ef256.tar.gz
feat: add send_twilio tool (#72)
Diffstat (limited to 'tools/send_twilio.sh')
-rwxr-xr-xtools/send_twilio.sh44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/send_twilio.sh b/tools/send_twilio.sh
new file mode 100755
index 0000000..cb9717e
--- /dev/null
+++ b/tools/send_twilio.sh
@@ -0,0 +1,44 @@
+#!/usr/bin/env bash
+set -e
+
+# @describe Send SMS or Twilio Messaging Channels messages using Twilio API.
+# @env TWILIO_ACCOUNT_SID! The twilio account sid
+# @env TWILIO_AUTH_TOKEN! The twilio auth token
+# @env TWILIO_FROM_NUMBER! The twilio from number
+# @option --to-number! The recipient's phone number. Prefix with 'whatsapp:' for WhatsApp messages, e.g. whatsapp:+1234567890
+# @option --message! The content of the message to be sent
+
+main() {
+ from_number="$TWILIO_FROM_NUMBER"
+ to_number="$argc_to_number"
+ if [[ "$to_number" == 'whatsapp:'* ]]; then
+ from_number="whatsapp:$from_number"
+ fi
+ if [[ "$to_number" != 'whatsapp:'* && "$to_number" != '+'* ]]; then
+ to_number="+$to_number"
+ fi
+ res="$(curl -s -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
+ -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN" \
+ -w "\n%{http_code}" \
+ --data-urlencode "From=$from_number" \
+ --data-urlencode "To=$to_number" \
+ --data-urlencode "Body=$argc_message")"
+ status="$(echo "$res" | tail -n 1)"
+ body="$(echo "$res" | head -n -1)"
+ if [[ "$status" -ge 200 && "$status" -lt 300 ]]; then
+ if [[ "$(echo "$body" | jq -r 'has("sid")')" == "true" ]]; then
+ echo "Message sent successfully" >> "$LLM_OUTPUT"
+ else
+ _die "$body"
+ fi
+ else
+ _die "$body"
+ fi
+}
+
+_die() {
+ echo "$*" >&2
+ exit 1
+}
+
+eval "$(argc --argc-eval "$0" "$@")"