mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
ad8c2d48c8
# Conflicts: # cmd/picoclaw/main.go # pkg/agent/context.go # pkg/agent/loop.go # pkg/channels/dingtalk.go # pkg/channels/feishu_64.go # pkg/channels/line.go # pkg/channels/manager.go # pkg/config/config.go # pkg/migrate/migrate_test.go # pkg/providers/anthropic/provider_test.go # pkg/providers/claude_provider_test.go # pkg/providers/http_provider.go # pkg/providers/openai_compat/provider.go # pkg/providers/protocoltypes/types.go # pkg/providers/types.go
193 lines
3.8 KiB
Go
193 lines
3.8 KiB
Go
package channels
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/bus"
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"github.com/sipeed/picoclaw/pkg/utils"
|
|
)
|
|
|
|
type WhatsAppChannel struct {
|
|
*BaseChannel
|
|
conn *websocket.Conn
|
|
config config.WhatsAppConfig
|
|
url string
|
|
mu sync.Mutex
|
|
connected bool
|
|
}
|
|
|
|
func NewWhatsAppChannel(cfg config.WhatsAppConfig, bus *bus.MessageBus) (*WhatsAppChannel, error) {
|
|
base := NewBaseChannel("whatsapp", cfg, bus, cfg.AllowFrom)
|
|
|
|
return &WhatsAppChannel{
|
|
BaseChannel: base,
|
|
config: cfg,
|
|
url: cfg.BridgeURL,
|
|
connected: false,
|
|
}, nil
|
|
}
|
|
|
|
func (c *WhatsAppChannel) Start(ctx context.Context) error {
|
|
log.Printf("Starting WhatsApp channel connecting to %s...", c.url)
|
|
|
|
dialer := websocket.DefaultDialer
|
|
dialer.HandshakeTimeout = 10 * time.Second
|
|
|
|
conn, _, err := dialer.Dial(c.url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to WhatsApp bridge: %w", err)
|
|
}
|
|
|
|
c.mu.Lock()
|
|
c.conn = conn
|
|
c.connected = true
|
|
c.mu.Unlock()
|
|
|
|
c.setRunning(true)
|
|
log.Println("WhatsApp channel connected")
|
|
|
|
go c.listen(ctx)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *WhatsAppChannel) Stop(ctx context.Context) error {
|
|
log.Println("Stopping WhatsApp channel...")
|
|
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
if c.conn != nil {
|
|
if err := c.conn.Close(); err != nil {
|
|
log.Printf("Error closing WhatsApp connection: %v", err)
|
|
}
|
|
c.conn = nil
|
|
}
|
|
|
|
c.connected = false
|
|
c.setRunning(false)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *WhatsAppChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
if c.conn == nil {
|
|
return fmt.Errorf("whatsapp connection not established")
|
|
}
|
|
|
|
payload := map[string]any{
|
|
"type": "message",
|
|
"to": msg.ChatID,
|
|
"content": msg.Content,
|
|
}
|
|
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal message: %w", err)
|
|
}
|
|
|
|
if err := c.conn.WriteMessage(websocket.TextMessage, data); err != nil {
|
|
return fmt.Errorf("failed to send message: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *WhatsAppChannel) listen(ctx context.Context) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
c.mu.Lock()
|
|
conn := c.conn
|
|
c.mu.Unlock()
|
|
|
|
if conn == nil {
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
if err != nil {
|
|
log.Printf("WhatsApp read error: %v", err)
|
|
time.Sleep(2 * time.Second)
|
|
continue
|
|
}
|
|
|
|
var msg map[string]any
|
|
if err := json.Unmarshal(message, &msg); err != nil {
|
|
log.Printf("Failed to unmarshal WhatsApp message: %v", err)
|
|
continue
|
|
}
|
|
|
|
msgType, ok := msg["type"].(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if msgType == "message" {
|
|
c.handleIncomingMessage(msg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *WhatsAppChannel) handleIncomingMessage(msg map[string]any) {
|
|
senderID, ok := msg["from"].(string)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
chatID, ok := msg["chat"].(string)
|
|
if !ok {
|
|
chatID = senderID
|
|
}
|
|
|
|
content, ok := msg["content"].(string)
|
|
if !ok {
|
|
content = ""
|
|
}
|
|
|
|
var mediaPaths []string
|
|
if mediaData, ok := msg["media"].([]any); ok {
|
|
mediaPaths = make([]string, 0, len(mediaData))
|
|
for _, m := range mediaData {
|
|
if path, ok := m.(string); ok {
|
|
mediaPaths = append(mediaPaths, path)
|
|
}
|
|
}
|
|
}
|
|
|
|
metadata := make(map[string]string)
|
|
if messageID, ok := msg["id"].(string); ok {
|
|
metadata["message_id"] = messageID
|
|
}
|
|
if userName, ok := msg["from_name"].(string); ok {
|
|
metadata["user_name"] = userName
|
|
}
|
|
|
|
if chatID == senderID {
|
|
metadata["peer_kind"] = "direct"
|
|
metadata["peer_id"] = senderID
|
|
} else {
|
|
metadata["peer_kind"] = "group"
|
|
metadata["peer_id"] = chatID
|
|
}
|
|
|
|
log.Printf("WhatsApp message from %s: %s...", senderID, utils.Truncate(content, 50))
|
|
|
|
c.HandleMessage(senderID, chatID, content, mediaPaths, metadata)
|
|
}
|