转发到webhook增加支持自定义post数据,并支持Json数据提交

This commit is contained in:
KoolOrange 2021-07-24 14:36:48 +08:00
parent e98118d993
commit f4096845df
6 changed files with 70 additions and 23 deletions

View File

@ -657,6 +657,7 @@ public class SenderActivity extends AppCompatActivity {
});
}
//webhook
private void setWebNotify(final SenderModel senderModel) {
WebNotifySettingVo webNotifySettingVo = null;
//try phrase json setting
@ -674,6 +675,8 @@ public class SenderActivity extends AppCompatActivity {
if (senderModel != null) editTextWebNotifyName.setText(senderModel.getName());
final EditText editTextWebNotifyWebServer = view1.findViewById(R.id.editTextWebNotifyWebServer);
if (webNotifySettingVo != null) editTextWebNotifyWebServer.setText(webNotifySettingVo.getWebServer());
final EditText editTextWebNotifyWebParams = view1.findViewById(R.id.editTextWebNotifyWebParams);
if (webNotifySettingVo != null) editTextWebNotifyWebParams.setText(webNotifySettingVo.getwebParams());
final EditText editTextWebNotifySecret = view1.findViewById(R.id.editTextWebNotifySecret);
if (webNotifySettingVo != null) editTextWebNotifySecret.setText(webNotifySettingVo.getSecret());
final RadioGroup radioGroupWebNotifyMethod = (RadioGroup) view1.findViewById(R.id.radioGroupWebNotifyMethod);
@ -692,38 +695,29 @@ public class SenderActivity extends AppCompatActivity {
buttonbebnotifyok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WebNotifySettingVo webNotifySettingVoNew = new WebNotifySettingVo(
editTextWebNotifyWebServer.getText().toString(),
editTextWebNotifySecret.getText().toString(),
(radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST"),
editTextWebNotifyWebParams.getText().toString()
);
if (senderModel == null) {
SenderModel newSenderModel = new SenderModel();
newSenderModel.setName(editTextWebNotifyName.getText().toString());
newSenderModel.setType(TYPE_WEB_NOTIFY);
newSenderModel.setStatus(STATUS_ON);
WebNotifySettingVo webNotifySettingVoNew = new WebNotifySettingVo(
editTextWebNotifyWebServer.getText().toString(),
editTextWebNotifySecret.getText().toString(),
(radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST")
);
newSenderModel.setJsonSetting(JSON.toJSONString(webNotifySettingVoNew));
SenderUtil.addSender(newSenderModel);
initSenders();
adapter.add(senderModels);
} else {
senderModel.setName(editTextWebNotifyName.getText().toString());
senderModel.setType(TYPE_WEB_NOTIFY);
senderModel.setStatus(STATUS_ON);
WebNotifySettingVo webNotifySettingVoNew = new WebNotifySettingVo(
editTextWebNotifyWebServer.getText().toString(),
editTextWebNotifySecret.getText().toString(),
(radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST")
);
senderModel.setJsonSetting(JSON.toJSONString(webNotifySettingVoNew));
SenderUtil.updateSender(senderModel);
initSenders();
adapter.update(senderModels);
}
initSenders();
adapter.update(senderModels);
show.dismiss();
}
});
buttonbebnotifydel.setOnClickListener(new View.OnClickListener() {
@ -741,11 +735,12 @@ public class SenderActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
String webServer = editTextWebNotifyWebServer.getText().toString();
String webParams = editTextWebNotifyWebParams.getText().toString();
String secret = editTextWebNotifySecret.getText().toString();
String method = radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST";
if (!webServer.isEmpty()) {
try {
SenderWebNotifyMsg.sendMsg(0, handler, webServer, secret, method, "SmsForwarder Title", "测试内容(content)@" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
SenderWebNotifyMsg.sendMsg(0, handler, webServer, webParams,secret, method, "SmsForwarder Title", "测试内容(content)@" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
} catch (Exception e) {
Toast.makeText(SenderActivity.this, "发送失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
@ -757,6 +752,7 @@ public class SenderActivity extends AppCompatActivity {
});
}
//企业微信群机器人
private void setQYWXGroupRobot(final SenderModel senderModel) {
QYWXGroupRobotSettingVo qywxGroupRobotSettingVo = null;
//try phrase json setting

View File

@ -8,14 +8,16 @@ public class WebNotifySettingVo implements Serializable {
private String webServer;
private String secret;
private String method;
private String webParams;
public WebNotifySettingVo() {
}
public WebNotifySettingVo(String webServer, String secret, String method) {
public WebNotifySettingVo(String webServer, String secret, String method, String webParams) {
this.webServer = webServer;
this.secret = secret;
this.method = method;
this.webParams = webParams;
}
public String getWebServer() {
@ -26,6 +28,14 @@ public class WebNotifySettingVo implements Serializable {
this.webServer = webServer;
}
public String getwebParams() {
return webParams;
}
public void setWebParams(String webParams) {
this.webParams = webParams;
}
public String getSecret() {
return secret;
}
@ -49,4 +59,6 @@ public class WebNotifySettingVo implements Serializable {
return R.id.radioWebNotifyMethodGet;
}
}
}

View File

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

View File

@ -16,6 +16,7 @@ import javax.crypto.spec.SecretKeySpec;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -28,8 +29,8 @@ public class SenderWebNotifyMsg {
static String TAG = "SenderWebNotifyMsg";
public static void sendMsg(final long logId, final Handler handError, String webServer, String secret, String method, String from, String content) throws Exception {
Log.i(TAG, "sendMsg webServer:" + webServer + " from:" + from + " content:" + content);
public static void sendMsg(final long logId, final Handler handError, String webServer, String webParams, String secret, String method, String from, String content) throws Exception {
Log.i(TAG, "sendMsg webServer:" + webServer + " webParams:" + webParams + " from:" + from + " content:" + content);
if (webServer == null || webServer.isEmpty()) {
return;
@ -57,6 +58,23 @@ public class SenderWebNotifyMsg {
Log.d(TAG, "method = GET, Url = " + webServer);
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";
if (webParams.startsWith("{")){
bodyMsg = content.replace("\n","\\n");
bodyMsg = webParams.replace("[msg]",bodyMsg);
Content_Type = "application/json;charset=utf-8";
}else{
bodyMsg = webParams.replace("[msg]",URLEncoder.encode(content, "UTF-8"));
}
RequestBody body = RequestBody.create(MediaType.parse(Content_Type), bodyMsg);
request = new Request.Builder()
.url(webServer)
.addHeader("Content-Type", Content_Type)
.method("POST", body)
.build();
Log.d(TAG, "method = POST webParams, Body = " + bodyMsg);
} else {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("from", from)

View File

@ -88,6 +88,27 @@
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='设置WebParams例如payload=%7B%22text%22%3A%22[msg]%22%7D [msg]将被替换成短信内容。\n支持Json格式例如{"text":[msg]}。\n注意除JSON格式外msg会自动进行UTF-8编码' />
<EditText
android:id="@+id/editTextWebNotifyWebParams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="14"
android:inputType=""
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@ -1,4 +1,4 @@
ext {
appVersionCode = 25
appVersionName = "1.7.4"
appVersionName = "1.7.5"
}