mirror of
https://github.com/nikoksr/notify.git
synced 2025-03-19 21:08:48 +02:00
Merge branch 'main' into feat/lark
This commit is contained in:
commit
96096f17d6
@ -99,6 +99,7 @@ Yes, please! Contributions of all kinds are very welcome! Feel free to check our
|
|||||||
| [Syslog](https://wikipedia.org/wiki/Syslog) | [service/syslog](service/syslog) | [log/syslog](https://pkg.go.dev/log/syslog) |
|
| [Syslog](https://wikipedia.org/wiki/Syslog) | [service/syslog](service/syslog) | [log/syslog](https://pkg.go.dev/log/syslog) |
|
||||||
| [Telegram](https://telegram.org) | [service/telegram](service/telegram) | [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) |
|
| [Telegram](https://telegram.org) | [service/telegram](service/telegram) | [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) |
|
||||||
| [TextMagic](https://www.textmagic.com) | [service/textmagic](service/textmagic) | [textmagic/textmagic-rest-go-v2](https://github.com/textmagic/textmagic-rest-go-v2) |
|
| [TextMagic](https://www.textmagic.com) | [service/textmagic](service/textmagic) | [textmagic/textmagic-rest-go-v2](https://github.com/textmagic/textmagic-rest-go-v2) |
|
||||||
|
| [Twilio](https://www.twilio.com/) | [service/twilio](service/twilio) | [kevinburke/twilio-go](https://github.com/kevinburke/twilio-go) |
|
||||||
| [Twitter](https://twitter.com) | [service/twitter](service/twitter) | [dghubble/go-twitter](https://github.com/dghubble/go-twitter) |
|
| [Twitter](https://twitter.com) | [service/twitter](service/twitter) | [dghubble/go-twitter](https://github.com/dghubble/go-twitter) |
|
||||||
| [WeChat](https://www.wechat.com) | [service/wechat](service/wechat) | [silenceper/wechat](https://github.com/silenceper/wechat) |
|
| [WeChat](https://www.wechat.com) | [service/wechat](service/wechat) | [silenceper/wechat](https://github.com/silenceper/wechat) |
|
||||||
| [WhatsApp](https://www.whatsapp.com) | [service/whatsapp](service/whatsapp) | [Rhymen/go-whatsapp](https://github.com/Rhymen/go-whatsapp) |
|
| [WhatsApp](https://www.whatsapp.com) | [service/whatsapp](service/whatsapp) | [Rhymen/go-whatsapp](https://github.com/Rhymen/go-whatsapp) |
|
||||||
|
7
go.mod
7
go.mod
@ -37,6 +37,7 @@ require (
|
|||||||
github.com/appleboy/go-fcm v0.1.5
|
github.com/appleboy/go-fcm v0.1.5
|
||||||
github.com/go-lark/lark v1.7.2
|
github.com/go-lark/lark v1.7.2
|
||||||
github.com/google/go-cmp v0.5.8
|
github.com/google/go-cmp v0.5.8
|
||||||
|
github.com/kevinburke/twilio-go v0.0.0-20220615032439-b0fe9b151b0e
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -56,6 +57,7 @@ require (
|
|||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/fatih/structs v1.1.0 // indirect
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
github.com/go-redis/redis/v8 v8.11.6-0.20220405070650-99c79f7041fc // indirect
|
github.com/go-redis/redis/v8 v8.11.6-0.20220405070650-99c79f7041fc // indirect
|
||||||
|
github.com/gofrs/uuid v4.2.0+incompatible // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/google/go-querystring v1.1.0 // indirect
|
github.com/google/go-querystring v1.1.0 // indirect
|
||||||
github.com/google/uuid v1.3.0 // indirect
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
@ -63,6 +65,8 @@ require (
|
|||||||
github.com/gorilla/websocket v1.5.0 // indirect
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/kevinburke/go-types v0.0.0-20210723172823-2deba1f80ba7 // indirect
|
||||||
|
github.com/kevinburke/rest v0.0.0-20210506044642-5611499aa33c // indirect
|
||||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
@ -77,10 +81,13 @@ require (
|
|||||||
github.com/tidwall/gjson v1.14.1 // indirect
|
github.com/tidwall/gjson v1.14.1 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.0 // indirect
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
|
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
|
||||||
|
github.com/ttacon/libphonenumber v1.2.1 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
|
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
|
||||||
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b // indirect
|
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b // indirect
|
||||||
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect
|
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect
|
||||||
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 // indirect
|
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 // indirect
|
||||||
|
golang.org/x/text v0.3.7 // indirect
|
||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/protobuf v1.28.1 // indirect
|
google.golang.org/protobuf v1.28.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
12
go.sum
12
go.sum
@ -92,6 +92,8 @@ github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaEL
|
|||||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||||
|
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
|
||||||
|
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
|
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
|
||||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
@ -143,6 +145,12 @@ github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
|||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/kevinburke/go-types v0.0.0-20210723172823-2deba1f80ba7 h1:K8qael4LemsmJCGt+ccI8b0fCNFDttmEu3qtpFt3G0M=
|
||||||
|
github.com/kevinburke/go-types v0.0.0-20210723172823-2deba1f80ba7/go.mod h1:/Pk5i/SqYdYv1cie5wGwoZ4P6TpgMi+Yf58mtJSHdOw=
|
||||||
|
github.com/kevinburke/rest v0.0.0-20210506044642-5611499aa33c h1:hnbwWED5rIu+UaMkLR3JtnscMVGqp35lfzQwLuZAAUY=
|
||||||
|
github.com/kevinburke/rest v0.0.0-20210506044642-5611499aa33c/go.mod h1:pD+iEcdAGVXld5foVN4e24zb/6fnb60tgZPZ3P/3T/I=
|
||||||
|
github.com/kevinburke/twilio-go v0.0.0-20220615032439-b0fe9b151b0e h1:2HUamy+op/UxwJxDIg19oy/tIO/2M2tSasvihvhex4s=
|
||||||
|
github.com/kevinburke/twilio-go v0.0.0-20220615032439-b0fe9b151b0e/go.mod h1:PDdDH7RSKjjy9iFyoMzfeChOSmXpXuMEUqmAJSihxx4=
|
||||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/line/line-bot-sdk-go v7.8.0+incompatible h1:Uf9/OxV0zCVfqyvwZPH8CrdiHXXmMRa/L91G3btQblQ=
|
github.com/line/line-bot-sdk-go v7.8.0+incompatible h1:Uf9/OxV0zCVfqyvwZPH8CrdiHXXmMRa/L91G3btQblQ=
|
||||||
@ -227,6 +235,10 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
|||||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0=
|
||||||
|
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w=
|
||||||
|
github.com/ttacon/libphonenumber v1.2.1 h1:fzOfY5zUADkCkbIafAed11gL1sW+bJ26p6zWLBMElR4=
|
||||||
|
github.com/ttacon/libphonenumber v1.2.1/go.mod h1:E0TpmdVMq5dyVlQ7oenAkhsLu86OkUl+yR4OAxyEg/M=
|
||||||
github.com/utahta/go-linenotify v0.5.0 h1:E1tJaB/XhqRY/iz203FD0MaHm10DjQPOq5/Mem2A3Gs=
|
github.com/utahta/go-linenotify v0.5.0 h1:E1tJaB/XhqRY/iz203FD0MaHm10DjQPOq5/Mem2A3Gs=
|
||||||
github.com/utahta/go-linenotify v0.5.0/go.mod h1:KsvBXil2wx+ByaCR0e+IZKTbp4pDesc7yjzRigLf6pE=
|
github.com/utahta/go-linenotify v0.5.0/go.mod h1:KsvBXil2wx+ByaCR0e+IZKTbp4pDesc7yjzRigLf6pE=
|
||||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
43
service/twilio/README.md
Normal file
43
service/twilio/README.md
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# Twilio (Message Service)
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/github.com/nikoksr/notify/service/twilio)
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Navigate to Twilio [console](https://console.twilio.com/), create a new account or login with an existing one.
|
||||||
|
You will find the `Account SID` and the `Auth Token` under the `Account Info` tab. You may also request a Twilio phone number, if required.
|
||||||
|
|
||||||
|
To test the integration with a phone number you can just use the sample code below.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/nikoksr/notify"
|
||||||
|
"github.com/nikoksr/notify/service/twilio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
twilioSvc, err := twilio.New("account_sid", "auth_token", "your_phone_number")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("twilio.New() failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
twilioSvc.AddReceivers("recipient_phone_number")
|
||||||
|
|
||||||
|
notifier := notify.New()
|
||||||
|
notifier.UseServices(twilioSvc)
|
||||||
|
|
||||||
|
err = notifier.Send(context.Background(), "subject", "message")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("notifier.Send() failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("notification sent")
|
||||||
|
}
|
||||||
|
```
|
35
service/twilio/doc.go
Normal file
35
service/twilio/doc.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
Package twilio provides message notification integration for Twilio (Message Service).
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/nikoksr/notify"
|
||||||
|
"github.com/nikoksr/notify/service/twilio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
twilioSvc, err := twilio.New("account_sid", "auth_token", "your_phone_number")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("twilio.New() failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
twilioSvc.AddReceivers("recipient_phone_number")
|
||||||
|
|
||||||
|
notifier := notify.New()
|
||||||
|
notifier.UseServices(twilioSvc)
|
||||||
|
|
||||||
|
err = notifier.Send(context.Background(), "subject", "message")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("notifier.Send() failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("notification sent")
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package twilio
|
49
service/twilio/mock_twilioClient.go
Normal file
49
service/twilio/mock_twilioClient.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// Code generated by mockery v2.12.2. DO NOT EDIT.
|
||||||
|
|
||||||
|
package twilio
|
||||||
|
|
||||||
|
import (
|
||||||
|
url "net/url"
|
||||||
|
testing "testing"
|
||||||
|
|
||||||
|
twilio "github.com/kevinburke/twilio-go"
|
||||||
|
mock "github.com/stretchr/testify/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mockTwilioClient is an autogenerated mock type for the twilioClient type
|
||||||
|
type mockTwilioClient struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage provides a mock function with given fields: from, to, body, mediaURLs
|
||||||
|
func (_m *mockTwilioClient) SendMessage(from string, to string, body string, mediaURLs []*url.URL) (*twilio.Message, error) {
|
||||||
|
ret := _m.Called(from, to, body, mediaURLs)
|
||||||
|
|
||||||
|
var r0 *twilio.Message
|
||||||
|
if rf, ok := ret.Get(0).(func(string, string, string, []*url.URL) *twilio.Message); ok {
|
||||||
|
r0 = rf(from, to, body, mediaURLs)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*twilio.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(1).(func(string, string, string, []*url.URL) error); ok {
|
||||||
|
r1 = rf(from, to, body, mediaURLs)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// newMockTwilioClient creates a new instance of mockTwilioClient. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations.
|
||||||
|
func newMockTwilioClient(t testing.TB) *mockTwilioClient {
|
||||||
|
mock := &mockTwilioClient{}
|
||||||
|
mock.Mock.Test(t)
|
||||||
|
|
||||||
|
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||||
|
|
||||||
|
return mock
|
||||||
|
}
|
63
service/twilio/twilio.go
Normal file
63
service/twilio/twilio.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package twilio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/kevinburke/twilio-go"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Compile-time check that twilio.MessageService satisfies twilioClient interface.
|
||||||
|
var _ twilioClient = &twilio.MessageService{}
|
||||||
|
|
||||||
|
// twilioClient abstracts twilio-go MessageService for writing unit tests
|
||||||
|
type twilioClient interface {
|
||||||
|
SendMessage(from, to, body string, mediaURLs []*url.URL) (*twilio.Message, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service encapsulates the Twilio Message Service client along with internal state for storing recipient phone numbers.
|
||||||
|
type Service struct {
|
||||||
|
client twilioClient
|
||||||
|
|
||||||
|
fromPhoneNumber string
|
||||||
|
toPhoneNumbers []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new instance of Twilio notification service.
|
||||||
|
func New(accountSID, authToken, fromPhoneNumber string) (*Service, error) {
|
||||||
|
client := twilio.NewClient(accountSID, authToken, nil)
|
||||||
|
|
||||||
|
s := &Service{
|
||||||
|
client: client.Messages,
|
||||||
|
fromPhoneNumber: fromPhoneNumber,
|
||||||
|
toPhoneNumbers: []string{},
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddReceivers takes strings of recipient phone numbers and appends them to the internal phone numbers slice.
|
||||||
|
// The Send method will send a given message to all those phone numbers.
|
||||||
|
func (s *Service) AddReceivers(phoneNumbers ...string) {
|
||||||
|
s.toPhoneNumbers = append(s.toPhoneNumbers, phoneNumbers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send takes a message subject and a message body and sends them to all previously set phone numbers.
|
||||||
|
func (s *Service) Send(ctx context.Context, subject, message string) error {
|
||||||
|
body := subject + "\n" + message
|
||||||
|
|
||||||
|
for _, toPhoneNumber := range s.toPhoneNumbers {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
|
||||||
|
_, err := s.client.SendMessage(s.fromPhoneNumber, toPhoneNumber, body, []*url.URL{})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to send message to phone number '%s' using Twilio", toPhoneNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
90
service/twilio/twilio_test.go
Normal file
90
service/twilio/twilio_test.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package twilio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
testing "testing"
|
||||||
|
|
||||||
|
twilio "github.com/kevinburke/twilio-go"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAddReceivers(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := require.New(t)
|
||||||
|
|
||||||
|
svc := &Service{
|
||||||
|
toPhoneNumbers: []string{},
|
||||||
|
}
|
||||||
|
toPhoneNumbers := []string{"PhoneNumber1", "PhoneNumber2", "PhoneNumber3"}
|
||||||
|
svc.AddReceivers(toPhoneNumbers...)
|
||||||
|
|
||||||
|
assert.Equal(svc.toPhoneNumbers, toPhoneNumbers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSend(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := require.New(t)
|
||||||
|
|
||||||
|
svc := &Service{
|
||||||
|
fromPhoneNumber: "my_phone_number",
|
||||||
|
toPhoneNumbers: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
mockPhoneNumber := "recipient_phone_number"
|
||||||
|
mockBody := "subject\nmessage"
|
||||||
|
mockError := errors.New("some error")
|
||||||
|
|
||||||
|
// test twilio client send
|
||||||
|
mockClient := newMockTwilioClient(t)
|
||||||
|
mockClient.On("SendMessage",
|
||||||
|
svc.fromPhoneNumber,
|
||||||
|
mockPhoneNumber,
|
||||||
|
mockBody,
|
||||||
|
[]*url.URL{}).Return(&twilio.Message{Body: "a response message"}, nil)
|
||||||
|
svc.client = mockClient
|
||||||
|
svc.AddReceivers(mockPhoneNumber)
|
||||||
|
err := svc.Send(context.Background(), "subject", "message")
|
||||||
|
assert.Nil(err)
|
||||||
|
mockClient.AssertExpectations(t)
|
||||||
|
|
||||||
|
// test twilio client send returning error
|
||||||
|
mockClient = newMockTwilioClient(t)
|
||||||
|
mockClient.On("SendMessage",
|
||||||
|
svc.fromPhoneNumber,
|
||||||
|
mockPhoneNumber,
|
||||||
|
mockBody,
|
||||||
|
[]*url.URL{}).Return(nil, mockError)
|
||||||
|
svc.client = mockClient
|
||||||
|
svc.AddReceivers(mockPhoneNumber)
|
||||||
|
err = svc.Send(context.Background(), "subject", "message")
|
||||||
|
assert.NotNil(err)
|
||||||
|
assert.Equal(
|
||||||
|
fmt.Sprintf("failed to send message to phone number '%s' using Twilio: %s", mockPhoneNumber, mockError.Error()),
|
||||||
|
err.Error())
|
||||||
|
mockClient.AssertExpectations(t)
|
||||||
|
|
||||||
|
// test twilio client send multiple receivers
|
||||||
|
anotherMockPhoneNumber := "another_recipient_phone_number"
|
||||||
|
mockClient = newMockTwilioClient(t)
|
||||||
|
mockClient.On("SendMessage",
|
||||||
|
svc.fromPhoneNumber,
|
||||||
|
mockPhoneNumber,
|
||||||
|
mockBody,
|
||||||
|
[]*url.URL{}).Return(&twilio.Message{Body: "a response message"}, nil)
|
||||||
|
mockClient.On("SendMessage",
|
||||||
|
svc.fromPhoneNumber,
|
||||||
|
anotherMockPhoneNumber,
|
||||||
|
mockBody,
|
||||||
|
[]*url.URL{}).Return(&twilio.Message{Body: "a response message"}, nil)
|
||||||
|
svc.client = mockClient
|
||||||
|
svc.AddReceivers(mockPhoneNumber)
|
||||||
|
svc.AddReceivers(anotherMockPhoneNumber)
|
||||||
|
err = svc.Send(context.Background(), "subject", "message")
|
||||||
|
assert.Nil(err)
|
||||||
|
mockClient.AssertExpectations(t)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user