-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms-logger.go
61 lines (51 loc) · 1.43 KB
/
sms-logger.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
package sendo
import (
"bytes"
"context"
"fmt"
"text/template"
"github.com/kamva/hexa/hlog"
"github.com/kamva/tracer"
)
type SMSLoggerOptions struct {
Templates map[string]string
}
// smsLogger implements the SMSService.
type smsLoggerService struct {
tpl *template.Template
}
// NewSMSLoggerService returns new instance of the sms logger.
func NewSMSLoggerService(o SMSLoggerOptions) (SMSService, error) {
t, err := parseTextTemplates("smsLogger_root", o.Templates)
return &smsLoggerService{
tpl: t,
}, tracer.Trace(err)
}
func (s smsLoggerService) Send(_ context.Context, o SMSOptions) error {
msg, err := s.renderTemplate(o.TemplateName, o.Data)
if err != nil {
return tracer.Trace(err)
}
hlog.Message("send sms",
hlog.String("to", o.PhoneNumber),
hlog.String("message", msg),
hlog.String("template_name", o.TemplateName),
hlog.String("data", fmt.Sprintf("%+v", o.Data)),
)
return nil
}
func (s *smsLoggerService) 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
}
func (s smsLoggerService) SendVerificationCode(_ context.Context, o VerificationOptions) error {
hlog.Message("send sms",
hlog.String("to", o.PhoneNumber),
hlog.String("code", o.Code),
hlog.String("template_name", o.TemplateName))
return nil
}
var _ SMSService = &smsLoggerService{}