Skip to content

Commit 93869e8

Browse files
committed
[webhooks] implement webhooks management commands
1 parent 34adbe2 commit 93869e8

File tree

11 files changed

+146
-47
lines changed

11 files changed

+146
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,4 @@ $RECYCLE.BIN/
125125

126126
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
127127

128+
go.work*

README.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# SMS Gateway for Android™ CLI
44

5-
A command-line interface for working with SMS Gateway for Android.
5+
A command-line interface for interacting with the SMS Gateway for Android API.
66

77
## Table of Contents
88

@@ -16,8 +16,6 @@ A command-line interface for working with SMS Gateway for Android.
1616
- [Output Formats](#output-formats)
1717
- [Usage](#usage)
1818
- [Commands](#commands)
19-
- [Send a message](#send-a-message)
20-
- [Get the status of a sent message](#get-the-status-of-a-sent-message)
2119
- [Exit codes](#exit-codes)
2220
- [Examples](#examples)
2321
- [Output formats](#output-formats-1)
@@ -91,32 +89,14 @@ smsgate [global options] command [command options] [arguments...]
9189

9290
### Commands
9391

94-
The CLI supports the following commands:
92+
The CLI offers two main groups of commands:
9593

96-
- `send` - send a message with single or multiple recipients
97-
- `status` - get the status of a sent message by message ID
94+
- **Messages**: Commands for sending messages and checking their status.
95+
- **Webhooks**: Commands for managing webhooks, including creating, updating, and deleting them.
9896

99-
#### Send a message
100-
101-
Syntax:
102-
```bash
103-
smsgate send [options] 'Message content'
104-
```
105-
106-
| Option | Description | Default value | Example |
107-
| --------------------------- | ------------------------------------------------------------------------------------------ | ------------- | ----------------------- |
108-
| `--id` | Message ID, will be generated if not provided | empty | `zXDYfTmTVf3iMd16zzdBj` |
109-
| `--phone`, `--phones`, `-p` | Phone number, can be used multiple times or with comma-separated values, E.164 format | **required** | `+19162255887` |
110-
| `--sim`, `--simNumber` | One-based SIM card slot number, if empty, the default SIM card will be used | empty | `2` |
111-
| `--ttl` | Time-to-live (TTL), if empty, the message will not expire<br>Conflicts with `--validUntil` | empty | `1h30m` |
112-
| `--validUntil` | Valid until, if empty, the message will not expire<br>Conflicts with `--ttl` | empty | `2024-12-31T23:59:59Z` |
113-
114-
#### Get the status of a sent message
115-
116-
Syntax:
117-
```bash
118-
smsgate status 'Message ID'
119-
```
97+
For a complete list of available commands, you can:
98+
- Run `smsgate help` or `smsgate --help` in your terminal.
99+
- Visit the official documentation at [docs.sms-gate.app](https://docs.sms-gate.app/integration/cli/#commands).
120100

121101
### Exit codes
122102

cmd/smsgate/smsgate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func main() {
9191
}
9292

9393
app.Commands = append(app.Commands, messages.Commands...)
94-
// app.Commands = append(app.Commands, webhooks.Commands...)
94+
app.Commands = append(app.Commands, webhooks.Commands...)
9595

9696
if err := app.Run(os.Args); err != nil {
9797
fmt.Fprintf(os.Stderr, "Error: %v\n", err)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/android-sms-gateway/cli
33
go 1.23.2
44

55
require (
6-
github.com/android-sms-gateway/client-go v1.1.0
6+
github.com/android-sms-gateway/client-go v1.4.0
7+
github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3
78
github.com/joho/godotenv v1.5.1
89
github.com/urfave/cli/v2 v2.27.5
910
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
github.com/android-sms-gateway/client-go v1.1.0 h1:mAPsueRrY/qOdQAU5yO3uLQAb7Po+3jBxB1tiqyClvg=
22
github.com/android-sms-gateway/client-go v1.1.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
3+
github.com/android-sms-gateway/client-go v1.3.1-0.20250129225754-59c34aa9d1c0 h1:OQgfNIUB32Ui1plEtfjjRAP85fpW8gBQgc69g5IAimI=
4+
github.com/android-sms-gateway/client-go v1.3.1-0.20250129225754-59c34aa9d1c0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
5+
github.com/android-sms-gateway/client-go v1.4.0 h1:yc6VqbiF1p/a0Ygw49VrKYHwcBJQIidcBz4C30jb27Q=
6+
github.com/android-sms-gateway/client-go v1.4.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
7+
github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3 h1:mq9rmBMCCzqGnZtbQqFSd+Ua3fahqUOYaTf26YFhWJc=
8+
github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3/go.mod h1:WDqc7HZNqHxUTisArkYIBZtqUfJBVyPWeQI+FMwEzAw=
39
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
410
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
511
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=

internal/commands/webhooks/delete.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package webhooks
22

33
import (
4+
"fmt"
5+
46
"github.com/android-sms-gateway/cli/internal/core/codes"
7+
"github.com/android-sms-gateway/cli/internal/utils/metadata"
58
"github.com/urfave/cli/v2"
69
)
710

@@ -18,9 +21,19 @@ var delete = &cli.Command{
1821
return cli.Exit("ID is empty", codes.ParamsError)
1922
}
2023

21-
// client := metadata.GetClient(c.App.Metadata)
22-
// renderer := metadata.GetRenderer(c.App.Metadata)
24+
client := metadata.GetClient(c.App.Metadata)
25+
renderer := metadata.GetRenderer(c.App.Metadata)
26+
27+
err := client.DeleteWebhook(c.Context, id)
28+
if err != nil {
29+
return cli.Exit(err.Error(), codes.ClientError)
30+
}
2331

24-
return cli.Exit("Not implemented", codes.ParamsError)
32+
b, err := renderer.Success()
33+
if err != nil {
34+
return cli.Exit(err.Error(), codes.OutputError)
35+
}
36+
fmt.Println(b)
37+
return nil
2538
},
2639
}

internal/commands/webhooks/list.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package webhooks
22

33
import (
4+
"fmt"
5+
46
"github.com/android-sms-gateway/cli/internal/core/codes"
7+
"github.com/android-sms-gateway/cli/internal/utils/metadata"
58
"github.com/urfave/cli/v2"
69
)
710

@@ -11,9 +14,20 @@ var list = &cli.Command{
1114
Aliases: []string{"l", "ls"},
1215
Usage: "List webhooks",
1316
Action: func(c *cli.Context) error {
14-
// client := metadata.GetClient(c.App.Metadata)
15-
// renderer := metadata.GetRenderer(c.App.Metadata)
17+
client := metadata.GetClient(c.App.Metadata)
18+
renderer := metadata.GetRenderer(c.App.Metadata)
19+
20+
res, err := client.ListWebhooks(c.Context)
21+
if err != nil {
22+
return cli.Exit(err.Error(), codes.ClientError)
23+
}
24+
25+
b, err := renderer.Webhooks(res)
26+
if err != nil {
27+
return cli.Exit(err.Error(), codes.OutputError)
28+
}
29+
fmt.Println(b)
1630

17-
return cli.Exit("Not implemented", codes.ParamsError)
31+
return nil
1832
},
1933
}

internal/commands/webhooks/register.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package webhooks
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/android-sms-gateway/cli/internal/core/codes"
8+
"github.com/android-sms-gateway/cli/internal/utils/metadata"
59
"github.com/android-sms-gateway/client-go/smsgateway"
10+
"github.com/capcom6/go-helpers/slices"
611
"github.com/urfave/cli/v2"
712
)
813

@@ -15,13 +20,16 @@ var register = &cli.Command{
1520
Flags: []cli.Flag{
1621
&cli.StringFlag{
1722
Name: "id",
18-
Usage: "ID",
23+
Usage: "ID, optional",
1924
Required: false,
2025
},
2126
&cli.StringFlag{
22-
Name: "event",
23-
Aliases: []string{"e"},
24-
Usage: "Event",
27+
Name: "event",
28+
Aliases: []string{"e"},
29+
Usage: "Event, one of: " + strings.Join(
30+
slices.Map(smsgateway.WebhookEventTypes(), func(e smsgateway.WebhookEvent) string { return string(e) }),
31+
", ",
32+
),
2533
Required: true,
2634
Action: func(c *cli.Context, event string) error {
2735
if !smsgateway.IsValidWebhookEvent(event) {
@@ -38,15 +46,26 @@ var register = &cli.Command{
3846
return cli.Exit("URL is empty", codes.ParamsError)
3947
}
4048

41-
// client := metadata.GetClient(c.App.Metadata)
42-
// renderer := metadata.GetRenderer(c.App.Metadata)
49+
client := metadata.GetClient(c.App.Metadata)
50+
renderer := metadata.GetRenderer(c.App.Metadata)
51+
52+
req := smsgateway.Webhook{
53+
ID: c.String("id"),
54+
URL: url,
55+
Event: c.String("event"),
56+
}
57+
58+
res, err := client.RegisterWebhook(c.Context, req)
59+
if err != nil {
60+
return cli.Exit(err.Error(), codes.ClientError)
61+
}
4362

44-
// req := smsgateway.Webhook{
45-
// ID: c.String("id"),
46-
// URL: url,
47-
// Event: c.String("event"),
48-
// }
63+
b, err := renderer.Webhook(res)
64+
if err != nil {
65+
return cli.Exit(err.Error(), codes.OutputError)
66+
}
67+
fmt.Println(b)
4968

50-
return cli.Exit("Not implemented", codes.ParamsError)
69+
return nil
5170
},
5271
}

internal/core/output/json.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ func NewJSONOutput() *JSONOutput {
2525
func (o *JSONOutput) MessageState(src smsgateway.MessageState) (string, error) {
2626
return o.marshaler(src)
2727
}
28+
29+
func (o *JSONOutput) Webhook(src smsgateway.Webhook) (string, error) {
30+
return o.marshaler(src)
31+
}
32+
33+
func (o *JSONOutput) Webhooks(src []smsgateway.Webhook) (string, error) {
34+
return o.marshaler(src)
35+
}
36+
37+
func (o *JSONOutput) Success() (string, error) {
38+
return "", nil
39+
}
40+
41+
var _ Renderer = (*JSONOutput)(nil)

internal/core/output/output.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const (
1616

1717
type Renderer interface {
1818
MessageState(src smsgateway.MessageState) (string, error)
19+
Webhook(src smsgateway.Webhook) (string, error)
20+
Webhooks(src []smsgateway.Webhook) (string, error)
21+
Success() (string, error)
1922
}
2023

2124
var ErrUnsupportedFormat = errors.New("unsupported format")

0 commit comments

Comments
 (0)