飞书使用Card发送通知消息

pull/86/head
xiao0yy 3 years ago
parent 6a26c222e1
commit 95838bc5e3

@ -1293,7 +1293,7 @@ public class SenderActivity extends AppCompatActivity {
String secret = editTextFeishuSecret.getText().toString().trim();
if (!token.isEmpty()) {
try {
SenderFeishuMsg.sendMsg(0, handler, token, secret, getString(R.string.test_content) + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
SenderFeishuMsg.sendMsg(0, handler, token, secret, getString(R.string.test_phone_num), new Date(), getString(R.string.test_sms));
} catch (Exception e) {
Toast.makeText(SenderActivity.this, getString(R.string.failed_to_fwd) + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();

@ -267,7 +267,7 @@ public class SendUtil {
FeiShuSettingVo feiShuSettingVo = JSON.parseObject(senderModel.getJsonSetting(), FeiShuSettingVo.class);
if (feiShuSettingVo != null) {
try {
SenderFeishuMsg.sendMsg(logId, handError, feiShuSettingVo.getWebhook(), feiShuSettingVo.getSecret(), smsVo.getSmsVoForSend(smsTemplate, regexReplace));
SenderFeishuMsg.sendMsg(logId, handError, feiShuSettingVo.getWebhook(), feiShuSettingVo.getSecret(), smsVo.getMobile(), smsVo.getDate(), smsVo.getSmsVoForSend(smsTemplate, regexReplace));
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Log.e(TAG, "senderSendMsg: feishu error " + e.getMessage());

@ -12,6 +12,8 @@ import com.idormy.sms.forwarder.utils.SettingUtil;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -35,7 +37,60 @@ public class SenderFeishuMsg extends SenderBaseMsg {
static final String TAG = "SenderFeishuMsg";
public static void sendMsg(final long logId, final Handler handError, String webhook, String secret, String content) throws Exception {
static final String MSG_TEMPLATE = "{\n" +
" \"config\": {\n" +
" \"wide_screen_mode\": true\n" +
" },\n" +
" \"elements\": [\n" +
" {\n" +
" \"fields\": [\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**时间**\\n${MSG_TIME}\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"is_short\": true,\n" +
" \"text\": {\n" +
" \"content\": \"**来源**\\n${MSG_FROM}\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"tag\": \"div\"\n" +
" },\n" +
" {\n" +
" \"tag\": \"div\",\n" +
" \"text\": {\n" +
" \"content\": \"${MSG_CONTENT}\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"tag\": \"hr\"\n" +
" },\n" +
" {\n" +
" \"elements\": [\n" +
" {\n" +
" \"content\": \"[SmsForwarder](https://github.com/pppscn/SmsForwarder)\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" ],\n" +
" \"tag\": \"note\"\n" +
" }\n" +
" ],\n" +
" \"header\": {\n" +
" \"template\": \"turquoise\",\n" +
" \"title\": {\n" +
" \"content\": \"${MSG_TITLE}\",\n" +
" \"tag\": \"plain_text\"\n" +
" }\n" +
" }\n" +
"}";
public static void sendMsg(final long logId, final Handler handError, String webhook, String secret, String from, Date date, String content) throws Exception {
Log.i(TAG, "sendMsg webhook:" + webhook + " secret:" + secret + " content:" + content);
if (webhook == null || webhook.isEmpty()) {
@ -62,14 +117,12 @@ public class SenderFeishuMsg extends SenderBaseMsg {
}
//组装报文
textMsgMap.put("msg_type", "text");
Map contentMap = new HashMap();
contentMap.put("text", content);
textMsgMap.put("content", contentMap);
textMsgMap.put("msg_type", "interactive");
textMsgMap.put("card", "${CARD_BODY}");
final String requestUrl = webhook;
Log.i(TAG, "requestUrl:" + requestUrl);
final String requestMsg = JSON.toJSONString(textMsgMap);
final String requestMsg = JSON.toJSONString(textMsgMap).replace("\"${CARD_BODY}\"", buildMsg(from, date, content));
Log.i(TAG, "requestMsg:" + requestMsg);
Observable
@ -123,4 +176,23 @@ public class SenderFeishuMsg extends SenderBaseMsg {
.subscribe(System.out::println);
}
private static String buildMsg(String from, Date date, String content) {
String msgTitle = jsonInnerStr("【" + SettingUtil.getAddExtraDeviceMark().trim() + "】来自" + from + "的通知");
String msgTime = jsonInnerStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
String msgFrom = jsonInnerStr(from);
String msgContent = jsonInnerStr(content);
return MSG_TEMPLATE.replace("${MSG_TITLE}", msgTitle)
.replace("${MSG_TIME}", msgTime)
.replace("${MSG_FROM}", msgFrom)
.replace("${MSG_CONTENT}", msgContent);
}
private static String jsonInnerStr(String string) {
if (string == null) {
return "null";
}
String jsonStr = JSON.toJSONString(string);
return jsonStr.length() >= 2 ? jsonStr.substring(1, jsonStr.length() - 1) : jsonStr;
}
}

Loading…
Cancel
Save