forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
smscentral.go
114 lines (90 loc) · 3.09 KB
/
smscentral.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
package smscentral
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)
/*
POST /handlers/smscentral/receive/uuid/
mobile=9779811781111&message=Msg
*/
var sendURL = "http://smail.smscentral.com.np/bp/ApiSms.php"
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("SC"), "SMS Central")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
err := s.AddHandlerRoute(h, "POST", "receive", h.ReceiveMessage)
if err != nil {
return err
}
return nil
}
type smsCentralMessage struct {
Message string `name:"message"`
Mobile string `name:"mobile" validate:"required" `
}
// ReceiveMessage is our HTTP handler function for incoming messages
func (h *handler) ReceiveMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
smsCentralMessage := &smsCentralMessage{}
err := handlers.DecodeAndValidateForm(smsCentralMessage, r)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}
// create our URN
urn := urns.NewTelURNForCountry(smsCentralMessage.Mobile, channel.Country())
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, smsCentralMessage.Message)
// and finally queue our message
err = h.Backend().WriteMsg(ctx, msg)
if err != nil {
return nil, err
}
return []courier.Event{msg}, courier.WriteMsgSuccess(ctx, w, r, []courier.Msg{msg})
}
// 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 SC channel")
}
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for SC channel")
}
// build our request
form := url.Values{
"user": []string{username},
"pass": []string{password},
"mobile": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"content": []string{handlers.GetTextAndAttachments(msg)},
}
req, err := http.NewRequest(http.MethodPost, sendURL, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr, err := utils.MakeHTTPRequest(req)
// record our status and log
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)
status.AddLog(courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err))
if err != nil {
return status, nil
}
if rr.StatusCode/100 != 2 {
return status, errors.Errorf("Got non-200 response [%d] from API", rr.StatusCode)
}
status.SetStatus(courier.MsgWired)
return status, nil
}