Files
picoclaw/pkg/channels/manager_channel.go
T
loveuer 7b56867f8a refactor: remove LINE, WhatsApp and Discord channels
Remove LINE, WhatsApp bridge/native and Discord adapters and configuration.
Drop unused SDKs, the Discord fork override and the native WhatsApp build target.
Clean up migration support and docs; preserve shared credential and voice coverage.
Validated with full Go tests, static checks, docs lint and a Linux amd64 build.
2026-11-12 09:51:39 +08:00

113 lines
2.8 KiB
Go

package channels
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"log"
"github.com/sipeed/picoclaw/pkg/config"
)
func toChannelHashes(cfg *config.Config) map[string]string {
result := make(map[string]string)
ch := cfg.Channels
marshal, err := json.Marshal(ch)
if err != nil {
log.Printf("[manager_channel] failed to marshal channels config: %v", err)
return result
}
var channelConfig map[string]map[string]any
if err := json.Unmarshal(marshal, &channelConfig); err != nil {
log.Printf("[manager_channel] failed to unmarshal channels config: %v", err)
return result
}
for key, value := range channelConfig {
if enabled, ok := value["enabled"].(bool); !ok || !enabled {
continue
}
hiddenValues(key, value, ch.Get(key))
valueBytes, err := json.Marshal(value)
if err != nil {
log.Printf("[manager_channel] failed to marshal channel %s config: %v", key, err)
continue
}
hash := md5.Sum(valueBytes)
result[key] = hex.EncodeToString(hash[:])
}
return result
}
func hiddenValues(key string, value map[string]any, ch *config.Channel) {
v, err := ch.GetDecoded()
if err != nil {
return
}
switch key {
case "telegram":
if settings, ok := v.(*config.TelegramSettings); ok {
value["token"] = settings.Token.String()
}
case "onebot":
if settings, ok := v.(*config.OneBotSettings); ok {
value["token"] = settings.AccessToken.String()
}
case "wecom":
if settings, ok := v.(*config.WeComSettings); ok {
value["secret"] = settings.Secret.String()
}
case "dingtalk":
if settings, ok := v.(*config.DingTalkSettings); ok {
value["secret"] = settings.ClientSecret.String()
}
case "qq":
if settings, ok := v.(*config.QQSettings); ok {
value["secret"] = settings.AppSecret.String()
}
case "feishu":
if settings, ok := v.(*config.FeishuSettings); ok {
value["app_secret"] = settings.AppSecret.String()
value["encrypt_key"] = settings.EncryptKey.String()
value["verification_token"] = settings.VerificationToken.String()
}
case "mqtt":
if settings, ok := v.(*config.MQTTSettings); ok {
value["username"] = settings.Username.String()
value["password"] = settings.Password.String()
}
}
}
func compareChannels(old, news map[string]string) (added, removed []string) {
for key, newHash := range news {
if oldHash, ok := old[key]; ok {
if newHash != oldHash {
removed = append(removed, key)
added = append(added, key)
}
} else {
added = append(added, key)
}
}
for key := range old {
if _, ok := news[key]; !ok {
removed = append(removed, key)
}
}
return added, removed
}
func toChannelConfig(cfg *config.Config, list []string) (*config.ChannelsConfig, error) {
result := make(config.ChannelsConfig)
for _, name := range list {
bc, ok := cfg.Channels[name]
if !ok || !bc.Enabled {
continue
}
result[name] = bc
}
return &result, nil
}