-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
73 lines (63 loc) · 1.68 KB
/
context.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
package ussd
import (
"encoding/json"
)
// Context of current USSD session.
type Context struct {
Request *Request
DataBag *DataBag
Data Data
FormData FormData
}
type FormData map[string]string
func (f FormData) Exists() bool {
return len(f) > 0
}
// Release USSD session.
func (c Context) Release(msg string) Response {
r := Response{}
r.Message = StrTrim(msg)
r.Release = true
return r
}
// Render to USSD client. Expects USSD client to respond with
// input which will be mapped to specified route.
func (c Context) Render(msg, ctrl, action string) Response {
r := Response{}
r.Message = StrTrim(msg)
r.route = route{StrTrim(ctrl), StrTrim(action)}
return r
}
// RenderMenu displays a menu and does appropriate routing to
// user's response.
func (c Context) RenderMenu(menu *Menu) Response {
b, err := json.Marshal(menu)
if err != nil {
return c.Err(err)
}
c.DataBag.Set(coredataMenu, string(b))
return c.Render(menu.render(), "core", "MenuProcessor")
}
// RenderForm starts the form input collection process.
func (c Context) RenderForm(form *Form, ctrl, action string) Response {
form.Route = route{StrTrim(ctrl), StrTrim(action)}
b, err := json.Marshal(form)
if err != nil {
return c.Err(err)
}
c.DataBag.Set(coredataForm, string(b))
return c.Redirect("core", "FormInputDisplay")
}
// Redirect to another route to continue processing request.
func (c Context) Redirect(ctrl, action string) Response {
r := Response{}
r.route = route{StrTrim(ctrl), StrTrim(action)}
r.redirect = true
return r
}
// Err releases USSD session with an error message.
func (c Context) Err(err error) Response {
r := c.Release("Sorry, something went wrong")
r.err = err
return r
}