-
Notifications
You must be signed in to change notification settings - Fork 14
/
twilio_gateway.go
127 lines (102 loc) · 2.58 KB
/
twilio_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
package sms
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/kevinburke/twilio-go"
)
type TwilioGateway struct {
SID string
Token string
FromNumber string
AuthyURL string
AuthyAPIKey string
}
func (g *TwilioGateway) SendOTPSMS(otp *OTP) (string, error) {
data := url.Values{}
data.Set("api_key", g.AuthyAPIKey)
data.Set("via", "sms")
data.Set("phone_number", otp.Number)
data.Set("country_code", otp.CC)
data.Set("custom_code", otp.OTP)
data.Set("locale", "en")
data.Set("code_length", "4")
baseURL, err := url.Parse(g.AuthyURL)
if err != nil {
return err.Error(), err
}
baseURL.RawQuery = data.Encode()
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": strconv.Itoa(len(data.Encode())),
}
_, _, code, err := helper.Call(
context.Background(),
"POST",
baseURL.String(),
headers,
time.Duration(timeoutInSeconds)*time.Second,
nil,
nil)
if err != nil {
return err.Error(), err
}
if code != http.StatusOK {
e := fmt.Errorf("expected status code OK, but got %v", code)
return e.Error(), e
}
// TODO: find out the format of response
return "success", nil
}
func (g *TwilioGateway) SendOTPCallout(otp *OTP) (string, error) {
data := url.Values{}
data.Set("api_key", g.AuthyAPIKey)
data.Set("via", "call")
data.Set("phone_number", otp.Number)
data.Set("country_code", otp.CC)
data.Set("custom_code", otp.OTP)
data.Set("locale", "en")
data.Set("code_length", "4")
baseURL, err := url.Parse(g.AuthyURL)
if err != nil {
return failure, err
}
baseURL.RawQuery = data.Encode()
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": strconv.Itoa(len(data.Encode())),
}
_, _, code, err := helper.Call(
context.Background(),
baseURL.String(),
"POST",
headers,
time.Duration(timeoutInSeconds)*time.Second,
nil,
nil)
if err != nil {
return failure, err
}
if code != http.StatusOK {
e := fmt.Errorf("expected status code OK, but got %v", code)
return e.Error(), e
}
// TODO: find out the format of response
return success, nil
}
func (g *TwilioGateway) SendSMSMessage(message *Message) (string, error) {
api := twilio.NewClient(g.SID, g.Token, nil)
msg, err := api.Messages.SendMessage(g.FromNumber, message.Number, message.Text, nil)
if err != nil {
return err.Error(), err
}
return msg.Status.Friendly(), nil
}
func (g *TwilioGateway) SendCalloutMessage(message *Message) (string, error) {
// not supported for now
return "", nil
}