-
Notifications
You must be signed in to change notification settings - Fork 13
/
sinch_gateway.go
145 lines (123 loc) · 3.25 KB
/
sinch_gateway.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package sms
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"time"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/clock"
)
const (
javascriptISOString = `2006-01-02T15:04:05.999Z07:00`
)
type SinchGateway struct {
Clock clock.IClock
AppID string
AppSecret string
MsgURL string
FromNumber string
CallURL string
CallerNumber string
}
func (g *SinchGateway) SendOTPSMS(otp *OTP) (string, error) {
return g.SendSMSMessage(&Message{
Text: fmt.Sprintf(otp.Template, otp.OTP),
Number: otp.Phone.Number,
})
}
func (g *SinchGateway) SendOTPCallout(otp *OTP) (string, error) {
return g.SendCalloutMessage(&Message{
Text: fmt.Sprintf(otp.Template, otp.OTP),
Number: otp.Phone.Number,
})
}
func (g *SinchGateway) SendSMSMessage(message *Message) (string, error) {
body := struct {
From string `json:"from"`
Message string `json:"message"`
Caller string `json:"caller"`
}{
From: g.FromNumber,
Message: message.Text,
Caller: message.Number,
}
headers := g.getSinchHeaders()
responseBody, _, code, err := helper.Call(
context.Background(),
"POST",
g.MsgURL+"/"+message.Number,
headers,
time.Duration(timeoutInSeconds)*time.Second,
body,
nil)
if err != nil {
return failure, err
}
if code != http.StatusOK {
return failure, fmt.Errorf("expected status code OK, but got %v Response: %s", code, string(responseBody))
}
return success, nil
}
type ttsCallOut struct {
CLI string `json:"cli"`
Domain string `json:"domain"`
Locale string `json:"locale"`
Text string `json:"text"`
Destination *destination `json:"destination"`
}
type destination struct {
Type string `json:"type"`
Endpoint string `json:"endpoint"`
}
func (g *SinchGateway) SendCalloutMessage(message *Message) (string, error) {
body := struct {
Method string `json:"method"`
TTSCallOut *ttsCallOut `json:"ttsCallOut"`
}{
Method: "ttsCallout",
TTSCallOut: &ttsCallOut{
CLI: g.CallerNumber,
Domain: "pstn",
Locale: "en-US",
Text: message.Text + "...." + message.Text + "...." + message.Text,
Destination: &destination{
Type: "number",
Endpoint: message.Number,
},
},
}
headers := g.getSinchHeaders()
responseBody, _, code, err := helper.Call(
context.Background(),
g.CallURL,
"POST",
headers,
time.Duration(timeoutInSeconds)*time.Second,
body,
nil)
if err != nil {
return failure, err
}
if code != http.StatusOK {
return failure, fmt.Errorf("expected status code OK, but got %v Response: %s", code, string(responseBody))
}
return success, nil
}
func (g *SinchGateway) getSinchHeaders() map[string]string {
base64Credentials := base64.StdEncoding.EncodeToString([]byte(g.AppID + ":" + g.AppSecret))
return map[string]string{
"Content-Type": "application/json",
"Authorization": "Basic " + base64Credentials,
"X-Timestamp": g.Clock.Now().Format(javascriptISOString),
}
}
func (g *SinchGateway) SendVerificationSMS(_ *OTP) (string, error) {
panic("not implemented.")
}
func (g *SinchGateway) SendVerificationCallout(_ *OTP) (string, error) {
panic("not implemented.")
}
func (g *SinchGateway) VerifyCode(_ *OTP) (string, error) {
panic("not implemented.")
}