-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringout.go
135 lines (124 loc) · 4.17 KB
/
ringout.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
package handlers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
hum "github.com/grokify/gotilla/net/httputilmore"
rc "github.com/grokify/go-ringcentral/client"
ru "github.com/grokify/go-ringcentral/clientutil"
)
// RingOutRequestParams represents the full list of request
// parameters that can be sent. All parameter names are lower-case.
// Supports both GET and POST.
type RingOutRequestParams struct {
Cmd string `schema:"cmd"`
Username string `schema:"username"`
Ext string `schema:"ext"`
Password string `schema:"password"`
To string `schema:"to"`
From string `schema:"from"`
Clid string `schema:"clid"`
Prompt string `schema:"prompt"`
SessionID string `schema:"sessionid"`
Format string `schema:"format"`
}
// HasValidCommand returns true if `cmd` is set to a supported value.
func (params *RingOutRequestParams) HasValidCommand() bool {
cmds := map[string]int{"call": 1, "list": 1, "status": 0, "cancel": 0}
if val, ok := cmds[strings.ToLower(params.Cmd)]; ok && val == 1 {
return true
}
return false
}
// PlayPrompt returns the prompt parameter converted to a boolean.
func (params *RingOutRequestParams) PlayPrompt() bool {
if params.Prompt == "1" {
return true
}
return false
}
func RingoutList(res http.ResponseWriter, apiClient *rc.APIClient, responseFormat string) {
fmt.Println("LIST")
info, resp, err := apiClient.CallHandlingSettingsApi.ListExtensionForwardingNumbers(
context.Background(), "~", "~", map[string]interface{}{})
if err != nil {
res.WriteHeader(http.StatusBadRequest)
eresp := hum.ResponseInfo{StatusCode: 500, Message: err.Error()}
res.Write(eresp.ToJson())
return
} else if resp.StatusCode >= 500 {
res.WriteHeader(http.StatusInternalServerError)
eresp := hum.ResponseInfo{StatusCode: resp.StatusCode, Message: fmt.Sprintf("REST API Response: %v", resp.StatusCode)}
res.Write(eresp.ToJson())
return
} else if resp.StatusCode >= 400 {
res.WriteHeader(http.StatusBadRequest)
eresp := hum.ResponseInfo{StatusCode: resp.StatusCode, Message: fmt.Sprintf("REST API Response: %v", resp.StatusCode)}
res.Write(eresp.ToJson())
return
} else if resp.StatusCode >= 300 {
res.WriteHeader(http.StatusInternalServerError)
eresp := hum.ResponseInfo{StatusCode: resp.StatusCode, Message: fmt.Sprintf("REST API Response: %v", resp.StatusCode)}
res.Write(eresp.ToJson())
return
}
bytes, err := json.Marshal(info)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
if responseFormat == "json" {
res.Header().Set(hum.HeaderContentType, hum.ContentTypeAppJsonUtf8)
res.Write(bytes)
} else {
res.Header().Set(hum.HeaderContentType, hum.ContentTypeTextPlainUsAscii)
res.Write([]byte(ringoutListLegacyResponseBody(info.Records)))
}
}
func ringoutListLegacyResponseBody(numberInfos []rc.ForwardingNumberInfo) string {
parts := []string{}
for _, numberInfo := range numberInfos {
label := strings.TrimSpace(numberInfo.Label)
number := strings.TrimSpace(numberInfo.PhoneNumber)
rx := regexp.MustCompile(`^\+1(\d{10})$`)
m := rx.FindStringSubmatch(number)
if len(m) > 1 {
number = m[1]
}
parts = append(parts, number)
parts = append(parts, label)
}
return fmt.Sprintf("OK %s", strings.Join(parts, ";"))
}
func RingoutCall(res http.ResponseWriter, apiClient *rc.APIClient, ringOut ru.RingOutRequest, responseFormat string) {
info, resp, err := apiClient.RingOutApi.MakeRingOutCallNew(
context.Background(), "~", "~", *ringOut.Body())
if err != nil {
res.WriteHeader(http.StatusBadRequest)
return
} else if resp.StatusCode >= 500 {
res.WriteHeader(http.StatusInternalServerError)
return
} else if resp.StatusCode >= 400 {
res.WriteHeader(http.StatusBadRequest)
return
} else if resp.StatusCode >= 300 {
res.WriteHeader(http.StatusInternalServerError)
return
}
bytes, err := json.Marshal(info)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
if responseFormat == "json" {
res.Header().Set(hum.HeaderContentType, hum.ContentTypeAppJsonUtf8)
res.Write(bytes)
} else {
res.Header().Set(hum.HeaderContentType, hum.ContentTypeTextPlainUsAscii)
res.Write([]byte(fmt.Sprintf("OK %s", info.Id)))
}
}