forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
m3tech.go
94 lines (78 loc) · 2.63 KB
/
m3tech.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
package m3tech
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/gsm7"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
)
var sendURL = "https://secure.m3techservice.com/GenericServiceRestAPI/api/SendSMS"
var maxLength = 160
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("M3"), "M3Tech")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
receiveHandler := handlers.NewTelReceiveHandler(h.BaseHandler, "from", "text")
return s.AddHandlerRoute(h, http.MethodPost, "receive", receiveHandler)
}
// SendMsg sends the passed in message, returning any error
func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for M3 channel")
}
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for M3 channel")
}
// figure out if we need to send as unicode (encoding 7)
text := gsm7.ReplaceSubstitutions(handlers.GetTextAndAttachments(msg))
encoding := "0"
if !gsm7.IsValid(text) {
encoding = "7"
}
// send our message
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)
for _, part := range handlers.SplitMsg(text, maxLength) {
// build our request
params := url.Values{
"AuthKey": []string{"m3-Tech"},
"UserId": []string{username},
"Password": []string{password},
"SMS": []string{part},
"SMSType": []string{encoding},
"MobileNo": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"MsgId": []string{msg.ID().String()},
"MsgHeader": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"HandsetPort": []string{"0"},
"SMSChannel": []string{"0"},
"Telco": []string{"0"},
}
msgURL, _ := url.Parse(sendURL)
msgURL.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodGet, msgURL.String(), nil)
if err != nil {
return nil, err
}
rr, err := utils.MakeHTTPRequest(req)
status.AddLog(courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err))
if err != nil {
break
}
// all went well, set ourselves to wired
status.SetStatus(courier.MsgWired)
}
return status, nil
}