feat(sender/onebot): 新增onebot协议支持
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
@@ -1,32 +0,0 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/CatchZeng/dingtalk/pkg/dingtalk"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
)
|
||||
|
||||
func Reminder() {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置文件失败: %v", err)
|
||||
}
|
||||
|
||||
remainingTime := GlobalTimerManager.GetRemainingTime()
|
||||
if remainingTime <= 48*time.Hour {
|
||||
sendMsg := fmt.Sprintf("距离摇篮系统触发还有%d小时,请尽快发送Signal", int(remainingTime.Hours()))
|
||||
sendDingTalkMsg("摇篮系统触发提醒", sendMsg, config.DingtalkBot.AccessToken, config.DingtalkBot.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func sendDingTalkMsg(title string, msg string, accessToken string, secret string) {
|
||||
client := dingtalk.NewClient(accessToken, secret)
|
||||
sendMsg := dingtalk.NewMarkdownMessage().SetMarkdown(title, msg)
|
||||
_, _, err := client.Send(sendMsg)
|
||||
if err != nil {
|
||||
log.Printf("发送钉钉消息失败: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package reminder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/sender"
|
||||
)
|
||||
|
||||
func Reminder() {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置文件失败: %v", err)
|
||||
}
|
||||
|
||||
remainingTime := GlobalTimerManager.GetRemainingTime()
|
||||
if remainingTime <= 48*time.Hour {
|
||||
sendMsg := fmt.Sprintf("距离摇篮系统触发还有%d小时,请尽快发送Signal", int(remainingTime.Hours()))
|
||||
sender.SendDingTalkMsg("摇篮系统触发提醒", sendMsg, config.DingtalkBot.AccessToken, config.DingtalkBot.Secret)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package reminder
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,90 +0,0 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
)
|
||||
|
||||
// 在QQ发送死亡通告
|
||||
func SendQQ() {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置文件失败: %v", err)
|
||||
return // 添加return避免继续执行
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/?secret=%s", config.CatBotUrl, config.CatBotKey)
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送(摇篮系统)", GetBasicInfo(), config.QQMsg)
|
||||
|
||||
for _, groupId := range config.QQSendGroup {
|
||||
sendQQMsg(url, groupId, stopMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func sendQQMsg(url string, groupId int, msg string) {
|
||||
method := "POST"
|
||||
payload := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(payload)
|
||||
|
||||
// 处理写入字段的错误
|
||||
if err := writer.WriteField("group_id", strconv.Itoa(groupId)); err != nil {
|
||||
log.Printf("写入group_id字段失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := writer.WriteField("message", msg); err != nil {
|
||||
log.Printf("写入message字段失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
log.Printf("关闭multipart writer失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(method, url, payload)
|
||||
if err != nil {
|
||||
log.Printf("创建HTTP请求失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
var res *http.Response
|
||||
|
||||
for i := range maxRetries {
|
||||
res, err = client.Do(req)
|
||||
if err == nil {
|
||||
break // 请求成功,跳出循环
|
||||
}
|
||||
if i < maxRetries {
|
||||
time.Sleep(retryDelay * time.Duration(i+1)) // 递增延迟
|
||||
}
|
||||
log.Printf("请求QQ API失败,正在重试... (%d/%d): %v", i+1, maxRetries, err)
|
||||
}
|
||||
|
||||
// 检查res是否为nil,避免panic
|
||||
if res == nil {
|
||||
log.Println("QQ API请求失败,无法获取响应")
|
||||
return
|
||||
}
|
||||
|
||||
// 使用defer确保资源被释放,但要确保res不为nil
|
||||
defer func() {
|
||||
if res != nil && res.Body != nil {
|
||||
res.Body.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
// 可选:记录响应结果
|
||||
log.Println("QQ消息发送完成")
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package sender
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/CuteReimu/bilibili/v2"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/base"
|
||||
)
|
||||
|
||||
// check tmp dir exist
|
||||
@@ -134,7 +135,7 @@ func SendBili(biliClient *bilibili.Client) {
|
||||
return
|
||||
}
|
||||
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送(摇篮系统)", GetBasicInfo(), config.BiliMsg)
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送(摇篮系统)", base.GetBasicInfo(), config.BiliMsg)
|
||||
|
||||
var dynamicParams bilibili.CreateDynamicParam
|
||||
dynamicParams = bilibili.CreateDynamicParam{
|
||||
@@ -254,7 +255,7 @@ func triggerLoginRequest(biliClient *bilibili.Client) {
|
||||
|
||||
// 发送Dingtalk通知
|
||||
if config.DingtalkBot.AccessToken != "" && config.DingtalkBot.Secret != "" {
|
||||
sendDingTalkMsg(title, msg, config.DingtalkBot.AccessToken, config.DingtalkBot.Secret)
|
||||
SendDingTalkMsg(title, msg, config.DingtalkBot.AccessToken, config.DingtalkBot.Secret)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package sender
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/CatchZeng/dingtalk/pkg/dingtalk"
|
||||
)
|
||||
|
||||
func SendDingTalkMsg(title string, msg string, accessToken string, secret string) {
|
||||
client := dingtalk.NewClient(accessToken, secret)
|
||||
sendMsg := dingtalk.NewMarkdownMessage().SetMarkdown(title, msg)
|
||||
_, _, err := client.Send(sendMsg)
|
||||
if err != nil {
|
||||
log.Printf("发送钉钉消息失败: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package sender
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/base"
|
||||
"github.com/wneessen/go-mail"
|
||||
)
|
||||
|
||||
@@ -22,10 +23,10 @@ func SendMail() {
|
||||
return
|
||||
}
|
||||
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送", GetBasicInfo(), config.MailContent)
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送", base.GetBasicInfo(), config.MailConfig.MailContent)
|
||||
|
||||
for _, address := range config.MailList {
|
||||
sendMailMsgWithRetry(address, stopMsg, config.MailTitle)
|
||||
for _, address := range config.MailConfig.MailList {
|
||||
sendMailMsgWithRetry(address, stopMsg, config.MailConfig.MailTitle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,11 +66,11 @@ func sendMailMsg(address string, msg string, title string) error {
|
||||
}
|
||||
|
||||
client, err := mail.NewClient(
|
||||
config.SMTPConfig.Host,
|
||||
mail.WithPort(config.SMTPConfig.Port),
|
||||
config.MailConfig.Host,
|
||||
mail.WithPort(config.MailConfig.Port),
|
||||
mail.WithSSL(),
|
||||
mail.WithUsername(config.SMTPConfig.User),
|
||||
mail.WithPassword(config.SMTPConfig.Password),
|
||||
mail.WithUsername(config.MailConfig.User),
|
||||
mail.WithPassword(config.MailConfig.Password),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthPlain),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -78,7 +79,7 @@ func sendMailMsg(address string, msg string, title string) error {
|
||||
|
||||
// 创建邮件
|
||||
message := mail.NewMsg()
|
||||
if err := message.From(config.SMTPConfig.User); err != nil {
|
||||
if err := message.From(config.MailConfig.User); err != nil {
|
||||
return fmt.Errorf("设置发件人失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package internal
|
||||
package sender
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/pkg"
|
||||
"github.com/ssdomei232/goodBaby/pkg/github"
|
||||
)
|
||||
|
||||
// 将repos设置为public
|
||||
@@ -18,19 +18,18 @@ func Github() {
|
||||
for _, repo := range config.GithubConfig.Repos {
|
||||
maxRetries := 10
|
||||
for attempt := range maxRetries {
|
||||
err := pkg.MakeRepositoryPublic(config.GithubConfig.Owner, repo, config.GithubConfig.Token)
|
||||
err := github.MakeRepositoryPublic(config.GithubConfig.Owner, repo, config.GithubConfig.Token)
|
||||
if err == nil {
|
||||
// 成功执行,跳出重试循环
|
||||
break
|
||||
}
|
||||
|
||||
log.Printf("尝试设置仓库 %s 为私有失败 (尝试 %d/%d): %v", repo, attempt+1, maxRetries, err)
|
||||
log.Printf("尝试设置仓库 %s 为公开失败 (尝试 %d/%d): %v", repo, attempt+1, maxRetries, err)
|
||||
|
||||
// 如果不是最后一次尝试,则等待一段时间后重试
|
||||
if attempt < maxRetries-1 {
|
||||
time.Sleep(time.Duration(attempt+1) * time.Second) // 逐步增加等待时间
|
||||
} else {
|
||||
log.Printf("设置仓库 %s 为私有最终失败", repo)
|
||||
log.Printf("设置仓库 %s 为公开最终失败", repo)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package sender
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/base"
|
||||
napcat "github.com/ssdomei232/napcat-http-go-sdk"
|
||||
)
|
||||
|
||||
func SendOneBot() {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置文件失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
stopMsg := fmt.Sprintf("%s\n\n%s\n\n本消息由自动程序发送(摇篮系统)", base.GetBasicInfo(), config.OneBotConfig.Msg)
|
||||
|
||||
for _, groupId := range config.OneBotConfig.SendGroups {
|
||||
sendOneBotMsg(groupId, stopMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送一条消息到OneBot
|
||||
func sendOneBotMsg(groupID int, msg string) {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置文件失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
client := napcat.NewClient(config.OneBotConfig.URL, config.OneBotConfig.Token)
|
||||
|
||||
err = client.SendGroupMsg(strconv.Itoa(groupID), msg)
|
||||
if err != nil {
|
||||
log.Printf("发送消息到OneBot失败: %v", err)
|
||||
} else {
|
||||
log.Printf("成功发送消息到OneBot群 %d", groupID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user