feat: add vocechat push #82
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VoceChat struct {
|
||||||
|
Server string
|
||||||
|
APIKey string
|
||||||
|
TargetType string // "user" or "group"
|
||||||
|
TargetID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VoceChat) Request(title, content string) ([]byte, error) {
|
||||||
|
if v.Server == "" || v.APIKey == "" || v.TargetID == "" {
|
||||||
|
return nil, fmt.Errorf("vocechat config missing: server, api_key and target_id are required")
|
||||||
|
}
|
||||||
|
|
||||||
|
server := strings.TrimSuffix(v.Server, "/")
|
||||||
|
endpoint := "send_to_user"
|
||||||
|
if v.TargetType == "group" {
|
||||||
|
endpoint = "send_to_group"
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/api/bot/%s/%s", server, endpoint, v.TargetID)
|
||||||
|
|
||||||
|
// Use text/plain for now as requested
|
||||||
|
body := content
|
||||||
|
if title != "" {
|
||||||
|
body = fmt.Sprintf("%s\n\n%s", title, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(body)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "text/plain")
|
||||||
|
req.Header.Set("x-api-key", v.APIKey)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return respBody, fmt.Errorf("vocechat response error (status %d): %s", resp.StatusCode, string(respBody))
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBody, nil
|
||||||
|
}
|
||||||
@@ -73,4 +73,5 @@ const (
|
|||||||
ChannelNtfy = "Ntfy"
|
ChannelNtfy = "Ntfy"
|
||||||
ChannelGotify = "Gotify"
|
ChannelGotify = "Gotify"
|
||||||
ChannelPushPlus = "PushPlus"
|
ChannelPushPlus = "PushPlus"
|
||||||
|
ChannelVoceChat = "VoceChat"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package channels
|
||||||
|
|
||||||
|
import "github.com/engigu/baihu-panel/internal/sdk/message"
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -48,6 +48,7 @@ const (
|
|||||||
ChannelNtfy = channels.ChannelNtfy
|
ChannelNtfy = channels.ChannelNtfy
|
||||||
ChannelGotify = channels.ChannelGotify
|
ChannelGotify = channels.ChannelGotify
|
||||||
ChannelPushPlus = channels.ChannelPushPlus
|
ChannelPushPlus = channels.ChannelPushPlus
|
||||||
|
ChannelVoceChat = channels.ChannelVoceChat
|
||||||
)
|
)
|
||||||
|
|
||||||
// 重导出辅助函数
|
// 重导出辅助函数
|
||||||
@@ -80,6 +81,7 @@ func init() {
|
|||||||
RegisterChannel(ChannelWeChatOFAccount, func() Channel { return channels.NewWeChatOFAccountChannel() })
|
RegisterChannel(ChannelWeChatOFAccount, func() Channel { return channels.NewWeChatOFAccountChannel() })
|
||||||
RegisterChannel(ChannelAliyunSMS, func() Channel { return channels.NewAliyunSMSChannel() })
|
RegisterChannel(ChannelAliyunSMS, func() Channel { return channels.NewAliyunSMSChannel() })
|
||||||
RegisterChannel(ChannelPushPlus, func() Channel { return channels.NewPushPlusChannel() })
|
RegisterChannel(ChannelPushPlus, func() Channel { return channels.NewPushPlusChannel() })
|
||||||
|
RegisterChannel(ChannelVoceChat, func() Channel { return channels.NewVoceChatChannel() })
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterChannel 注册自定义渠道(可用于扩展)
|
// RegisterChannel 注册自定义渠道(可用于扩展)
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ var SupportedChannelTypes = []map[string]string{
|
|||||||
// {"type": messenger.ChannelWeChatOFAccount, "label": "微信公众号"},
|
// {"type": messenger.ChannelWeChatOFAccount, "label": "微信公众号"},
|
||||||
{"type": messenger.ChannelAliyunSMS, "label": "阿里云短信"},
|
{"type": messenger.ChannelAliyunSMS, "label": "阿里云短信"},
|
||||||
{"type": messenger.ChannelPushPlus, "label": "PushPlus"},
|
{"type": messenger.ChannelPushPlus, "label": "PushPlus"},
|
||||||
|
{"type": messenger.ChannelVoceChat, "label": "VoceChat"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportedEvents 支持的事件类型
|
// SupportedEvents 支持的事件类型
|
||||||
|
|||||||
@@ -117,6 +117,13 @@ const channelConfigFields: Record<string, { key: string; label: string; required
|
|||||||
{ key: 'callback_url', label: '回调地址', required: false },
|
{ key: 'callback_url', label: '回调地址', required: false },
|
||||||
{ key: 'to', label: '好友令牌', required: false },
|
{ key: 'to', label: '好友令牌', required: false },
|
||||||
],
|
],
|
||||||
|
VoceChat: [
|
||||||
|
{ key: 'server', label: '服务地址', required: true, placeholder: 'https://vocechat.yourdomain.com' },
|
||||||
|
{ key: 'api_key', label: 'API Key', required: true, placeholder: 'Bot API Key' },
|
||||||
|
{ key: 'target_id', label: '目标 ID', required: true, placeholder: 'uid 或 gid' },
|
||||||
|
{ key: 'target_type', label: '目标类型', required: false, placeholder: 'user (默认) / group' },
|
||||||
|
{ key: 'note', label: '说明', required: false, placeholder: '当前仅支持 text/plain', type: 'note' },
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载数据
|
// 加载数据
|
||||||
|
|||||||
@@ -109,14 +109,17 @@ function updateConfigField(key: string, value: string) {
|
|||||||
</Label>
|
</Label>
|
||||||
<div class="col-span-3">
|
<div class="col-span-3">
|
||||||
<Input
|
<Input
|
||||||
v-if="!field.type || field.type !== 'textarea'"
|
v-if="!field.type"
|
||||||
:model-value="channel.config?.[field.key] || ''"
|
:model-value="channel.config?.[field.key] || ''"
|
||||||
@update:model-value="updateConfigField(field.key, String($event))"
|
@update:model-value="updateConfigField(field.key, String($event))"
|
||||||
:placeholder="field.placeholder || ''"
|
:placeholder="field.placeholder || ''"
|
||||||
class="text-sm"
|
class="text-sm"
|
||||||
/>
|
/>
|
||||||
|
<div v-else-if="field.type === 'note'" class="text-xs text-muted-foreground pt-2">
|
||||||
|
{{ field.placeholder || '' }}
|
||||||
|
</div>
|
||||||
<textarea
|
<textarea
|
||||||
v-else
|
v-else-if="field.type === 'textarea'"
|
||||||
:value="channel.config?.[field.key] || ''"
|
:value="channel.config?.[field.key] || ''"
|
||||||
@input="(e: Event) => updateConfigField(field.key, (e.target as HTMLTextAreaElement).value)"
|
@input="(e: Event) => updateConfigField(field.key, (e.target as HTMLTextAreaElement).value)"
|
||||||
:placeholder="field.placeholder || ''"
|
:placeholder="field.placeholder || ''"
|
||||||
|
|||||||
Reference in New Issue
Block a user