-
Notifications
You must be signed in to change notification settings - Fork 14
/
mobica_provider.go
136 lines (110 loc) · 3.02 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package sms
import (
"context"
"encoding/json"
"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 {
Username string
Password string
Route string
From string
Endpoint string
}
func NewMobicaProvider(configService config.IConfig, _ clock.IClock) (IProvider, error) {
username, ok := configService.String("sms.mobica.username")
if !ok {
return nil, errors.New("missing sms.mobica.username")
}
password, ok := configService.String("sms.mobica.password")
if !ok {
return nil, errors.New("missing sms.mobica.password")
}
route, _ := configService.String("sms.mobica.route")
from, _ := configService.String("sms.mobica.from")
endpoint, ok := configService.String("sms.mobica.endpoint")
if !ok {
return nil, errors.New("missing sms.mobica.endpoint")
}
return &MobicaProvider{
Username: username,
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 `json:"route,omitempty"`
SmartCut uint8 `json:"smartCut"`
Message string `json:"message"`
From string `json:"from,omitempty"`
}
type mobicaMsg struct {
Phone string `json:"phone"` //it supports comma separated phones
Sms sms `json:"sms"`
User string `json:"user"`
Pass string `json:"pass"`
}
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.Username,
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 {
bodyByte, _ := json.Marshal(body)
return failure, fmt.Errorf("expected status code OK, but got %v Response: %s \n Request: %s", code, string(responseBody), string(bodyByte))
}
responseBodyJSON := &struct {
Status int `json:"status"`
Desc string `json:"desc"`
}{}
err = json.Unmarshal(responseBody, responseBodyJSON)
if err != nil {
bodyByte, _ := json.Marshal(body)
return failure, fmt.Errorf("cannot unmarshal response Response: %s \n Request: %s", string(responseBody), string(bodyByte))
}
if responseBodyJSON.Status != 1004 {
bodyByte, _ := json.Marshal(body)
return failure, fmt.Errorf("unexpected status code Response: %s \n Request: %s", string(responseBody), string(bodyByte))
}
return success, nil
}
func (g *MobicaProvider) getHeaders() map[string]string {
return map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
}
}