-
Notifications
You must be signed in to change notification settings - Fork 13
/
kavenegar_gateway.go
73 lines (56 loc) · 1.68 KB
/
kavenegar_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
package sms
import (
"fmt"
"github.com/kavenegar/kavenegar-go"
)
type KavenegarGateway struct {
APIKey string
Sender string
}
func (g *KavenegarGateway) SendOTPSMS(otp *OTP) (string, error) {
api := kavenegar.New(g.APIKey)
res, err := api.Verify.Lookup(otp.Phone.Number, otp.Template, otp.OTP, nil)
if err != nil {
return err.Error(), err
}
if res.Status != kavenegar.Type_MessageStatus_Sent && res.Status != kavenegar.Type_MessageStatus_Delivered {
e := fmt.Errorf("there was a problem sending sms")
return e.Error(), e
}
return res.Message, nil
}
func (g *KavenegarGateway) SendOTPCallout(otp *OTP) (string, error) {
return g.SendCalloutMessage(&Message{
Text: fmt.Sprintf(otp.Template, otp.OTP),
Number: otp.Phone.Number,
})
}
func (g *KavenegarGateway) 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
}
func (g *KavenegarGateway) SendCalloutMessage(message *Message) (string, error) {
api := kavenegar.New(g.APIKey)
tts, err := api.Call.MakeTTS(message.Number, message.Text, &kavenegar.CallParam{})
if err != nil {
return err.Error(), err
}
return tts.StatusText, nil
}
func (g *KavenegarGateway) SendVerificationSMS(_ *OTP) (string, error) {
panic("not implemented.")
}
func (g *KavenegarGateway) SendVerificationCallout(_ *OTP) (string, error) {
panic("not implemented.")
}
func (g *KavenegarGateway) VerifyCode(_ *OTP) (string, error) {
panic("not implemented.")
}