-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat.go
156 lines (132 loc) · 3.69 KB
/
at.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package at
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/SharkFourSix/grouter"
cmap "github.com/orcaman/concurrent-map"
)
const RouterName = "AfricasTalkingUSSDRouter"
var (
instance router
)
func init() {
grouter.RegisterRouter(RouterName, &instance)
}
type router struct {
}
type requestData struct {
Text string
SessionId string
ServiceCode string
PhoneNumber string
NetworkCode string
}
type binder func(r *requestData, value string)
var (
binders map[string]binder = map[string]binder{
"text": func(r *requestData, value string) { r.Text = value },
"sessionId": func(r *requestData, value string) { r.SessionId = value },
"serviceCode": func(r *requestData, value string) { r.ServiceCode = value },
"phoneNumber": func(r *requestData, value string) { r.PhoneNumber = value },
"networkCode": func(r *requestData, value string) { r.NetworkCode = value },
}
)
func (r *router) CreateRequest(resp *grouter.BufferedResponse, req *http.Request, store grouter.Storage) (grouter.UssdRequest, error) {
var (
request = new(requestData)
form url.Values
)
if err := req.ParseForm(); err != nil {
return nil, err
}
form = req.Form
bind := func(name string, b binder) error {
if form.Has(name) {
b(request, form.Get(name))
return nil
} else {
return fmt.Errorf("missing form field: `%s`", name)
}
}
for field, Binder := range binders {
if err := bind(field, Binder); err != nil {
return nil, err
}
}
sess := store.Get(request.SessionId)
if grouter.IsEmptyText(request.Text) {
// New session
sess = &africasTalkingUssdSession{
startTime: time.Now(),
readPointer: 0,
store: cmap.New(),
id: request.SessionId,
state: grouter.READ_OPTION,
autoAdjustReadPointer: false,
}
store.Set(request.SessionId, sess)
} else {
if sess == nil {
return nil, fmt.Errorf("session %s not found", request.SessionId)
} else {
sess.(*africasTalkingUssdSession).Read(request)
}
}
ussdRequest := ussd_request{
resp: resp,
data: request,
req: req,
attr: map[string]any{},
sess: sess.(*africasTalkingUssdSession),
}
return &ussdRequest, nil
}
type ussd_request struct {
resp *grouter.BufferedResponse
req *http.Request
data *requestData
sess *africasTalkingUssdSession
attr map[string]any
}
func (r *ussd_request) Session() grouter.UssdSession {
return r.sess
}
func (r *ussd_request) MSISDN() string {
return r.data.PhoneNumber
}
func (r *ussd_request) Option() string {
return r.sess.option
}
func (r *ussd_request) Input() string {
return r.sess.input
}
func (r *ussd_request) Continue(text string, args ...any) {
r.sess.state = grouter.READ_OPTION
_, _ = fmt.Fprintf(r.resp, "CON %s\n", fmt.Sprintf(text, args...))
}
func (r *ussd_request) ContinueWithTemplate(tmplName string, values grouter.TemplateValues) {
r.sess.state = grouter.READ_OPTION
r.resp.RenderContinueTemplate(tmplName, values)
}
func (r *ussd_request) Prompt(text string, args ...any) {
r.sess.state = grouter.READ_INPUT
_, _ = fmt.Fprintf(r.resp, "CON %s\n", fmt.Sprintf(text, args...))
}
func (r *ussd_request) PromptWithTemplate(tmplName string, values grouter.TemplateValues) {
r.sess.state = grouter.READ_INPUT
r.resp.RenderContinueTemplate(tmplName, values)
}
func (r *ussd_request) End(text string, args ...any) {
_, _ = fmt.Fprintf(r.resp, "END %s\n", fmt.Sprintf(text, args...))
}
func (r *ussd_request) EndWithTemplate(tmplName string, values grouter.TemplateValues) {
r.resp.RenderEndTemplate(tmplName, values)
}
func (r *ussd_request) SetAttribute(key string, value any) {
r.attr[key] = value
}
func (r *ussd_request) GetAttribute(key string) any {
return r.attr[key]
}