feat: 增加WxPusher推送
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<String, Any>) {
|
||||
val articles = content["articles"] as List<Article>?
|
||||
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 "<h2>要闻集合</h2>\n${articles.joinToString("\n") { "<b><a href=\"${it.url}\">${it.title}</a></b><br/><div style=\"margin-top: 5px;padding-left: 5px;padding-bottom: 5px;border-left: 3px solid #0d0c0c\">${it.description}</div><br/>" }}",
|
||||
"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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt
Normal file
30
src/test/kotlin/com/lanyuanxiaoyao/digtal/market/TestPush.kt
Normal file
@@ -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,
|
||||
)
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user