新增:Telegram允许指定请求方式(POST/GET)

This commit is contained in:
pppscn 2022-01-11 22:46:12 +08:00
parent 798790b7cf
commit c34133b45b
8 changed files with 263 additions and 192 deletions

View File

@ -1022,6 +1022,7 @@ public class SenderActivity extends AppCompatActivity {
final EditText editTextTelegramApiToken = view1.findViewById(R.id.editTextTelegramApiToken);
final EditText editTextTelegramChatId = view1.findViewById(R.id.editTextTelegramChatId);
final RadioGroup radioGroupTelegramMethod = view1.findViewById(R.id.radioGroupTelegramMethod);
final RadioGroup radioGroupProxyType = view1.findViewById(R.id.radioGroupProxyType);
final EditText editTextProxyHost = view1.findViewById(R.id.editTextProxyHost);
@ -1058,6 +1059,7 @@ public class SenderActivity extends AppCompatActivity {
if (telegramSettingVo != null) {
editTextTelegramApiToken.setText(telegramSettingVo.getApiToken());
editTextTelegramChatId.setText(telegramSettingVo.getChatId());
radioGroupTelegramMethod.check(telegramSettingVo.getMethodCheckId());
radioGroupProxyType.check(telegramSettingVo.getProxyTypeCheckId());
layoutProxyAuthenticator.setVisibility(telegramSettingVo.getProxyAuthenticator() ? View.VISIBLE : View.GONE);
@ -1102,7 +1104,8 @@ public class SenderActivity extends AppCompatActivity {
editTextProxyPort.getText().toString().trim(),
switchProxyAuthenticator.isChecked(),
editTextProxyUsername.getText().toString().trim(),
editTextProxyPassword.getText().toString().trim()
editTextProxyPassword.getText().toString().trim(),
(radioGroupTelegramMethod.getCheckedRadioButtonId() == R.id.radioTelegramMethodGet ? "GET" : "POST")
);
newSenderModel.setJsonSetting(JSON.toJSONString(telegramSettingVoNew));
@ -1121,7 +1124,8 @@ public class SenderActivity extends AppCompatActivity {
editTextProxyPort.getText().toString().trim(),
switchProxyAuthenticator.isChecked(),
editTextProxyUsername.getText().toString().trim(),
editTextProxyPassword.getText().toString().trim()
editTextProxyPassword.getText().toString().trim(),
(radioGroupTelegramMethod.getCheckedRadioButtonId() == R.id.radioTelegramMethodGet ? "GET" : "POST")
);
senderModel.setJsonSetting(JSON.toJSONString(telegramSettingVoNew));
SenderUtil.updateSender(senderModel);
@ -1153,9 +1157,10 @@ public class SenderActivity extends AppCompatActivity {
editTextProxyPort.getText().toString().trim(),
switchProxyAuthenticator.isChecked(),
editTextProxyUsername.getText().toString().trim(),
editTextProxyPassword.getText().toString().trim()
editTextProxyPassword.getText().toString().trim(),
(radioGroupTelegramMethod.getCheckedRadioButtonId() == R.id.radioTelegramMethodGet ? "GET" : "POST")
);
SenderTelegramMsg.sendMsg(0, handler, telegramSettingVoNew, getString(R.string.test_phone_num), getString(R.string.test_sms));
SenderTelegramMsg.sendMsg(0, handler, telegramSettingVoNew, getString(R.string.test_phone_num), getString(R.string.test_sms), telegramSettingVoNew.getMethod());
} catch (Exception e) {
Toast.makeText(SenderActivity.this, getString(R.string.failed_to_fwd) + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();

View File

@ -17,6 +17,7 @@ public class TelegramSettingVo implements Serializable {
private Boolean proxyAuthenticator = false;
private String proxyUsername;
private String proxyPassword;
private String method;
public TelegramSettingVo() {
}
@ -25,9 +26,10 @@ public class TelegramSettingVo implements Serializable {
this.apiToken = apiToken;
this.chatId = chatId;
this.proxyType = Proxy.Type.DIRECT;
this.method = "POST";
}
public TelegramSettingVo(String apiToken, String chatId, int proxyTypeId, String proxyHost, String proxyPort, boolean proxyAuthenticator, String proxyUsername, String proxyPassword) {
public TelegramSettingVo(String apiToken, String chatId, int proxyTypeId, String proxyHost, String proxyPort, boolean proxyAuthenticator, String proxyUsername, String proxyPassword, String method) {
this.apiToken = apiToken;
this.chatId = chatId;
if (proxyTypeId == R.id.btnProxyHttp) {
@ -42,6 +44,7 @@ public class TelegramSettingVo implements Serializable {
this.proxyAuthenticator = proxyAuthenticator;
this.proxyUsername = proxyUsername;
this.proxyPassword = proxyPassword;
this.method = method;
}
public int getProxyTypeCheckId() {
@ -53,4 +56,12 @@ public class TelegramSettingVo implements Serializable {
return R.id.btnProxyNone;
}
}
public int getMethodCheckId() {
if (method == null || method.equals("POST")) {
return R.id.radioTelegramMethodPost;
} else {
return R.id.radioTelegramMethodGet;
}
}
}

View File

@ -225,7 +225,7 @@ public class SendUtil {
TelegramSettingVo telegramSettingVo = JSON.parseObject(senderModel.getJsonSetting(), TelegramSettingVo.class);
if (telegramSettingVo != null) {
try {
SenderTelegramMsg.sendMsg(logId, handError, telegramSettingVo, smsVo.getMobile(), smsVo.getSmsVoForSend(smsTemplate, regexReplace));
SenderTelegramMsg.sendMsg(logId, handError, telegramSettingVo, smsVo.getMobile(), smsVo.getSmsVoForSend(smsTemplate, regexReplace), telegramSettingVo.getMethod());
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Log.e(TAG, "senderSendMsg: SenderTelegramMsg error " + e.getMessage());

View File

@ -37,7 +37,7 @@ public class SenderTelegramMsg extends SenderBaseMsg {
static final String TAG = "SenderTelegramMsg";
public static void sendMsg(final long logId, final Handler handError, TelegramSettingVo telegramSettingVo, String from, String text) throws Exception {
public static void sendMsg(final long logId, final Handler handError, TelegramSettingVo telegramSettingVo, final String from, final String text, final String method) throws Exception {
Log.i(TAG, "sendMsg telegramSettingVo:" + telegramSettingVo.toString() + " text:" + text);
String apiToken = telegramSettingVo.getApiToken();
@ -47,15 +47,14 @@ public class SenderTelegramMsg extends SenderBaseMsg {
}
//特殊处理避免标题重复
text = text.replaceAll("#", "").trim();
final String finalText = text.replaceAll("#", "").trim();
if (!apiToken.startsWith("http")) {
apiToken = "https://api.telegram.org/bot" + apiToken + "/sendMessage";
}
final String requestUrl = apiToken;
Log.i(TAG, "requestUrl:" + requestUrl);
String finalText = text;
Log.i(TAG, "requestMsg:" + finalText);
//代理相关
final Proxy.Type proxyType = telegramSettingVo.getProxyType();
@ -101,9 +100,27 @@ public class SenderTelegramMsg extends SenderBaseMsg {
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS)).build();
}
final Request request = new Request.Builder()
.url(requestUrl + "?chat_id=" + chatId + "&text=" + finalText)
.build();
final Request request;
if (method.equals("GET")) {
request = new Request.Builder()
.url(requestUrl + "?chat_id=" + chatId + "&text=" + finalText)
.build();
} else {
Map bodyMap = new HashMap();
bodyMap.put("chat_id", chatId);
bodyMap.put("text", finalText);
bodyMap.put("parse_mode", "HTML");
String requestMsg = JSON.toJSONString(bodyMap);
Log.i(TAG, "requestMsg:" + requestMsg);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), requestMsg);
request = new Request.Builder()
.url(requestUrl)
.addHeader("Content-Type", "application/json; charset=utf-8")
.post(requestBody)
.build();
}
client.newCall(request).enqueue(new Callback() {
@Override
@ -127,7 +144,7 @@ public class SenderTelegramMsg extends SenderBaseMsg {
}
}
});
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Log.e(TAG, e.getMessage(), e);

View File

@ -58,6 +58,44 @@
tools:ignore="LabelFor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/Method"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radioGroupTelegramMethod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioTelegramMethodPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/post" />
<RadioButton
android:id="@+id/radioTelegramMethodGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@ -1,177 +1,177 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_name" />
<EditText
android:id="@+id/editTextWebNotifyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="11"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/WebNotifyMethod" />
<RadioGroup
android:id="@+id/radioGroupWebNotifyMethod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioWebNotifyMethodPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/post" />
<RadioButton
android:id="@+id/radioWebNotifyMethodGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get" />
</RadioGroup>
</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="@string/WebNotifyWebServer" />
<EditText
android:id="@+id/editTextWebNotifyWebServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</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='@string/WebNotifyWebParams' />
<EditText
android:id="@+id/editTextWebNotifyWebParams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</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="@string/WebNotifySecret" />
<EditText
android:id="@+id/editTextWebNotifySecret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="textPassword"
android:text=""
tools:ignore="LabelFor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/buttonWebNotifyTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="@string/test"
tools:ignore="ButtonStyle,NestedWeights" />
<Button
android:id="@+id/buttonWebNotifyDel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="@string/del"
tools:ignore="ButtonStyle,NestedWeights" />
<Button
android:id="@+id/buttonWebNotifyOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ok"
tools:ignore="ButtonStyle,NestedWeights" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_name" />
<EditText
android:id="@+id/editTextWebNotifyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="11"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/Method" />
<RadioGroup
android:id="@+id/radioGroupWebNotifyMethod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioWebNotifyMethodPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/post" />
<RadioButton
android:id="@+id/radioWebNotifyMethodGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get" />
</RadioGroup>
</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="@string/WebNotifyWebServer" />
<EditText
android:id="@+id/editTextWebNotifyWebServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</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='@string/WebNotifyWebParams' />
<EditText
android:id="@+id/editTextWebNotifyWebParams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:text=""
tools:ignore="LabelFor" />
</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="@string/WebNotifySecret" />
<EditText
android:id="@+id/editTextWebNotifySecret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="14"
android:inputType="textPassword"
android:text=""
tools:ignore="LabelFor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/buttonWebNotifyTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="@string/test"
tools:ignore="ButtonStyle,NestedWeights" />
<Button
android:id="@+id/buttonWebNotifyDel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="@string/del"
tools:ignore="ButtonStyle,NestedWeights" />
<Button
android:id="@+id/buttonWebNotifyOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ok"
tools:ignore="ButtonStyle,NestedWeights" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -147,7 +147,7 @@
<string name="ServerChanSendKey">ServerChan\'s SendKey</string>
<string name="TelegramApiToken">ApiToken or Custom address</string>
<string name="TelegramChatId">ChatId</string>
<string name="WebNotifyMethod">Method</string>
<string name="Method">Method</string>
<string name="WebNotifyWebServer">WebServer e.g. https://a.b.com/msg?token=xyz</string>
<string name="WebNotifyWebParams">WebParams e.g. payload=%7B%22text%22%3A%22[msg]%22%7D [msg] will be replaced with text message content. \nSupport Json format, for example: {"text":[MSG]}.\n Note: MSG is automatically utF-8 encoded in addition to JSON format</string>
<string name="WebNotifySecret">Secret (sign is not counted if left blank)</string>

View File

@ -147,7 +147,7 @@
<string name="ServerChanSendKey">设置Server酱·Turbo版的SendKey</string>
<string name="TelegramApiToken">设置Telegram机器人的ApiToken 或 自定义地址(http开头)</string>
<string name="TelegramChatId">设置被通知人(或群组)的ChatId</string>
<string name="WebNotifyMethod">请求方式</string>
<string name="Method">请求方式</string>
<string name="WebNotifyWebServer">设置WebServer例如https://a.b.com/msg?token=xyz</string>
<string name="WebNotifyWebParams">设置WebParams例如payload=%7B%22text%22%3A%22[msg]%22%7D [msg]将被替换成短信内容。\n支持Json格式例如{"text":[msg]}。\n注意除JSON格式外msg会自动进行UTF-8编码</string>
<string name="WebNotifySecret">设置Secret:置空则不计算sign</string>