From f2dc54773923b1277be6c7bce22f909c809a3899 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Sun, 24 Nov 2024 14:15:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0WxPusher=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../digtal/market/runner/PushRunner.kt | 2 + .../digtal/market/sender/WxPusherSender.kt | 41 +++++++++++++++++++ .../lanyuanxiaoyao/digtal/market/TestPush.kt | 30 ++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/main/kotlin/com/lanyuanxiaoyao/digtal/market/sender/WxPusherSender.kt create mode 100644 src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt diff --git a/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/runner/PushRunner.kt b/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/runner/PushRunner.kt index 5772899..cd56cbb 100644 --- a/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/runner/PushRunner.kt +++ b/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/runner/PushRunner.kt @@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.digtal.market.runner import com.lanyuanxiaoyao.digtal.market.ArticleRepository import com.lanyuanxiaoyao.digtal.market.sender.DingtalkSender +import com.lanyuanxiaoyao.digtal.market.sender.WxPusherSender import jakarta.annotation.Resource import org.slf4j.LoggerFactory import org.springframework.data.domain.Sort @@ -25,6 +26,7 @@ class PushRunner : Runner { }, Sort.by(Sort.Direction.DESC, "createTime")) try { DingtalkSender().send("近期要闻", mapOf("articles" to articles.take(10))) + WxPusherSender().send("近期发生的数据要素新闻", mapOf("articles" to articles.take(10))) articles.forEach { articleRepository.updatePushedById(it.id, true) } } catch (e: RuntimeException) { logger.error("发送失败", e) diff --git a/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/sender/WxPusherSender.kt b/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/sender/WxPusherSender.kt new file mode 100644 index 0000000..3f705cc --- /dev/null +++ b/src/main/kotlin/com/lanyuanxiaoyao/digtal/market/sender/WxPusherSender.kt @@ -0,0 +1,41 @@ +package com.lanyuanxiaoyao.digtal.market.sender + +import cn.hutool.http.HttpUtil +import cn.hutool.json.JSONUtil +import com.lanyuanxiaoyao.digtal.market.Article +import org.slf4j.LoggerFactory + +class WxPusherSender : Sender { + private val logger = LoggerFactory.getLogger(javaClass) + + override fun send(title: String, content: Map) { + val articles = content["articles"] as List
? + if (articles.isNullOrEmpty()) { + logger.warn("Articles is not found") + return + } + val response = HttpUtil + .createPost("https://wxpusher.zjiecode.com/api/send/message") + .body( + JSONUtil.toJsonStr( + mapOf( + "appToken" to "AT_7gtCYQ2bW2wlFYYdEZDdgsUcf1gSd7hR", + // language=HTML + "content" to "

要闻集合

\n${articles.joinToString("\n") { "${it.title}
${it.description}

" }}", + "summary" to title, + "contentType" to 2, + "topicIds" to listOf("35482"), + "verifyPayType" to 0, + ) + ) + ) + .execute() + if (response.isOk.not() || JSONUtil.parseObj(response.body()).getInt("code", -1) != 1000) { + throw RuntimeException(try { + response.body() + } catch (e: Exception) { + e.message + }) + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt b/src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt new file mode 100644 index 0000000..f911a05 --- /dev/null +++ b/src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt @@ -0,0 +1,30 @@ +package com.lanyuanxiaoyao.digtal.market + +import com.lanyuanxiaoyao.digtal.market.sender.WxPusherSender +import org.junit.jupiter.api.Test + +class TestPush { + @Test + fun testWxPusher() { + WxPusherSender() + .send("近期发生的数据要素新闻", mapOf( + "articles" to listOf( + Article( + "helloworld", + "helloworld", + "https://www.baidu.com", + "Hello world", + null, + null, + null, + null, + null, + "近期发生的数据要素新闻", + null, + null, + null, + ) + ) + )) + } +} \ No newline at end of file