Initial commit: TaskPool React panel

- React frontend with route-level code splitting
- Backend rebranded from Baihu to TaskPool
- DB brand migration script and local compatibility
This commit is contained in:
2026-07-26 08:43:52 +08:00
commit e6956aa001
397 changed files with 73621 additions and 0 deletions
@@ -0,0 +1,52 @@
package channels
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type AliyunSMSChannel struct{ *BaseChannel }
func NewAliyunSMSChannel() Channel {
return &AliyunSMSChannel{NewBaseChannel(ChannelAliyunSMS, []string{FormatTypeText})}
}
func (c *AliyunSMSChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
accessKeyId := config.GetString("access_key_id")
accessKeySecret := config.GetString("access_key_secret")
signName := config.GetString("sign_name")
regionId := config.GetString("region_id")
phoneNumber := config.GetString("phone_number")
templateCode := config.GetString("template_code")
if accessKeyId == "" || accessKeySecret == "" || signName == "" {
return SendError("aliyun sms config missing: access_key_id, access_key_secret, sign_name are required"), nil
}
if phoneNumber == "" || templateCode == "" {
return SendError("aliyun sms config missing: phone_number, template_code are required"), nil
}
_, formattedContent := c.FormatContent(msg)
if regionId == "" {
regionId = "cn-hangzhou"
}
client, err := createAliyunSMSClient(accessKeyId, accessKeySecret, regionId)
if err != nil {
return SendError("创建阿里云短信客户端失败: %s", err.Error()), nil
}
result, err := sendAliyunSMS(client, phoneNumber, signName, templateCode, formattedContent, msg.Extra)
if err != nil {
return ErrorResult("", err), nil
}
return SuccessResult(result), nil
}
+51
View File
@@ -0,0 +1,51 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type BarkChannel struct{ *BaseChannel }
func NewBarkChannel() Channel {
return &BarkChannel{NewBaseChannel(ChannelBark, []string{FormatTypeText})}
}
func (c *BarkChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
pushKey := config.GetString("push_key")
if pushKey == "" {
return SendError("bark config missing: push_key is required"), nil
}
cli := message.Bark{
PushKey: pushKey,
Archive: config.GetString("archive"),
Group: config.GetString("group"),
Sound: config.GetString("sound"),
Icon: config.GetString("icon"),
Level: config.GetString("level"),
URL: config.GetString("url"),
Key: config.GetString("key"),
IV: config.GetString("iv"),
Server: config.GetString("server"),
Badge: config.GetString("badge"),
Copy: config.GetString("copy"),
AutoCopy: config.GetString("auto_copy"),
ProxyURL: config.GetString("proxy_url"),
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
@@ -0,0 +1,86 @@
package channels
import "fmt"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
// Channel 渠道接口 - SDK 版本,零业务依赖
type Channel interface {
// GetType 返回渠道类型标识
GetType() string
// GetSupportedFormats 返回支持的消息格式
GetSupportedFormats() []string
// Send 发送消息
Send(config ChannelConfig, msg *Message) (*Result, error)
}
// BaseChannel 渠道基础实现
type BaseChannel struct {
channelType string
supportedFormats []string
}
func NewBaseChannel(channelType string, supportedFormats []string) *BaseChannel {
return &BaseChannel{channelType: channelType, supportedFormats: supportedFormats}
}
func (c *BaseChannel) GetType() string { return c.channelType }
func (c *BaseChannel) GetSupportedFormats() []string { return c.supportedFormats }
// FormatContent 根据渠道支持的格式选择最佳内容
func (c *BaseChannel) FormatContent(msg *Message) (formatType string, content string) {
for _, ft := range c.supportedFormats {
switch ft {
case FormatTypeMarkdown:
if msg.HasMarkdown() {
return FormatTypeMarkdown, msg.Markdown
}
case FormatTypeHTML:
if msg.HasHTML() {
return FormatTypeHTML, msg.HTML
}
case FormatTypeText:
if msg.HasText() {
return FormatTypeText, msg.Text
}
}
}
if msg.HasText() {
return FormatTypeText, msg.Text
}
return FormatTypeText, ""
}
// SuccessResult 创建成功结果
func SuccessResult(response string) *Result {
return &Result{Success: true, Response: response}
}
// ErrorResult 创建失败结果
func ErrorResult(response string, err error) *Result {
errMsg := ""
if err != nil {
errMsg = err.Error()
}
return &Result{Success: false, Response: response, Error: errMsg}
}
// ErrorResultStr 创建失败结果(字符串错误)
func ErrorResultStr(response string, errMsg string) *Result {
return &Result{Success: false, Response: response, Error: errMsg}
}
// SendError 发送失败时的格式化错误
func SendError(format string, args ...any) *Result {
return &Result{Success: false, Error: fmt.Sprintf(format, args...)}
}
+59
View File
@@ -0,0 +1,59 @@
package channels
import (
"encoding/json"
"github.com/engigu/taskpool/internal/sdk/message"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type CustomChannel struct{ *BaseChannel }
func NewCustomChannel() Channel {
return &CustomChannel{NewBaseChannel(ChannelCustom, []string{FormatTypeText})}
}
func (c *CustomChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
webhook := config.GetString("webhook")
body := config.GetString("body")
headersStr := config.GetString("headers")
if webhook == "" {
return SendError("custom config missing: webhook is required"), nil
}
var headers map[string]string
if headersStr != "" {
if err := json.Unmarshal([]byte(headersStr), &headers); err != nil {
return SendError("custom config error: headers must be a valid JSON object"), nil
}
}
_, formattedContent := c.FormatContent(msg)
cli := message.CustomWebhook{}
// 替换 body 模板中的 TEXT 占位符
bodyStr := body
if bodyStr != "" {
bodyStr = replaceBodyPlaceholder(bodyStr, formattedContent)
} else {
bodyStr = formattedContent
}
res, err := cli.Request(webhook, bodyStr, headers)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+54
View File
@@ -0,0 +1,54 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type DtalkChannel struct{ *BaseChannel }
func NewDtalkChannel() Channel {
return &DtalkChannel{NewBaseChannel(ChannelDtalk, []string{FormatTypeMarkdown, FormatTypeText})}
}
func (c *DtalkChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
accessToken := config.GetString("access_token")
secret := config.GetString("secret")
if accessToken == "" {
return SendError("dtalk config missing: access_token is required"), nil
}
contentType, formattedContent := c.FormatContent(msg)
atMobiles := msg.GetAtMobiles()
if msg.AtAll {
atMobiles = append(atMobiles, "all")
}
cli := message.Dtalk{AccessToken: accessToken, Secret: secret}
var res []byte
var err error
switch contentType {
case FormatTypeText:
res, err = cli.SendMessageText(formattedContent, atMobiles...)
case FormatTypeMarkdown:
res, err = cli.SendMessageMarkdown(msg.Title, formattedContent, atMobiles...)
default:
return SendError("未知的钉钉发送内容类型:%s", contentType), nil
}
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+62
View File
@@ -0,0 +1,62 @@
package channels
import (
"fmt"
"github.com/engigu/taskpool/internal/sdk/message"
"strconv"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type EmailChannel struct{ *BaseChannel }
func NewEmailChannel() Channel {
return &EmailChannel{NewBaseChannel(ChannelEmail, []string{FormatTypeHTML, FormatTypeText})}
}
func (c *EmailChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
server := config.GetString("server")
portStr := config.GetString("port")
account := config.GetString("account")
passwd := config.GetString("passwd")
fromName := config.GetString("from_name")
toAccount := config.GetString("to_account")
if server == "" || account == "" || passwd == "" {
return SendError("email config missing: server, account, passwd are required"), nil
}
if toAccount == "" {
return SendError("email config missing: to_account is required"), nil
}
port, _ := strconv.Atoi(portStr)
contentType, formattedContent := c.FormatContent(msg)
var emailer message.EmailMessage
emailer.Init(server, port, account, passwd, fromName)
var errMsg string
switch contentType {
case FormatTypeText:
errMsg = emailer.SendTextMessage(toAccount, msg.Title, formattedContent)
case FormatTypeHTML:
errMsg = emailer.SendHtmlMessage(toAccount, msg.Title, formattedContent)
default:
errMsg = fmt.Sprintf("未知的邮件发送内容类型:%s", contentType)
}
if errMsg != "" {
return ErrorResultStr("", errMsg), nil
}
return SuccessResult(""), nil
}
+56
View File
@@ -0,0 +1,56 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type FeishuChannel struct{ *BaseChannel }
func NewFeishuChannel() Channel {
return &FeishuChannel{NewBaseChannel(ChannelFeishu, []string{FormatTypeMarkdown, FormatTypeText})}
}
func (c *FeishuChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
accessToken := config.GetString("access_token")
secret := config.GetString("secret")
if accessToken == "" {
return SendError("feishu config missing: access_token is required"), nil
}
contentType, formattedContent := c.FormatContent(msg)
atMobiles := msg.GetAtMobiles()
atUserIds := msg.GetAtUserIds()
atList := append(atMobiles, atUserIds...)
if msg.AtAll {
atList = append(atList, "all")
}
cli := message.Feishu{AccessToken: accessToken, Secret: secret}
var res []byte
var err error
switch contentType {
case FormatTypeText:
res, err = cli.SendMessageText(formattedContent, atList...)
case FormatTypeMarkdown:
res, err = cli.SendMessageMarkdown(msg.Title, formattedContent, atList...)
default:
return SendError("未知的飞书发送内容类型:%s", contentType), nil
}
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+46
View File
@@ -0,0 +1,46 @@
package channels
import (
"github.com/engigu/taskpool/internal/sdk/message"
"strconv"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type GotifyChannel struct{ *BaseChannel }
func NewGotifyChannel() Channel {
return &GotifyChannel{NewBaseChannel(ChannelGotify, []string{FormatTypeText})}
}
func (c *GotifyChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
url := config.GetString("url")
token := config.GetString("token")
if url == "" || token == "" {
return SendError("gotify config missing: url and token are required"), nil
}
priority, _ := strconv.Atoi(config.GetString("priority"))
cli := message.Gotify{
Url: url,
Token: token,
Priority: priority,
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
@@ -0,0 +1,77 @@
package channels
import (
"encoding/json"
"fmt"
"strings"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
"github.com/alibabacloud-go/tea/tea"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
// replaceBodyPlaceholder 替换自定义 webhook body 中的 TEXT 占位符
func replaceBodyPlaceholder(body string, content string) string {
data, _ := json.Marshal(content)
dataStr := strings.Trim(string(data), "\"")
return strings.Replace(body, "TEXT", dataStr, -1)
}
// createAliyunSMSClient 创建阿里云短信客户端 (V2.0)
func createAliyunSMSClient(accessKeyId, accessKeySecret, regionId string) (*dysmsapi.Client, error) {
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
RegionId: tea.String(regionId),
}
// 设置端点,通常为 dysmsapi.aliyuncs.com
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
return dysmsapi.NewClient(config)
}
// sendAliyunSMS 发送短信 (V2.0)
func sendAliyunSMS(client *dysmsapi.Client, phoneNumber, signName, templateCode, content string, extra map[string]any) (string, error) {
templateParam := map[string]interface{}{
"content": content,
}
for k, v := range extra {
templateParam[k] = v
}
templateParamJSON, _ := json.Marshal(templateParam)
request := &dysmsapi.SendSmsRequest{
PhoneNumbers: tea.String(phoneNumber),
SignName: tea.String(signName),
TemplateCode: tea.String(templateCode),
TemplateParam: tea.String(string(templateParamJSON)),
}
response, err := client.SendSms(request)
if err != nil {
return "", fmt.Errorf("发送短信失败: %s", err.Error())
}
if response.Body == nil || tea.StringValue(response.Body.Code) != "OK" {
msg := "Unknown Error"
code := "Unknown Code"
if response.Body != nil {
msg = tea.StringValue(response.Body.Message)
code = tea.StringValue(response.Body.Code)
}
return "", fmt.Errorf("发送失败: %s - %s", code, msg)
}
return fmt.Sprintf("RequestId: %s, BizId: %s", tea.StringValue(response.Body.RequestId), tea.StringValue(response.Body.BizId)), nil
}
+45
View File
@@ -0,0 +1,45 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type NtfyChannel struct{ *BaseChannel }
func NewNtfyChannel() Channel {
return &NtfyChannel{NewBaseChannel(ChannelNtfy, []string{FormatTypeText})}
}
func (c *NtfyChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
topic := config.GetString("topic")
if topic == "" {
return SendError("ntfy config missing: topic is required"), nil
}
cli := message.Ntfy{
Url: config.GetString("url"),
Topic: topic,
Priority: config.GetString("priority"),
Icon: config.GetString("icon"),
Token: config.GetString("token"),
Username: config.GetString("username"),
Password: config.GetString("password"),
Actions: config.GetString("actions"),
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+41
View File
@@ -0,0 +1,41 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type PushMeChannel struct{ *BaseChannel }
func NewPushMeChannel() Channel {
return &PushMeChannel{NewBaseChannel(ChannelPushMe, []string{FormatTypeText})}
}
func (c *PushMeChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
pushKey := config.GetString("push_key")
if pushKey == "" {
return SendError("pushme config missing: push_key is required"), nil
}
cli := message.PushMe{
PushKey: pushKey,
URL: config.GetString("url"),
Date: config.GetString("date"),
Type: config.GetString("type"),
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(res, err), nil
}
return SuccessResult(res), nil
}
@@ -0,0 +1,44 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type PushPlusChannel struct{ *BaseChannel }
func NewPushPlusChannel() Channel {
return &PushPlusChannel{NewBaseChannel(ChannelPushPlus, []string{FormatTypeText, FormatTypeHTML, FormatTypeMarkdown})}
}
func (c *PushPlusChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
token := config.GetString("token")
if token == "" {
return SendError("pushplus config missing: token is required"), nil
}
cli := message.PushPlus{
Token: token,
Topic: config.GetString("topic"),
Template: config.GetString("template"),
Channel: config.GetString("channel"),
Webhook: config.GetString("webhook"),
CallbackUrl: config.GetString("callback_url"),
To: config.GetString("to"),
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
@@ -0,0 +1,55 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type QyWeiXinChannel struct{ *BaseChannel }
func NewQyWeiXinChannel() Channel {
return &QyWeiXinChannel{NewBaseChannel(ChannelQyWeiXin, []string{FormatTypeMarkdown, FormatTypeText})}
}
func (c *QyWeiXinChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
accessToken := config.GetString("access_token")
if accessToken == "" {
return SendError("qyweixin config missing: access_token is required"), nil
}
contentType, formattedContent := c.FormatContent(msg)
atList := []string{}
atList = append(atList, msg.GetAtUserIds()...)
atList = append(atList, msg.GetAtMobiles()...)
if msg.AtAll {
atList = append(atList, "@all")
}
cli := message.QyWeiXin{AccessToken: accessToken}
var res []byte
var err error
switch contentType {
case FormatTypeText:
res, err = cli.SendMessageText(formattedContent, atList...)
case FormatTypeMarkdown:
res, err = cli.SendMessageMarkdown(msg.Title, formattedContent, atList...)
default:
return SendError("未知的企业微信发送内容类型:%s", contentType), nil
}
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
@@ -0,0 +1,59 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type TelegramChannel struct{ *BaseChannel }
func NewTelegramChannel() Channel {
return &TelegramChannel{NewBaseChannel(ChannelTelegram, []string{FormatTypeMarkdown, FormatTypeHTML, FormatTypeText})}
}
func (c *TelegramChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
botToken := config.GetString("bot_token")
chatID := config.GetString("chat_id")
apiHost := config.GetString("api_host")
proxyURL := config.GetString("proxy_url")
if botToken == "" || chatID == "" {
return SendError("telegram config missing: bot_token, chat_id are required"), nil
}
contentType, formattedContent := c.FormatContent(msg)
cli := message.Telegram{
BotToken: botToken,
ChatID: chatID,
ApiHost: apiHost,
ProxyURL: proxyURL,
}
var res []byte
var err error
switch contentType {
case FormatTypeText:
res, err = cli.SendMessageText(formattedContent)
case FormatTypeMarkdown:
res, err = cli.SendMessageMarkdown(formattedContent)
case FormatTypeHTML:
res, err = cli.SendMessageHTML(formattedContent)
default:
return SendError("未知的Telegram发送内容类型:%s", contentType), nil
}
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+90
View File
@@ -0,0 +1,90 @@
package channels
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
// Message 统一消息内容
type Message struct {
Title string `json:"title"`
Text string `json:"text"`
HTML string `json:"html"`
Markdown string `json:"markdown"`
URL string `json:"url"`
ImageURL string `json:"image_url"`
Summary string `json:"summary"`
AtMobiles []string `json:"at_mobiles"`
AtUserIds []string `json:"at_user_ids"`
AtAll bool `json:"at_all"`
Extra map[string]any `json:"extra"`
}
func (m *Message) HasText() bool { return m.Text != "" }
func (m *Message) HasHTML() bool { return m.HTML != "" }
func (m *Message) HasMarkdown() bool { return m.Markdown != "" }
func (m *Message) GetAtMobiles() []string {
if m.AtMobiles == nil {
return []string{}
}
return m.AtMobiles
}
func (m *Message) GetAtUserIds() []string {
if m.AtUserIds == nil {
return []string{}
}
return m.AtUserIds
}
// ChannelConfig 渠道认证配置(Key-Value 形式,各渠道自行定义字段)
type ChannelConfig map[string]string
// GetString 安全获取配置值
func (c ChannelConfig) GetString(key string) string {
if v, ok := c[key]; ok {
return v
}
return ""
}
// Result 发送结果
type Result struct {
Success bool `json:"success"`
Response string `json:"response"` // 原始响应
Error string `json:"error"` // 错误信息
}
// 消息格式类型常量
const (
FormatTypeText = "text"
FormatTypeHTML = "html"
FormatTypeMarkdown = "markdown"
)
// 渠道类型常量
const (
ChannelEmail = "Email"
ChannelDtalk = "Dtalk"
ChannelQyWeiXin = "QyWeiXin"
ChannelFeishu = "Feishu"
ChannelCustom = "Custom"
ChannelWeChatOFAccount = "WeChatOFAccount"
ChannelAliyunSMS = "AliyunSMS"
ChannelTelegram = "Telegram"
ChannelBark = "Bark"
ChannelPushMe = "PushMe"
ChannelNtfy = "Ntfy"
ChannelGotify = "Gotify"
ChannelPushPlus = "PushPlus"
ChannelVoceChat = "VoceChat"
ChannelWxPusher = "WxPusher"
)
@@ -0,0 +1,44 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type VoceChatChannel struct{ *BaseChannel }
func NewVoceChatChannel() Channel {
return &VoceChatChannel{NewBaseChannel(ChannelVoceChat, []string{FormatTypeMarkdown})}
}
func (c *VoceChatChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
server := config.GetString("server")
apiKey := config.GetString("api_key")
targetID := config.GetString("target_id")
if server == "" || apiKey == "" || targetID == "" {
return SendError("vocechat config missing: server, api_key and target_id are required"), nil
}
cli := message.VoceChat{
Server: server,
APIKey: apiKey,
TargetType: config.GetString("target_type"), // defaults to "user" in SDK if not matched differently
TargetID: targetID,
}
res, err := cli.Request(msg.Title, msg.Text)
if err != nil {
return ErrorResult(string(res), err), nil
}
return SuccessResult(string(res)), nil
}
+50
View File
@@ -0,0 +1,50 @@
package channels
import "github.com/engigu/taskpool/internal/sdk/message"
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type WeChatOFAccountChannel struct{ *BaseChannel }
func NewWeChatOFAccountChannel() Channel {
return &WeChatOFAccountChannel{NewBaseChannel(ChannelWeChatOFAccount, []string{FormatTypeText})}
}
func (c *WeChatOFAccountChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
appID := config.GetString("appID")
appSecret := config.GetString("appsecret")
tempID := config.GetString("tempid")
toAccount := config.GetString("to_account")
if appID == "" || appSecret == "" {
return SendError("wechat config missing: appID, appsecret are required"), nil
}
if toAccount == "" {
return SendError("wechat config missing: to_account is required"), nil
}
_, formattedContent := c.FormatContent(msg)
cli := message.WeChatOFAccount{
AppID: appID,
AppSecret: appSecret,
TemplateID: tempID,
ToUser: toAccount,
URL: msg.URL,
}
res, err := cli.Send(msg.Title, formattedContent)
if err != nil {
return ErrorResult(res, err), nil
}
return SuccessResult(res), nil
}
@@ -0,0 +1,90 @@
package channels
import (
"fmt"
"github.com/engigu/taskpool/internal/sdk/message"
"strconv"
"strings"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
type WxPusherChannel struct{ *BaseChannel }
func NewWxPusherChannel() Channel {
return &WxPusherChannel{NewBaseChannel(ChannelWxPusher, []string{FormatTypeText})}
}
func (c *WxPusherChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
appToken := config.GetString("app_token")
if appToken == "" {
return SendError("wxpusher config missing: app_token is required"), nil
}
uidsStr := config.GetString("uids")
topicIdsStr := config.GetString("topic_ids")
verifyPayTypeStr := config.GetString("verify_pay_type")
if uidsStr == "" && topicIdsStr == "" {
return SendError("wxpusher config missing: uids or topic_ids is required"), nil
}
var uids []string
if uidsStr != "" {
uids = strings.Split(uidsStr, ",")
for i := range uids {
uids[i] = strings.TrimSpace(uids[i])
}
}
var topicIds []int
if topicIdsStr != "" {
ids := strings.Split(topicIdsStr, ",")
for _, idStr := range ids {
idStr = strings.TrimSpace(idStr)
if id, err := strconv.Atoi(idStr); err == nil {
topicIds = append(topicIds, id)
}
}
}
verifyPayType := 0
if verifyPayTypeStr != "" {
if v, err := strconv.Atoi(verifyPayTypeStr); err == nil {
verifyPayType = v
}
}
_, formattedContent := c.FormatContent(msg)
// 如果有标题,将标题和内容合并
content := formattedContent
if msg.Title != "" {
content = fmt.Sprintf("%s\n\n%s", msg.Title, formattedContent)
}
cli := message.WxPusher{
AppToken: appToken,
Content: content,
ContentType: 1, // 仅支持文字
Uids: uids,
TopicIds: topicIds,
VerifyPayType: verifyPayType,
}
res, err := cli.Send()
if err != nil {
return ErrorResult(res, err), nil
}
return SuccessResult(res), nil
}
+213
View File
@@ -0,0 +1,213 @@
// Package messenger 提供统一的消息发送SDK
//
// 此包可独立于 Message-Push-Nest 的业务层(数据库、HTTP路由等)使用
// 适合在其他服务中直接引入来发送消息
//
// 快速使用:
//
// result, err := messenger.Send("Telegram", messenger.ChannelConfig{
// "bot_token": "your-bot-token",
// "chat_id": "your-chat-id",
// }, &messenger.Message{
// Title: "Hello",
// Text: "World",
// })
package messenger
import (
"fmt"
"github.com/engigu/taskpool/internal/sdk/messenger/channels"
"sync"
)
// Copyright (c) 2026 engigu (TaskPool). All rights reserved.
// Use of this source code is governed by the Apache License 2.0.
//
// 【重要声明 / IMPORTANT NOTICE】
// 本代码(包括其架构设计与核心实现)属于任务池(TaskPool)开源项目的一部分。
// 任何个人或组织在引用、移植、修改或重新分发此文件中的任何代码时,必须保留本版权声明,
// 并在您的衍生作品、文档、软件关于页面或说明文件中显式声明引用自任务池(TaskPool)。
//
// Anyone referencing, porting, modifying, or redistributing this code must retain this
// copyright notice and explicitly state the source: TaskPool (github.com/engigu/taskpool).
// 重导出 channels 包的类型,方便外部使用
type (
Channel = channels.Channel
Message = channels.Message
ChannelConfig = channels.ChannelConfig
Result = channels.Result
BaseChannel = channels.BaseChannel
)
// 重导出常量
const (
FormatTypeText = channels.FormatTypeText
FormatTypeHTML = channels.FormatTypeHTML
FormatTypeMarkdown = channels.FormatTypeMarkdown
ChannelEmail = channels.ChannelEmail
ChannelDtalk = channels.ChannelDtalk
ChannelQyWeiXin = channels.ChannelQyWeiXin
ChannelFeishu = channels.ChannelFeishu
ChannelCustom = channels.ChannelCustom
ChannelWeChatOFAccount = channels.ChannelWeChatOFAccount
ChannelAliyunSMS = channels.ChannelAliyunSMS
ChannelTelegram = channels.ChannelTelegram
ChannelBark = channels.ChannelBark
ChannelPushMe = channels.ChannelPushMe
ChannelNtfy = channels.ChannelNtfy
ChannelGotify = channels.ChannelGotify
ChannelPushPlus = channels.ChannelPushPlus
ChannelVoceChat = channels.ChannelVoceChat
ChannelWxPusher = channels.ChannelWxPusher
)
// 重导出辅助函数
var (
SuccessResult = channels.SuccessResult
ErrorResult = channels.ErrorResult
ErrorResultStr = channels.ErrorResultStr
SendError = channels.SendError
NewBaseChannel = channels.NewBaseChannel
)
// channelFactory 渠道工厂注册表
var (
channelFactories = map[string]func() Channel{}
factoryMu sync.RWMutex
)
func init() {
// 注册所有内置渠道
RegisterChannel(ChannelEmail, func() Channel { return channels.NewEmailChannel() })
RegisterChannel(ChannelDtalk, func() Channel { return channels.NewDtalkChannel() })
RegisterChannel(ChannelQyWeiXin, func() Channel { return channels.NewQyWeiXinChannel() })
RegisterChannel(ChannelFeishu, func() Channel { return channels.NewFeishuChannel() })
RegisterChannel(ChannelTelegram, func() Channel { return channels.NewTelegramChannel() })
RegisterChannel(ChannelBark, func() Channel { return channels.NewBarkChannel() })
RegisterChannel(ChannelNtfy, func() Channel { return channels.NewNtfyChannel() })
RegisterChannel(ChannelGotify, func() Channel { return channels.NewGotifyChannel() })
RegisterChannel(ChannelPushMe, func() Channel { return channels.NewPushMeChannel() })
RegisterChannel(ChannelCustom, func() Channel { return channels.NewCustomChannel() })
RegisterChannel(ChannelWeChatOFAccount, func() Channel { return channels.NewWeChatOFAccountChannel() })
RegisterChannel(ChannelAliyunSMS, func() Channel { return channels.NewAliyunSMSChannel() })
RegisterChannel(ChannelPushPlus, func() Channel { return channels.NewPushPlusChannel() })
RegisterChannel(ChannelVoceChat, func() Channel { return channels.NewVoceChatChannel() })
RegisterChannel(ChannelWxPusher, func() Channel { return channels.NewWxPusherChannel() })
}
// RegisterChannel 注册自定义渠道(可用于扩展)
func RegisterChannel(channelType string, factory func() Channel) {
factoryMu.Lock()
defer factoryMu.Unlock()
channelFactories[channelType] = factory
}
// GetChannel 获取渠道实例
func GetChannel(channelType string) (Channel, error) {
factoryMu.RLock()
defer factoryMu.RUnlock()
factory, ok := channelFactories[channelType]
if !ok {
return nil, fmt.Errorf("未知的渠道类型: %s", channelType)
}
return factory(), nil
}
// ListChannels 列出所有已注册的渠道类型
func ListChannels() []string {
factoryMu.RLock()
defer factoryMu.RUnlock()
types := make([]string, 0, len(channelFactories))
for t := range channelFactories {
types = append(types, t)
}
return types
}
// Send 发送消息的便捷函数
//
// 参数:
// - channelType: 渠道类型(如 "Telegram", "Dtalk" 等)
// - config: 渠道必要的认证配置
// - msg: 消息内容
//
// 使用示例:
//
// result, err := messenger.Send("Ntfy", messenger.ChannelConfig{
// "topic": "my-topic",
// }, &messenger.Message{
// Title: "Alert",
// Text: "Something happened!",
// })
func Send(channelType string, config ChannelConfig, msg *Message) (*Result, error) {
ch, err := GetChannel(channelType)
if err != nil {
return nil, err
}
return ch.Send(config, msg)
}
// Client 消息发送客户端(支持预设默认配置)
type Client struct {
defaultConfigs map[string]ChannelConfig
mu sync.RWMutex
}
// NewClient 创建消息发送客户端
func NewClient() *Client {
return &Client{
defaultConfigs: make(map[string]ChannelConfig),
}
}
// SetDefaultConfig 为指定渠道设置默认配置
//
// 使用示例:
//
// client := messenger.NewClient()
// client.SetDefaultConfig("Telegram", messenger.ChannelConfig{
// "bot_token": "default-token",
// "chat_id": "default-chat",
// })
// // 后续发送时不需要再传 config 的相关字段
// result, err := client.Send("Telegram", nil, &messenger.Message{...})
func (c *Client) SetDefaultConfig(channelType string, config ChannelConfig) {
c.mu.Lock()
defer c.mu.Unlock()
c.defaultConfigs[channelType] = config
}
// Send 使用客户端发送消息(会合并默认配置)
func (c *Client) Send(channelType string, config ChannelConfig, msg *Message) (*Result, error) {
mergedConfig := c.mergeConfig(channelType, config)
return Send(channelType, mergedConfig, msg)
}
func (c *Client) mergeConfig(channelType string, config ChannelConfig) ChannelConfig {
c.mu.RLock()
defer c.mu.RUnlock()
defaultConfig, hasDefault := c.defaultConfigs[channelType]
if !hasDefault && config == nil {
return ChannelConfig{}
}
if !hasDefault {
return config
}
if config == nil {
return defaultConfig
}
// 合并:config 覆盖 defaultConfig
merged := make(ChannelConfig, len(defaultConfig)+len(config))
for k, v := range defaultConfig {
merged[k] = v
}
for k, v := range config {
merged[k] = v
}
return merged
}