Skip to content

Commit

Permalink
[webhooks] add sms:sent event
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Aug 9, 2024
1 parent db322fd commit 8a4d4f1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
27 changes: 10 additions & 17 deletions smsgateway/domain_webhooks.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package smsgateway

// The type of event a webhook can be triggered for.
type WebhookEvent string
import "github.com/android-sms-gateway/client-go/smsgateway/webhooks"

// Deprecated: use webhooks package instead.
type WebhookEvent = webhooks.EventType

const (
// Triggered when an SMS is received.
WebhookEventSmsReceived WebhookEvent = "sms:received"
// Triggered when the device pings the server.
WebhookEventSystemPing WebhookEvent = "system:ping"
// Deprecated: use webhooks package instead.
WebhookEventSmsReceived WebhookEvent = webhooks.EventTypeSmsReceived
// Deprecated: use webhooks package instead.
WebhookEventSystemPing WebhookEvent = webhooks.EventTypeSystemPing
)

// A webhook configuration.
type Webhook struct {
// The unique identifier of the webhook.
ID string `json:"id" validate:"max=36" example:"123e4567-e89b-12d3-a456-426614174000"`

// The URL the webhook will be sent to.
URL string `json:"url" validate:"required,http_url" example:"https://example.com/webhook"`

// The type of event the webhook is triggered for.
Event WebhookEvent `json:"event" validate:"required" example:"sms:received"`
}
// Deprecated: use webhook package instead.
type Webhook = webhooks.Webhook
39 changes: 39 additions & 0 deletions smsgateway/webhooks/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package webhooks

type EventType string

const (
// Triggered when an SMS is received.
EventTypeSmsReceived EventType = "sms:received"
// Triggered when an SMS is sent.
EventTypeSmsSent EventType = "sms:sent"
// Triggered when the device pings the server.
EventTypeSystemPing EventType = "system:ping"
)

var allEventTypes = map[EventType]struct{}{
EventTypeSmsReceived: {},
EventTypeSmsSent: {},
EventTypeSystemPing: {},
}

// IsValid checks if the given event type is valid.
//
// e is the event type to be checked.
// Returns true if the event type is valid, false otherwise.
func IsValidEventType(e EventType) bool {
_, ok := allEventTypes[e]
return ok
}

// A webhook configuration.
type Webhook struct {
// The unique identifier of the webhook.
ID string `json:"id" validate:"max=36" example:"123e4567-e89b-12d3-a456-426614174000"`

// The URL the webhook will be sent to.
URL string `json:"url" validate:"required,http_url" example:"https://example.com/webhook"`

// The type of event the webhook is triggered for.
Event EventType `json:"event" validate:"required" example:"sms:received"`
}
34 changes: 34 additions & 0 deletions smsgateway/webhooks/webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package webhooks_test

import (
"testing"

"github.com/android-sms-gateway/client-go/smsgateway/webhooks"
)

// TestIsValidEventType tests the IsValidEventType function.
func TestIsValidEventType(t *testing.T) {
tests := []struct {
name string
e webhooks.EventType
want bool
}{
{
name: "Valid event type",
e: webhooks.EventTypeSmsReceived,
want: true,
},
{
name: "Invalid event type",
e: "invalid:event",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := webhooks.IsValidEventType(tt.e); got != tt.want {
t.Errorf("IsValid() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 8a4d4f1

Please sign in to comment.