-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (65 loc) · 1.71 KB
/
main.go
File metadata and controls
77 lines (65 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Example: agent — send a message to the OpenClaw agent.
//
// Usage:
//
// GATEWAY_URL=ws://localhost:18789 \
// GATEWAY_TOKEN=mytoken \
// MESSAGE="帮我写一个 hello world" \
// go run ./examples/agent/
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/google/uuid"
"github.com/yuchou87/openclaw-go/client"
"github.com/yuchou87/openclaw-go/protocol"
)
func main() {
url := getenv("GATEWAY_URL", "ws://localhost:18789")
token := getenv("GATEWAY_TOKEN", "")
password := getenv("GATEWAY_PASSWORD", "")
message := getenv("MESSAGE", "Hello from the Go SDK!")
sessionKey := getenv("SESSION_KEY", "main")
c := client.New(url, token, password)
ctx := context.Background()
fmt.Printf("Connecting to %s ...\n", url)
if err := c.Connect(ctx); err != nil {
log.Fatalf("Connect: %v", err)
}
defer c.Close()
fmt.Println("✓ Connected")
// Subscribe to events before sending so we catch the response.
events, err := c.Subscribe(ctx)
if err != nil {
log.Fatalf("Subscribe: %v", err)
}
thinking := "default"
idempotencyKey := uuid.NewString()
fmt.Printf("Sending agent message (session=%s): %q\n", sessionKey, message)
if err := c.SendAgent(ctx, protocol.AgentParams{
Message: message,
SessionKey: &sessionKey,
Thinking: &thinking,
IdempotencyKey: idempotencyKey,
}); err != nil {
log.Fatalf("SendAgent: %v", err)
}
fmt.Println("✓ Agent message sent. Waiting for events...")
// Print the first few events.
count := 0
for evt := range events {
fmt.Printf(" [event] %s: %s\n", evt.Event, string(evt.Payload))
count++
if count >= 5 {
break
}
}
}
func getenv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}