Skip to content

Commit 6db7096

Browse files
committed
feat: add webhook format and request method options for SMS forwarding
1 parent 1dccae6 commit 6db7096

File tree

5 files changed

+44
-61
lines changed

5 files changed

+44
-61
lines changed

application/sms_forwarder/files/etc/init.d/sms_forwarder

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,18 @@ generate_api_config() {
186186
local webhook_url headers
187187
config_get webhook_url "$section" webhook_url
188188
config_get headers "$section" webhook_headers
189+
config_get format "$section" webhook_format
190+
config_get request_method "$section" webhook_request_method
189191

190192
[ -z "$webhook_url" ] && {
191193
echo "Error: Webhook requires webhook_url"
192194
return 1
193195
}
194196

195197
if [ -n "$headers" ]; then
196-
config_json="{\"webhook_url\":\"$webhook_url\",\"headers\":\"$headers\"}"
198+
config_json="{\"webhook_url\":\"$webhook_url\",\"headers\":\"$headers\",\"format\":\"$format\",\"request_method\":\"$request_method\"}"
197199
else
198-
config_json="{\"webhook_url\":\"$webhook_url\"}"
200+
config_json="{\"webhook_url\":\"$webhook_url\",\"format\":\"$format\",\"request_method\":\"$request_method\"}"
199201
fi
200202
;;
201203

application/sms_forwarder/files/usr/bin/sms_forward_webhook.sh

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fi
1717
# Extract configuration using jq or manual parsing
1818
WEBHOOK_URL=$(echo "$API_CONFIG" | jq -r '.webhook_url' 2>/dev/null)
1919
HEADERS=$(echo "$API_CONFIG" | jq -r '.headers' 2>/dev/null)
20+
FORMAT=$(echo "$API_CONFIG" | jq -r '.format' 2>/dev/null)
21+
REQUEST_METHOD=$(echo "$API_CONFIG" | jq -r '.request_method' 2>/dev/null)
2022

2123
# Fallback to manual parsing if jq fails
2224
if [ -z "$WEBHOOK_URL" ] || [ "$WEBHOOK_URL" = "null" ]; then
@@ -32,68 +34,28 @@ if [ -z "$WEBHOOK_URL" ]; then
3234
exit 1
3335
fi
3436

35-
# Prepare JSON payload using jq if available
36-
if command -v jq >/dev/null 2>&1; then
37-
JSON_PAYLOAD=$(jq -n \
38-
--arg type "sms" \
39-
--arg title "QModem SMS: ($SMS_SENDER)" \
40-
--arg timestamp "$SMS_TIME" \
41-
--arg sender "$SMS_SENDER" \
42-
--arg content "$SMS_CONTENT" \
43-
'{
44-
type: $type,
45-
title: $title,
46-
timestamp: $timestamp,
47-
sender: $sender,
48-
content: $content
49-
}')
37+
# Prepare payload based on format
38+
if [ -z "$FORMAT" ] || [ "$FORMAT" = "null" ];then
39+
payload="$SMS_SENDER/$SMS_CONTENT($SMS_TIME)"
5040
else
51-
# Fallback JSON generation
52-
JSON_PAYLOAD="{
53-
\"type\": \"sms\",
54-
\"title\": \"QModem SMS: ($SMS_SENDER)\",
55-
\"timestamp\": \"$SMS_TIME\",
56-
\"sender\": \"$SMS_SENDER\",
57-
\"content\": \"$SMS_CONTENT\"
58-
}"
41+
payload=$(echo "$FORMAT" | sed "s/{SENDER}/$SMS_SENDER/g; s/{TIME}/$SMS_TIME/g; s/{CONTENT}/$SMS_CONTENT/g")
5942
fi
60-
61-
# Try curl first, then wget
62-
if command -v curl >/dev/null 2>&1; then
63-
CURL_CMD="curl -X POST \"$WEBHOOK_URL\""
64-
CURL_CMD="$CURL_CMD -H \"Content-Type: application/json\""
65-
66-
# Add custom headers if provided
67-
if [ -n "$HEADERS" ]; then
68-
CURL_CMD="$CURL_CMD -H \"$HEADERS\""
69-
fi
70-
71-
CURL_CMD="$CURL_CMD -d '$JSON_PAYLOAD'"
72-
CURL_CMD="$CURL_CMD --connect-timeout 10 --max-time 30"
73-
74-
eval "$CURL_CMD"
75-
elif command -v wget >/dev/null 2>&1; then
76-
# Create temporary file for POST data
77-
TEMP_FILE=$(mktemp)
78-
echo "$JSON_PAYLOAD" > "$TEMP_FILE"
79-
80-
WGET_CMD="wget -O-"
81-
WGET_CMD="$WGET_CMD --header=\"Content-Type: application/json\""
82-
83-
# Add custom headers if provided
84-
if [ -n "$HEADERS" ]; then
85-
WGET_CMD="$WGET_CMD --header=\"$HEADERS\""
86-
fi
87-
88-
WGET_CMD="$WGET_CMD --post-file=\"$TEMP_FILE\""
89-
WGET_CMD="$WGET_CMD --timeout=30"
90-
WGET_CMD="$WGET_CMD \"$WEBHOOK_URL\""
91-
92-
eval "$WGET_CMD"
93-
rm -f "$TEMP_FILE"
43+
# Prepare curl command
44+
if [ -z "$REQUEST_METHOD" ] || [ "$REQUEST_METHOD" = "null" ]; then
45+
REQUEST_METHOD="GET"
46+
fi
47+
CURL_CMD="curl -s -X $REQUEST_METHOD"
48+
[ -n "$HEADERS" ] && CURL_CMD="$CURL_CMD -H \"$HEADERS\""
49+
if [ "$REQUEST_METHOD" = "POST" ]; then
50+
CURL_CMD="$CURL_CMD -d \"$payload\""
9451
else
95-
echo "Error: Neither curl nor wget available"
96-
exit 1
52+
# replay space and special characters in payload for URL
53+
payload=$(echo "$payload" | sed 's/ /%20/g;s/!/%21/g;s/"/%22/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g;s/*/%2A/g;s/+/%2B/g;s/,/%2C/g;s/;/%3B/g;s/=/%3D/g;s/?/%3F/g;s/@/%40/g;s/\[/%5B/g;s/\\/%5C/g;s/\]/%5D/g;s/\^/%5E/g;s/_/%5F/g;s/`/%60/g;s/{/%7B/g;s/|/%7C/g;s/}/%7D/g;s/~/%7E/g')
54+
WEBHOOK_URL="$WEBHOOK_URL/$payload"
9755
fi
56+
CURL_CMD="$CURL_CMD \"$WEBHOOK_URL\""
57+
# Execute curl command
58+
echo "Executing curl command: $CURL_CMD"
59+
eval $CURL_CMD
9860

9961
exit $?

luci/luci-app-qmodem-sms/luasrc/model/cbi/qmodem_sms/sms_forward.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ api_type:value("webhook", translate("Webhook"))
112112
api_type:value("serverchan", translate("ServerChan"))
113113
api_type:value("pushdeer", translate("PushDeer"))
114114
api_type:value("custom_script", translate("Custom Script"))
115+
api_type:value("feishu", translate("Feishu Bot"))
115116

116117
-- 删除已转发短信选项
117118
delete_after_forward = s2:option(Flag, "delete_after_forward", translate("Delete After Forward"))

luci/luci-app-qmodem-sms/luasrc/model/cbi/qmodem_sms/sms_forward_extedit.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ webhook_headers = s2:option(Value, "webhook_headers", translate("Headers (option
100100
webhook_headers:depends("api_type", "webhook")
101101
webhook_headers.placeholder = "Authorization: Bearer token"
102102

103+
webhook_format = s2:option(Value, "webhook_format", translate("Message Format (optional)"))
104+
webhook_format:depends("api_type", "webhook")
105+
webhook_format.placeholder = "{SENDER}/{CONTENT}({TIME})"
106+
webhook_format.description = translate("Custom message format using placeholders:") .. " {SENDER}, {CONTENT}, {TIME}"
107+
webhook_request_method = s2:option(ListValue, "webhook_request_method", translate("Request Method"))
108+
webhook_request_method:depends("api_type", "webhook")
109+
webhook_request_method:value("GET", "GET")
110+
webhook_request_method:value("POST", "POST")
111+
103112
-- ServerChan 配置
104113
serverchan_token = s2:option(Value, "serverchan_token", translate("Token"))
105114
serverchan_token:depends("api_type", "serverchan")

luci/luci-app-qmodem-sms/po/zh_Hans/modem_sms.po

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ msgstr "Telegram机器人"
107107
msgid "Generic Webhook"
108108
msgstr "通用Webhook"
109109

110+
msgid "Message Format (optional)"
111+
msgstr "消息格式(可选)"
112+
113+
msgid "Custom message format using placeholders:"
114+
msgstr "使用占位符的自定义消息格式:"
115+
116+
msgid "Request Method"
117+
msgstr "请求方法"
118+
110119
msgid "Custom Script"
111120
msgstr "自定义脚本"
112121

0 commit comments

Comments
 (0)