|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/fastclaw-ai/weclaw/ilink" |
| 11 | + "github.com/fastclaw-ai/weclaw/messaging" |
| 12 | +) |
| 13 | + |
| 14 | +// Server provides an HTTP API for sending messages. |
| 15 | +type Server struct { |
| 16 | + clients []*ilink.Client |
| 17 | + addr string |
| 18 | +} |
| 19 | + |
| 20 | +// NewServer creates an API server. |
| 21 | +func NewServer(clients []*ilink.Client, addr string) *Server { |
| 22 | + if addr == "" { |
| 23 | + addr = "127.0.0.1:18011" |
| 24 | + } |
| 25 | + return &Server{clients: clients, addr: addr} |
| 26 | +} |
| 27 | + |
| 28 | +// SendRequest is the JSON body for POST /api/send. |
| 29 | +type SendRequest struct { |
| 30 | + To string `json:"to"` |
| 31 | + Text string `json:"text"` |
| 32 | +} |
| 33 | + |
| 34 | +// Run starts the HTTP server. Blocks until ctx is cancelled. |
| 35 | +func (s *Server) Run(ctx context.Context) error { |
| 36 | + mux := http.NewServeMux() |
| 37 | + mux.HandleFunc("/api/send", s.handleSend) |
| 38 | + mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 39 | + w.WriteHeader(http.StatusOK) |
| 40 | + fmt.Fprintln(w, "ok") |
| 41 | + }) |
| 42 | + |
| 43 | + srv := &http.Server{Addr: s.addr, Handler: mux} |
| 44 | + |
| 45 | + go func() { |
| 46 | + <-ctx.Done() |
| 47 | + srv.Shutdown(context.Background()) |
| 48 | + }() |
| 49 | + |
| 50 | + log.Printf("[api] listening on %s", s.addr) |
| 51 | + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 52 | + return err |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (s *Server) handleSend(w http.ResponseWriter, r *http.Request) { |
| 58 | + if r.Method != http.MethodPost { |
| 59 | + http.Error(w, "POST only", http.StatusMethodNotAllowed) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + var req SendRequest |
| 64 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 65 | + http.Error(w, "invalid JSON: "+err.Error(), http.StatusBadRequest) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if req.To == "" || req.Text == "" { |
| 70 | + http.Error(w, `"to" and "text" are required`, http.StatusBadRequest) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + if len(s.clients) == 0 { |
| 75 | + http.Error(w, "no accounts configured", http.StatusServiceUnavailable) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // Use the first client |
| 80 | + client := s.clients[0] |
| 81 | + if err := messaging.SendTextReply(r.Context(), client, req.To, req.Text, "", ""); err != nil { |
| 82 | + log.Printf("[api] send failed: %v", err) |
| 83 | + http.Error(w, "send failed: "+err.Error(), http.StatusInternalServerError) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + log.Printf("[api] sent message to %s: %q", req.To, req.Text) |
| 88 | + w.Header().Set("Content-Type", "application/json") |
| 89 | + json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) |
| 90 | +} |
0 commit comments