-
Notifications
You must be signed in to change notification settings - Fork 13
/
kavenegar_provider.go
59 lines (43 loc) · 1.16 KB
/
kavenegar_provider.go
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
package sms
import (
"errors"
"fmt"
"github.com/kavenegar/kavenegar-go"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
const Kavenegar = "kavenegar"
type KavenegarProvider struct {
APIKey string
Sender string
}
func NewKavenegarProvider(configService config.IConfig, _ clock.IClock) (IProvider, error) {
// register kavenegar
apiKey, ok := configService.String("sms.kavenegar.api_key")
if !ok {
return nil, errors.New("missing sms.kavenegar.api_key")
}
sender, ok := configService.String("sms.kavenegar.sender")
if !ok {
return nil, errors.New("missing sms.kavenegar.sender")
}
return &KavenegarProvider{
APIKey: apiKey,
Sender: sender,
}, nil
}
func (g *KavenegarProvider) GetName() string {
return Kavenegar
}
func (g *KavenegarProvider) SendSMSMessage(message *Message) (string, error) {
api := kavenegar.New(g.APIKey)
res, err := api.Message.Send(g.Sender, []string{message.Number}, message.Text, nil)
if err != nil {
return err.Error(), err
}
if len(res) < 1 {
e := fmt.Errorf("there was a problem sending sms")
return e.Error(), e
}
return res[0].StatusText, nil
}