/
main.go
142 lines (126 loc) · 3.14 KB
/
main.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
// ThingsDB module for communication with DutyCalls.
//
// For example:
//
// // Create the module (@thingsdb scope)
// new_module('dutycalls', 'github.com/thingsdb/module-go-dutycalls');
//
// // Configure the module
// set_module_conf('dutycalls', {
// login: 'mylogin',
// password: 'mysecret',
// });
//
// // Use the module
// dutycalls.new_ticket("mychannel", {
// title: "my title"
// }).then(|sid| {
// sid; // the sid of the new ticket
// });
//
package main
import (
"fmt"
"log"
"sync"
timod "github.com/thingsdb/go-timod"
"github.com/vmihailenco/msgpack"
)
var mux sync.Mutex
var cred authDutyCalls
// DefaultURI is the default api URI to use
// The UIR can be changed using:
//
// set_module_conf('dutycalls', {
// login: 'mylogin',
// password: 'mysecret',
// uri: 'https://playground.dutycalls.me/api'
// });
const DefaultURI = "https://dutycalls.me/api"
type authDutyCalls struct {
Login string `msgpack:"login"`
Password string `msgpack:"password"`
URI string `msgpack:"uri"`
}
type dcRequest struct {
Handler *string `msgpack:"handler"`
}
func handleConf(auth *authDutyCalls) {
cred = *auth
if cred.URI == "" {
cred.URI = DefaultURI
}
timod.WriteConfOk()
}
func onModuleReq(pkg *timod.Pkg) {
var req dcRequest
err := msgpack.Unmarshal(pkg.Data, &req)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Failed to unpack DutyCalls request")
return
}
if *req.Handler == "new-ticket" {
handleNewTicket(pkg)
} else if *req.Handler == "get-ticket" {
handleGetTicket(pkg)
} else if *req.Handler == "get-tickets" {
handleGetTickets(pkg)
} else if *req.Handler == "close-ticket" {
handleStatusTicket(pkg, "closed")
} else if *req.Handler == "close-tickets" {
handleStatusTickets(pkg, "closed")
} else if *req.Handler == "unack-ticket" {
handleStatusTicket(pkg, "unacknowledged")
} else if *req.Handler == "unack-tickets" {
handleStatusTickets(pkg, "unacknowledged")
} else if *req.Handler == "new-hit" {
handleNewHit(pkg)
} else if *req.Handler == "get-hits" {
handleGetHits(pkg)
} else if req.Handler == nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Missing handler")
} else {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Unknown handler: %s", *req.Handler))
}
}
func handler(buf *timod.Buffer, quit chan bool) {
for {
select {
case pkg := <-buf.PkgCh:
switch timod.Proto(pkg.Tp) {
case timod.ProtoModuleConf:
var auth authDutyCalls
err := msgpack.Unmarshal(pkg.Data, &auth)
if err == nil {
handleConf(&auth)
} else {
log.Println("Missing or invalid DutyCalls configuration")
timod.WriteConfErr()
}
case timod.ProtoModuleReq:
onModuleReq(pkg)
default:
log.Printf("Unexpected package type: %d", pkg.Tp)
}
case err := <-buf.ErrCh:
// In case of an error you probably want to quit the module.
// ThingsDB will try to restart the module a few times if this
// happens.
log.Printf("Error: %s", err)
quit <- true
}
}
}
func main() {
// Starts the module
timod.StartModule("dutycalls", handler)
}