优化:webhook发送通道的 webParams 新增可用标签

This commit is contained in:
pppscn 2022-02-21 16:01:21 +08:00
parent a1fa816e89
commit c357517f6f
3 changed files with 60 additions and 13 deletions

View File

@ -935,7 +935,7 @@ public class SenderActivity extends AppCompatActivity {
try {
SmsVo smsVo = new SmsVo(getString(R.string.test_phone_num), getString(R.string.test_sender_sms), new Date(), getString(R.string.test_sim_info));
SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, smsVo.getMobile(), smsVo.getSmsVoForSend());
SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, smsVo, "", "");
} catch (Exception e) {
ToastUtils.delayedShow(getString(R.string.failed_to_fwd) + e.getMessage(), 3000);
e.printStackTrace();

View File

@ -244,7 +244,7 @@ public class SendUtil {
WebNotifySettingVo webNotifySettingVo = JSON.parseObject(senderModel.getJsonSetting(), WebNotifySettingVo.class);
if (webNotifySettingVo != null) {
try {
SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), smsVo.getMobile(), smsVo.getSmsVoForSend(smsTemplate, regexReplace));
SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), smsVo, smsTemplate, regexReplace);
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Log.e(TAG, "senderSendMsg: SenderWebNotifyMsg error " + e.getMessage());

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.sender;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Base64;
@ -7,13 +8,16 @@ import android.util.Log;
import androidx.annotation.NonNull;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.utils.CertUtils;
import com.idormy.sms.forwarder.utils.Define;
import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -34,7 +38,9 @@ public class SenderWebNotifyMsg extends SenderBaseMsg {
static final String TAG = "SenderWebNotifyMsg";
public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, String from, String content) throws Exception {
public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, SmsVo smsVo, String smsTemplate, String regexReplace) throws Exception {
String from = smsVo.getMobile();
String content = smsVo.getSmsVoForSend(smsTemplate, regexReplace);
Log.i(TAG, "sendMsg webServer:" + webServer + " webParams:" + webParams + " from:" + from + " content:" + content);
if (webServer == null || webServer.isEmpty()) {
@ -42,6 +48,12 @@ public class SenderWebNotifyMsg extends SenderBaseMsg {
}
Long timestamp = System.currentTimeMillis();
String orgContent = smsVo.getContent();
String deviceMark = SettingUtil.getAddExtraDeviceMark().trim();
String appVersion = SettingUtil.getVersionName();
String simInfo = smsVo.getSimInfo();
@SuppressLint("SimpleDateFormat") String receiveTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(smsVo.getDate());
String sign = "";
if (secret != null && !secret.isEmpty()) {
String stringToSign = timestamp + "\n" + secret;
@ -64,9 +76,16 @@ public class SenderWebNotifyMsg extends SenderBaseMsg {
Log.d(TAG, "method = GET, Url = " + webServer);
request = new Request.Builder().url(webServer).get().build();
} else if (method.equals("GET") && !TextUtils.isEmpty(webParams)) {
webParams = webParams.replace("\n", "%0A")
.replace("[from]", URLEncoder.encode(from, "UTF-8"))
.replace("[msg]", URLEncoder.encode(content, "UTF-8"));
webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8"))
.replace("[content]", URLEncoder.encode(content, "UTF-8"))
.replace("[msg]", URLEncoder.encode(content, "UTF-8"))
.replace("[org_content]", URLEncoder.encode(orgContent, "UTF-8"))
.replace("[device_mark]", URLEncoder.encode(deviceMark, "UTF-8"))
.replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8"))
.replace("[title]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8"))
.replace("\n", "%0A");
if (secret != null && !secret.isEmpty()) {
webParams = webParams.replace("[timestamp]", String.valueOf(timestamp))
.replace("[sign]", URLEncoder.encode(sign, "UTF-8"));
@ -77,18 +96,33 @@ public class SenderWebNotifyMsg extends SenderBaseMsg {
request = new Request.Builder().url(webServer).get().build();
} else if (webParams != null && webParams.contains("[msg]")) {
String bodyMsg;
String Content_Type = "application/x-www-form-urlencoded";
String contentType = "application/x-www-form-urlencoded";
if (webParams.startsWith("{")) {
bodyMsg = content.replace("\n", "\\n");
bodyMsg = webParams.replace("[msg]", bodyMsg);
Content_Type = "application/json;charset=utf-8";
contentType = "application/json;charset=utf-8";
bodyMsg = webParams.replace("[from]", from)
.replace("[content]", escapeJson(content))
.replace("[msg]", escapeJson(content))
.replace("[org_content]", escapeJson(orgContent))
.replace("[device_mark]", escapeJson(deviceMark))
.replace("[app_version]", appVersion)
.replace("[title]", escapeJson(simInfo))
.replace("[card_slot]", escapeJson(simInfo))
.replace("[receive_time]", receiveTime);
} else {
bodyMsg = webParams.replace("[msg]", URLEncoder.encode(content, "UTF-8"));
bodyMsg = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8"))
.replace("[content]", URLEncoder.encode(content, "UTF-8"))
.replace("[msg]", URLEncoder.encode(content, "UTF-8"))
.replace("[org_content]", URLEncoder.encode(orgContent, "UTF-8"))
.replace("[device_mark]", URLEncoder.encode(deviceMark, "UTF-8"))
.replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8"))
.replace("[title]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8"));
}
RequestBody body = RequestBody.create(MediaType.parse(Content_Type), bodyMsg);
RequestBody body = RequestBody.create(MediaType.parse(contentType), bodyMsg);
request = new Request.Builder()
.url(webServer)
.addHeader("Content-Type", Content_Type)
.addHeader("Content-Type", contentType)
.method("POST", body)
.build();
Log.d(TAG, "method = POST webParams, Body = " + bodyMsg);
@ -142,4 +176,17 @@ public class SenderWebNotifyMsg extends SenderBaseMsg {
}
//JSON需要转义的字符
private static String escapeJson(String str) {
return str.replace("\n", "\\n")
.replace("\"", "\\\"")
.replace("\\/", "\\\\/")
.replace("\\b", "\\\\b")
.replace("\\f", "\\\\f")
.replace("\\t", "\\\\t")
.replace("\\n", "\\\\n")
.replace("\\r", "\\\\r")
.replace("\\u", "\\\\u");
}
}