Skip to content

Commit

Permalink
优化:webhook发送通道允许在消息模板中直接使用自定义模板可用变量 #516
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Aug 15, 2024
1 parent b35ba17 commit a83f0da
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
95 changes: 53 additions & 42 deletions app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.idormy.sms.forwarder.utils.task.TaskUtils
import com.xuexiang.xutil.net.NetworkUtils
import com.xuexiang.xutil.resource.ResUtils.getString
import java.io.Serializable
import java.net.URLEncoder
import java.text.SimpleDateFormat
import java.util.Date

Expand Down Expand Up @@ -74,60 +75,60 @@ data class MsgInfo(
fun getContentFromJson(jsonTemplate: String): String {
var template = jsonTemplate.replace("null", "")
if (TextUtils.isEmpty(template)) template = getString(R.string.tag_from)
return replaceTemplate(template, "", true)
return replaceTemplate(template, "", "Gson")
}

@SuppressLint("SimpleDateFormat")
fun replaceTemplate(template: String, regexReplace: String = "", needJson: Boolean = false): String {
return template.replaceTag(getString(R.string.tag_from), from, needJson)
.replaceTag(getString(R.string.tag_package_name), from, needJson)
.replaceTag(getString(R.string.tag_sms), content, needJson)
.replaceTag(getString(R.string.tag_msg), content, needJson)
.replaceTag(getString(R.string.tag_card_slot), simInfo, needJson)
.replaceTag(getString(R.string.tag_card_subid), subId.toString(), needJson)
.replaceTag(getString(R.string.tag_title), simInfo, needJson)
.replaceTag(getString(R.string.tag_uid), uid.toString(), needJson)
fun replaceTemplate(template: String, regexReplace: String = "", encoderName: String = ""): String {
return template.replaceTag(getString(R.string.tag_from), from, encoderName)
.replaceTag(getString(R.string.tag_package_name), from, encoderName)
.replaceTag(getString(R.string.tag_sms), content, encoderName)
.replaceTag(getString(R.string.tag_msg), content, encoderName)
.replaceTag(getString(R.string.tag_card_slot), simInfo, encoderName)
.replaceTag(getString(R.string.tag_card_subid), subId.toString(), encoderName)
.replaceTag(getString(R.string.tag_title), simInfo, encoderName)
.replaceTag(getString(R.string.tag_uid), uid.toString(), encoderName)
.replaceTag(
getString(R.string.tag_receive_time),
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),
needJson
encoderName
)
.replaceTag(
getString(R.string.tag_current_time),
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_app_version), AppUtils.getAppVersionName(), needJson)
.replaceTag(getString(R.string.tag_device_name), extraDeviceMark.trim(), encoderName)
.replaceTag(getString(R.string.tag_app_version), AppUtils.getAppVersionName(), encoderName)
.replaceTag(
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_ipv6), TaskUtils.ipv6, needJson)
.replaceTag(getString(R.string.tag_ip_list), TaskUtils.ipList, needJson)
.replaceTag(getString(R.string.tag_battery_pct), "%.0f%%".format(TaskUtils.batteryPct), needJson)
.replaceTag(getString(R.string.tag_battery_status), BatteryUtils.getStatus(TaskUtils.batteryStatus), needJson)
.replaceTag(getString(R.string.tag_battery_plugged), BatteryUtils.getPlugged(TaskUtils.batteryPlugged), needJson)
.replaceTag(getString(R.string.tag_battery_info), TaskUtils.batteryInfo, needJson)
.replaceTag(getString(R.string.tag_ipv4), TaskUtils.ipv4, encoderName)
.replaceTag(getString(R.string.tag_ipv6), TaskUtils.ipv6, encoderName)
.replaceTag(getString(R.string.tag_ip_list), TaskUtils.ipList, encoderName)
.replaceTag(getString(R.string.tag_battery_pct), "%.0f%%".format(TaskUtils.batteryPct), encoderName)
.replaceTag(getString(R.string.tag_battery_status), BatteryUtils.getStatus(TaskUtils.batteryStatus), encoderName)
.replaceTag(getString(R.string.tag_battery_plugged), BatteryUtils.getPlugged(TaskUtils.batteryPlugged), encoderName)
.replaceTag(getString(R.string.tag_battery_info), TaskUtils.batteryInfo, encoderName)
.replaceTag(
getString(R.string.tag_battery_info_simple),
"%.0f%%".format(TaskUtils.batteryPct)
+ with(BatteryUtils.getPlugged(TaskUtils.batteryPlugged)) {
if (this == getString(R.string.battery_unknown)) "" else " - $this"
},
needJson
encoderName
)
.replaceTag(
getString(R.string.tag_net_type), with(NetworkUtils.getNetStateType()) {
if (this == NetworkUtils.NetState.NET_NO || this == NetworkUtils.NetState.NET_UNKNOWN)
this.name
this.name.removePrefix("NET_")
},
needJson
encoderName
)
.replaceAppNameTag(from, needJson)
.replaceLocationTag(needJson)
.replaceAppNameTag(from, encoderName)
.replaceLocationTag(encoderName)
.regexReplace(regexReplace)
.trim()
}
Expand Down Expand Up @@ -155,11 +156,11 @@ data class MsgInfo(
}

//替换标签(支持正则替换)
private fun String.replaceTag(tag: String, info: String, needJson: Boolean = false, ignoreCase: Boolean = true): String {
var result = if (needJson) {
this.replace(tag, toJsonStr(info), ignoreCase)
} else {
this.replace(tag, info, ignoreCase)
private fun String.replaceTag(tag: String, info: String, encoderName: String = "", ignoreCase: Boolean = true): String {
var result = when (encoderName) {
"Gson" -> this.replace(tag, toJsonStr(info), ignoreCase)
"URLEncoder" -> this.replace(tag, URLEncoder.encode(info, "UTF-8"), ignoreCase)
else -> this.replace(tag, info, ignoreCase)
}

val tagName = tag.removePrefix("{{").removeSuffix("}}")
Expand All @@ -171,10 +172,10 @@ data class MsgInfo(
val replacement = it.groupValues[2]
val temp = info.replace(regex.toRegex(), replacement)
Log.d("MsgInfo", "tagRegex: regex=$regex, replacement=$replacement, temp=$temp")
result = if (needJson) {
result.replace(it.value, toJsonStr(temp))
} else {
result.replace(it.value, temp)
result = when (encoderName) {
"Gson" -> this.replace(it.value, toJsonStr(temp), ignoreCase)
"URLEncoder" -> this.replace(it.value, URLEncoder.encode(temp, "UTF-8"), ignoreCase)
else -> this.replace(it.value, temp, ignoreCase)
}
} catch (e: Exception) {
Log.e("MsgInfo", "Failed to replace tagRegex: ${e.message}")
Expand All @@ -185,7 +186,7 @@ data class MsgInfo(
}

//替换{{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 (this.indexOf(getString(R.string.tag_app_name)) == -1) return this

Expand All @@ -206,22 +207,32 @@ 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)
}

//替换 {{定位信息}} 标签
private fun String.replaceLocationTag(needJson: Boolean = false): String {
private fun String.replaceLocationTag(encoderName: String = ""): String {
if (TextUtils.isEmpty(this)) return this

val location = HttpServerUtils.apiLocationCache
var locationStr = location.toString()
var address = location.address
if (needJson) {
locationStr = toJsonStr(locationStr)
address = toJsonStr(address)
when (encoderName) {
"Gson" -> {
locationStr = toJsonStr(locationStr)
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)
.replaceTag(getString(R.string.tag_location_longitude), location.longitude.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class WebhookUtils {
Log.d(TAG, "method = GET, Url = $requestUrl")
XHttp.get(requestUrl).keepJson(true)
} else if (setting.method == "GET" && !TextUtils.isEmpty(webParams)) {
webParams = msgInfo.replaceTemplate(webParams, "", "URLEncoder")
webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8"))
.replace("[content]", URLEncoder.encode(content, "UTF-8"))
.replace("[msg]", URLEncoder.encode(content, "UTF-8"))
Expand All @@ -138,6 +139,7 @@ class WebhookUtils {
Log.d(TAG, "method = GET, Url = $requestUrl")
XHttp.get(requestUrl).keepJson(true)
} else if (webParams.isNotEmpty() && (isJson || webParams.startsWith("{"))) {
webParams = msgInfo.replaceTemplate(webParams, "", "Gson")
val bodyMsg = webParams.replace("[from]", from)
.replace("[content]", escapeJson(content))
.replace("[msg]", escapeJson(content))
Expand Down Expand Up @@ -169,6 +171,7 @@ class WebhookUtils {
"PATCH" -> XHttp.patch(requestUrl).keepJson(true)
else -> XHttp.post(requestUrl).keepJson(true)
}
webParams = msgInfo.replaceTemplate(webParams)
webParams.trim('&').split("&").forEach {
val sepIndex = it.indexOf("=")
if (sepIndex != -1) {
Expand Down

0 comments on commit a83f0da

Please sign in to comment.