-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.go
More file actions
141 lines (122 loc) · 4.36 KB
/
Copy pathwebhooks.go
File metadata and controls
141 lines (122 loc) · 4.36 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"flag"
"fmt"
"strings"
)
// This file implements `splashify webhooks` — the CLI mirror of webhook
// management. It covers:
//
// webhooks list all registered webhooks
// webhooks create --url <url> --events event1,event2 ...
// register a new webhook
// webhooks <id> show one webhook
// webhooks <id> test send a synthetic test event
// webhooks <id> rotate-secret rotate the signing secret
// webhooks <id> delete unregister a webhook
//
// Webhook events may include: incoming_message, message_sent, message_delivered,
// message_read, message_failed, team_member_assigned, team_member_unassigned,
// wallet_low, wallet_empty, etc.
// ─── splashify webhooks ──────────────────────────────────────────────────────
func cmdWebhooks(args []string) error {
if len(args) == 0 {
return runReq("GET", "/app/webhooks", nil)
}
switch args[0] {
case "create", "new", "add":
return cmdWebhookCreate(args[1:])
default:
return cmdWebhook(args)
}
}
// cmdWebhookCreate registers a new webhook. Required: url and events.
// Optional: description.
//
// splashify webhooks create --url "https://..." --events "incoming_message,message_delivered"
// [--description "..."]
func cmdWebhookCreate(args []string) error {
fs := flag.NewFlagSet("webhooks create", flag.ContinueOnError)
url := fs.String("url", "", "webhook URL (required)")
events := fs.String("events", "", "comma-separated event types (required)")
desc := fs.String("description", "", "optional description")
if err := fs.Parse(args); err != nil {
return err
}
if *url == "" || *events == "" {
return fmt.Errorf(`usage: splashify webhooks create --url "https://..." --events "incoming_message,message_delivered" [--description "..."]`)
}
eventList := splitTags(*events)
if len(eventList) == 0 {
return fmt.Errorf("--events must contain at least one event type")
}
body := map[string]any{
"url": *url,
"events": eventList,
}
if *desc != "" {
body["description"] = *desc
}
return runReq("POST", "/app/webhooks", body)
}
// cmdWebhook handles per-webhook actions: show, test, rotate-secret, delete.
//
// splashify webhooks <id> show one webhook
// splashify webhooks <id> test send synthetic event
// splashify webhooks <id> rotate-secret get new signing secret
// splashify webhooks <id> delete unregister
func cmdWebhook(args []string) error {
if len(args) == 0 {
return fmt.Errorf("usage: splashify webhooks <webhook_id> [test|rotate-secret|delete]")
}
id := args[0]
if len(args) == 1 {
return runReq("GET", "/app/webhooks/"+id, nil)
}
switch args[1] {
case "test", "send-test":
return runReq("POST", "/app/webhooks/"+id+"/test", nil)
case "rotate", "rotate-secret", "rotate-key", "new-secret":
return runReq("POST", "/app/webhooks/"+id+"/rotate-secret", nil)
case "delete", "remove", "rm", "unregister":
return runReq("DELETE", "/app/webhooks/"+id, nil)
case "update", "edit", "patch":
return cmdWebhookUpdate(id, args[2:])
default:
return fmt.Errorf("unknown webhook action: %s\nrun: splashify webhooks", args[1])
}
}
// cmdWebhookUpdate PATCHes a webhook. Accepts partial updates: --url, --events,
// --description.
//
// splashify webhooks <id> update --url "https://..." --events "..."
func cmdWebhookUpdate(id string, args []string) error {
fs := flag.NewFlagSet("webhooks update", flag.ContinueOnError)
url := fs.String("url", "", "new webhook URL")
events := fs.String("events", "", "comma-separated event types")
desc := fs.String("description", "", "new description")
if err := fs.Parse(args); err != nil {
return err
}
passed := map[string]bool{}
fs.Visit(func(f *flag.Flag) { passed[f.Name] = true })
if len(passed) == 0 {
return fmt.Errorf("usage: splashify webhooks %s update [--url ...] [--events ...] [--description ...]", id)
}
body := map[string]any{}
if passed["url"] {
body["url"] = *url
}
if passed["events"] {
eventList := splitTags(*events)
if len(eventList) == 0 {
return fmt.Errorf("--events must contain at least one event type")
}
body["events"] = eventList
}
if passed["description"] {
body["description"] = *desc
}
return runReq("PATCH", "/app/webhooks/"+id, body)
}