mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 01:17:41 +08:00
优化:webhook
发送通道允许在消息模板
中直接使用自定义模板可用变量
#516
This commit is contained in:
parent
b35ba17beb
commit
a83f0da45a
@ -18,6 +18,7 @@ import com.idormy.sms.forwarder.utils.task.TaskUtils
|
|||||||
import com.xuexiang.xutil.net.NetworkUtils
|
import com.xuexiang.xutil.net.NetworkUtils
|
||||||
import com.xuexiang.xutil.resource.ResUtils.getString
|
import com.xuexiang.xutil.resource.ResUtils.getString
|
||||||
import java.io.Serializable
|
import java.io.Serializable
|
||||||
|
import java.net.URLEncoder
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@ -74,49 +75,49 @@ data class MsgInfo(
|
|||||||
fun getContentFromJson(jsonTemplate: String): String {
|
fun getContentFromJson(jsonTemplate: String): String {
|
||||||
var template = jsonTemplate.replace("null", "")
|
var template = jsonTemplate.replace("null", "")
|
||||||
if (TextUtils.isEmpty(template)) template = getString(R.string.tag_from)
|
if (TextUtils.isEmpty(template)) template = getString(R.string.tag_from)
|
||||||
return replaceTemplate(template, "", true)
|
return replaceTemplate(template, "", "Gson")
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("SimpleDateFormat")
|
@SuppressLint("SimpleDateFormat")
|
||||||
fun replaceTemplate(template: String, regexReplace: String = "", needJson: Boolean = false): String {
|
fun replaceTemplate(template: String, regexReplace: String = "", encoderName: String = ""): String {
|
||||||
return template.replaceTag(getString(R.string.tag_from), from, needJson)
|
return template.replaceTag(getString(R.string.tag_from), from, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_package_name), from, needJson)
|
.replaceTag(getString(R.string.tag_package_name), from, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_sms), content, needJson)
|
.replaceTag(getString(R.string.tag_sms), content, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_msg), content, needJson)
|
.replaceTag(getString(R.string.tag_msg), content, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_card_slot), simInfo, needJson)
|
.replaceTag(getString(R.string.tag_card_slot), simInfo, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_card_subid), subId.toString(), needJson)
|
.replaceTag(getString(R.string.tag_card_subid), subId.toString(), encoderName)
|
||||||
.replaceTag(getString(R.string.tag_title), simInfo, needJson)
|
.replaceTag(getString(R.string.tag_title), simInfo, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_uid), uid.toString(), needJson)
|
.replaceTag(getString(R.string.tag_uid), uid.toString(), encoderName)
|
||||||
.replaceTag(
|
.replaceTag(
|
||||||
getString(R.string.tag_receive_time),
|
getString(R.string.tag_receive_time),
|
||||||
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),
|
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),
|
||||||
needJson
|
encoderName
|
||||||
)
|
)
|
||||||
.replaceTag(
|
.replaceTag(
|
||||||
getString(R.string.tag_current_time),
|
getString(R.string.tag_current_time),
|
||||||
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()),
|
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()),
|
||||||
needJson
|
encoderName
|
||||||
)
|
)
|
||||||
.replaceTag(getString(R.string.tag_device_name), extraDeviceMark.trim(), needJson)
|
.replaceTag(getString(R.string.tag_device_name), extraDeviceMark.trim(), encoderName)
|
||||||
.replaceTag(getString(R.string.tag_app_version), AppUtils.getAppVersionName(), needJson)
|
.replaceTag(getString(R.string.tag_app_version), AppUtils.getAppVersionName(), encoderName)
|
||||||
.replaceTag(
|
.replaceTag(
|
||||||
getString(R.string.tag_call_type),
|
getString(R.string.tag_call_type),
|
||||||
CALL_TYPE_MAP[callType.toString()] ?: getString(R.string.unknown_call), needJson
|
CALL_TYPE_MAP[callType.toString()] ?: getString(R.string.unknown_call), encoderName
|
||||||
)
|
)
|
||||||
.replaceTag(getString(R.string.tag_ipv4), TaskUtils.ipv4, needJson)
|
.replaceTag(getString(R.string.tag_ipv4), TaskUtils.ipv4, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_ipv6), TaskUtils.ipv6, needJson)
|
.replaceTag(getString(R.string.tag_ipv6), TaskUtils.ipv6, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_ip_list), TaskUtils.ipList, needJson)
|
.replaceTag(getString(R.string.tag_ip_list), TaskUtils.ipList, encoderName)
|
||||||
.replaceTag(getString(R.string.tag_battery_pct), "%.0f%%".format(TaskUtils.batteryPct), needJson)
|
.replaceTag(getString(R.string.tag_battery_pct), "%.0f%%".format(TaskUtils.batteryPct), encoderName)
|
||||||
.replaceTag(getString(R.string.tag_battery_status), BatteryUtils.getStatus(TaskUtils.batteryStatus), needJson)
|
.replaceTag(getString(R.string.tag_battery_status), BatteryUtils.getStatus(TaskUtils.batteryStatus), encoderName)
|
||||||
.replaceTag(getString(R.string.tag_battery_plugged), BatteryUtils.getPlugged(TaskUtils.batteryPlugged), needJson)
|
.replaceTag(getString(R.string.tag_battery_plugged), BatteryUtils.getPlugged(TaskUtils.batteryPlugged), encoderName)
|
||||||
.replaceTag(getString(R.string.tag_battery_info), TaskUtils.batteryInfo, needJson)
|
.replaceTag(getString(R.string.tag_battery_info), TaskUtils.batteryInfo, encoderName)
|
||||||
.replaceTag(
|
.replaceTag(
|
||||||
getString(R.string.tag_battery_info_simple),
|
getString(R.string.tag_battery_info_simple),
|
||||||
"%.0f%%".format(TaskUtils.batteryPct)
|
"%.0f%%".format(TaskUtils.batteryPct)
|
||||||
+ with(BatteryUtils.getPlugged(TaskUtils.batteryPlugged)) {
|
+ with(BatteryUtils.getPlugged(TaskUtils.batteryPlugged)) {
|
||||||
if (this == getString(R.string.battery_unknown)) "" else " - $this"
|
if (this == getString(R.string.battery_unknown)) "" else " - $this"
|
||||||
},
|
},
|
||||||
needJson
|
encoderName
|
||||||
)
|
)
|
||||||
.replaceTag(
|
.replaceTag(
|
||||||
getString(R.string.tag_net_type), with(NetworkUtils.getNetStateType()) {
|
getString(R.string.tag_net_type), with(NetworkUtils.getNetStateType()) {
|
||||||
@ -124,10 +125,10 @@ data class MsgInfo(
|
|||||||
this.name
|
this.name
|
||||||
this.name.removePrefix("NET_")
|
this.name.removePrefix("NET_")
|
||||||
},
|
},
|
||||||
needJson
|
encoderName
|
||||||
)
|
)
|
||||||
.replaceAppNameTag(from, needJson)
|
.replaceAppNameTag(from, encoderName)
|
||||||
.replaceLocationTag(needJson)
|
.replaceLocationTag(encoderName)
|
||||||
.regexReplace(regexReplace)
|
.regexReplace(regexReplace)
|
||||||
.trim()
|
.trim()
|
||||||
}
|
}
|
||||||
@ -155,11 +156,11 @@ data class MsgInfo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//替换标签(支持正则替换)
|
//替换标签(支持正则替换)
|
||||||
private fun String.replaceTag(tag: String, info: String, needJson: Boolean = false, ignoreCase: Boolean = true): String {
|
private fun String.replaceTag(tag: String, info: String, encoderName: String = "", ignoreCase: Boolean = true): String {
|
||||||
var result = if (needJson) {
|
var result = when (encoderName) {
|
||||||
this.replace(tag, toJsonStr(info), ignoreCase)
|
"Gson" -> this.replace(tag, toJsonStr(info), ignoreCase)
|
||||||
} else {
|
"URLEncoder" -> this.replace(tag, URLEncoder.encode(info, "UTF-8"), ignoreCase)
|
||||||
this.replace(tag, info, ignoreCase)
|
else -> this.replace(tag, info, ignoreCase)
|
||||||
}
|
}
|
||||||
|
|
||||||
val tagName = tag.removePrefix("{{").removeSuffix("}}")
|
val tagName = tag.removePrefix("{{").removeSuffix("}}")
|
||||||
@ -171,10 +172,10 @@ data class MsgInfo(
|
|||||||
val replacement = it.groupValues[2]
|
val replacement = it.groupValues[2]
|
||||||
val temp = info.replace(regex.toRegex(), replacement)
|
val temp = info.replace(regex.toRegex(), replacement)
|
||||||
Log.d("MsgInfo", "tagRegex: regex=$regex, replacement=$replacement, temp=$temp")
|
Log.d("MsgInfo", "tagRegex: regex=$regex, replacement=$replacement, temp=$temp")
|
||||||
result = if (needJson) {
|
result = when (encoderName) {
|
||||||
result.replace(it.value, toJsonStr(temp))
|
"Gson" -> this.replace(it.value, toJsonStr(temp), ignoreCase)
|
||||||
} else {
|
"URLEncoder" -> this.replace(it.value, URLEncoder.encode(temp, "UTF-8"), ignoreCase)
|
||||||
result.replace(it.value, temp)
|
else -> this.replace(it.value, temp, ignoreCase)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("MsgInfo", "Failed to replace tagRegex: ${e.message}")
|
Log.e("MsgInfo", "Failed to replace tagRegex: ${e.message}")
|
||||||
@ -185,7 +186,7 @@ data class MsgInfo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//替换{{APP名称}}标签
|
//替换{{APP名称}}标签
|
||||||
private fun String.replaceAppNameTag(packageName: String, needJson: Boolean = false): String {
|
private fun String.replaceAppNameTag(packageName: String, encoderName: String = ""): String {
|
||||||
if (TextUtils.isEmpty(this)) return this
|
if (TextUtils.isEmpty(this)) return this
|
||||||
if (this.indexOf(getString(R.string.tag_app_name)) == -1) return this
|
if (this.indexOf(getString(R.string.tag_app_name)) == -1) return this
|
||||||
|
|
||||||
@ -206,23 +207,33 @@ data class MsgInfo(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (needJson) {
|
|
||||||
appName = toJsonStr(appName)
|
when (encoderName) {
|
||||||
|
"Gson" -> appName = toJsonStr(appName)
|
||||||
|
"URLEncoder" -> appName = URLEncoder.encode(appName, "UTF-8")
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.replaceTag(getString(R.string.tag_app_name), appName)
|
return this.replaceTag(getString(R.string.tag_app_name), appName)
|
||||||
}
|
}
|
||||||
|
|
||||||
//替换 {{定位信息}} 标签
|
//替换 {{定位信息}} 标签
|
||||||
private fun String.replaceLocationTag(needJson: Boolean = false): String {
|
private fun String.replaceLocationTag(encoderName: String = ""): String {
|
||||||
if (TextUtils.isEmpty(this)) return this
|
if (TextUtils.isEmpty(this)) return this
|
||||||
|
|
||||||
val location = HttpServerUtils.apiLocationCache
|
val location = HttpServerUtils.apiLocationCache
|
||||||
var locationStr = location.toString()
|
var locationStr = location.toString()
|
||||||
var address = location.address
|
var address = location.address
|
||||||
if (needJson) {
|
when (encoderName) {
|
||||||
|
"Gson" -> {
|
||||||
locationStr = toJsonStr(locationStr)
|
locationStr = toJsonStr(locationStr)
|
||||||
address = toJsonStr(address)
|
address = toJsonStr(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"URLEncoder" -> {
|
||||||
|
locationStr = URLEncoder.encode(locationStr, "UTF-8")
|
||||||
|
address = URLEncoder.encode(address, "UTF-8")
|
||||||
|
}
|
||||||
|
}
|
||||||
return this.replaceTag(getString(R.string.tag_location), locationStr)
|
return this.replaceTag(getString(R.string.tag_location), locationStr)
|
||||||
.replaceTag(getString(R.string.tag_location_longitude), location.longitude.toString())
|
.replaceTag(getString(R.string.tag_location_longitude), location.longitude.toString())
|
||||||
.replaceTag(getString(R.string.tag_location_latitude), location.latitude.toString())
|
.replaceTag(getString(R.string.tag_location_latitude), location.latitude.toString())
|
||||||
|
@ -113,6 +113,7 @@ class WebhookUtils {
|
|||||||
Log.d(TAG, "method = GET, Url = $requestUrl")
|
Log.d(TAG, "method = GET, Url = $requestUrl")
|
||||||
XHttp.get(requestUrl).keepJson(true)
|
XHttp.get(requestUrl).keepJson(true)
|
||||||
} else if (setting.method == "GET" && !TextUtils.isEmpty(webParams)) {
|
} else if (setting.method == "GET" && !TextUtils.isEmpty(webParams)) {
|
||||||
|
webParams = msgInfo.replaceTemplate(webParams, "", "URLEncoder")
|
||||||
webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8"))
|
webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8"))
|
||||||
.replace("[content]", URLEncoder.encode(content, "UTF-8"))
|
.replace("[content]", URLEncoder.encode(content, "UTF-8"))
|
||||||
.replace("[msg]", URLEncoder.encode(content, "UTF-8"))
|
.replace("[msg]", URLEncoder.encode(content, "UTF-8"))
|
||||||
@ -138,6 +139,7 @@ class WebhookUtils {
|
|||||||
Log.d(TAG, "method = GET, Url = $requestUrl")
|
Log.d(TAG, "method = GET, Url = $requestUrl")
|
||||||
XHttp.get(requestUrl).keepJson(true)
|
XHttp.get(requestUrl).keepJson(true)
|
||||||
} else if (webParams.isNotEmpty() && (isJson || webParams.startsWith("{"))) {
|
} else if (webParams.isNotEmpty() && (isJson || webParams.startsWith("{"))) {
|
||||||
|
webParams = msgInfo.replaceTemplate(webParams, "", "Gson")
|
||||||
val bodyMsg = webParams.replace("[from]", from)
|
val bodyMsg = webParams.replace("[from]", from)
|
||||||
.replace("[content]", escapeJson(content))
|
.replace("[content]", escapeJson(content))
|
||||||
.replace("[msg]", escapeJson(content))
|
.replace("[msg]", escapeJson(content))
|
||||||
@ -169,6 +171,7 @@ class WebhookUtils {
|
|||||||
"PATCH" -> XHttp.patch(requestUrl).keepJson(true)
|
"PATCH" -> XHttp.patch(requestUrl).keepJson(true)
|
||||||
else -> XHttp.post(requestUrl).keepJson(true)
|
else -> XHttp.post(requestUrl).keepJson(true)
|
||||||
}
|
}
|
||||||
|
webParams = msgInfo.replaceTemplate(webParams)
|
||||||
webParams.trim('&').split("&").forEach {
|
webParams.trim('&').split("&").forEach {
|
||||||
val sepIndex = it.indexOf("=")
|
val sepIndex = it.indexOf("=")
|
||||||
if (sepIndex != -1) {
|
if (sepIndex != -1) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user