-
Notifications
You must be signed in to change notification settings - Fork 1
/
kavenegar.go
66 lines (52 loc) · 1.61 KB
/
kavenegar.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
60
61
62
63
64
65
66
package sendo
import (
"bytes"
"context"
"text/template"
"github.com/kamva/tracer"
"github.com/kavenegar/kavenegar-go"
)
// kavenegarService implements the SMSService.
type kavenegarService struct {
client *kavenegar.Kavenegar
defaultSender string
tpl *template.Template
}
type KavenegarOptions struct {
Client *kavenegar.Kavenegar
DefaultSender string
Templates map[string]string
}
func NewKavenegarService(o KavenegarOptions) (SMSService, error) {
t, err := parseTextTemplates("kavenegar_root", o.Templates)
return &kavenegarService{
client: o.Client,
defaultSender: o.DefaultSender,
tpl: t,
}, tracer.Trace(err)
}
func (s kavenegarService) Send(_ context.Context, o SMSOptions) error {
msg, err := s.renderTemplate(o.TemplateName, o.Data)
if err != nil {
return tracer.Trace(err)
}
if o.Sender == "" {
o.Sender = s.defaultSender
}
_, err = s.client.Message.Send(o.Sender, []string{o.PhoneNumber}, msg, nil)
return tracer.Trace(err)
}
func (s *kavenegarService) renderTemplate(tplName string, data interface{}) (string, error) {
var buf bytes.Buffer
if err := s.tpl.ExecuteTemplate(&buf, tplName, data); err != nil {
return "", tracer.Trace(err)
}
return buf.String(), nil
}
// SendVerificationCode ignores the sender param in kavenegar driver.
func (s kavenegarService) SendVerificationCode(_ context.Context, o VerificationOptions) error {
lookupParam := extraOption[*kavenegar.VerifyLookupParam](o.Extra)
_, err := s.client.Verify.Lookup(o.PhoneNumber, o.TemplateName, o.Code, lookupParam)
return tracer.Trace(err)
}
var _ SMSService = &kavenegarService{}