-
Notifications
You must be signed in to change notification settings - Fork 13
/
mobica_provider.go
121 lines (101 loc) · 2.34 KB
/
mobica_provider.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
package sms
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
const Mobica = "mobica"
type MobicaProvider struct {
Email string
Password string
Route string
From string
Endpoint string
}
func NewMobicaProvider(configService config.IConfig, _ clock.IClock) (IProvider, error) {
email, ok := configService.String("sms.mobica.email")
if !ok {
return nil, errors.New("missing sms.mobica.email")
}
password, ok := configService.String("sms.mobica.password")
if !ok {
return nil, errors.New("missing sms.mobica.password")
}
route, ok := configService.String("sms.mobica.route")
if !ok {
return nil, errors.New("missing sms.mobica.route")
}
from, ok := configService.String("sms.mobica.from")
if !ok {
return nil, errors.New("missing sms.mobica.from")
}
endpoint, ok := configService.String("sms.mobica.endpoint")
if !ok {
return nil, errors.New("missing sms.mobica.endpoint")
}
return &MobicaProvider{
Email: email,
Password: password,
Route: route,
From: from,
Endpoint: endpoint,
}, nil
}
func NewMobicaProviderNoConfig(_ config.IConfig, _ clock.IClock) (IProvider, error) {
return &MobicaProvider{}, nil
}
func (g *MobicaProvider) GetName() string {
return Mobica
}
type sms struct {
route string
smartCut uint8
message string
from string
}
type mobicaMsg struct {
phone string
sms sms
user string
pass string
}
func (g *MobicaProvider) SendSMSMessage(message *Message) (string, error) {
body := &mobicaMsg{
phone: message.Number,
sms: sms{
route: g.Route,
smartCut: 1,
message: message.Text,
from: g.From,
},
user: g.Email,
pass: g.Password,
}
headers := g.getHeaders()
responseBody, _, code, err := helper.Call(
context.Background(),
"POST",
g.Endpoint,
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 *MobicaProvider) getHeaders() map[string]string {
return map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
}
}