-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
114 lines (89 loc) · 3.26 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
// Copyright 2022 Crisp IM SAS All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/crisp-im/go-crisp-api/crisp/v3"
"fmt"
"io/ioutil"
"net/http"
)
const (
HOOKS_LISTENER = "localhost:8080"
HOOKS_PATH_EVENTS = "/hook/events"
)
const (
CONFIG_TOKEN_IDENTIFIER = "43f34724-9eeb-4474-9cec-560250754dec"
CONFIG_TOKEN_KEY = "d12e60c5d2aa264b90997a641b6474ffd6602b66d8e8abc49634c404f06fa7d0"
CONFIG_TOKEN_SIGNING_SECRET = "914a657d8cc2efbc20593f1806f9bde2"
)
func main() {
client := crisp.New()
client.AuthenticateTier("plugin", CONFIG_TOKEN_IDENTIFIER, CONFIG_TOKEN_KEY)
// Subscribe to realtime events (RTM events from Web Hooks)
client.Events.Listen(
crisp.EventsModeWebHooks,
[]string{
"message:send",
"message:received",
},
func(reg *crisp.EventsRegister) {
fmt.Print("Web Hooks channel bound: now listening for events\n")
reg.On("message:send/text", func(evt crisp.EventsReceiveTextMessage) {
fmt.Printf("[message:send/text] %s\n", evt)
})
reg.On("message:received/text", func(evt crisp.EventsReceiveTextMessage) {
fmt.Printf("[message:received/text] %s\n", evt)
})
},
func() {
fmt.Print("Web Hooks channel unbound: not listening anymore\n")
},
func(err error) {
fmt.Printf("Web Hooks channel error: %+v\n", err)
},
)
// Start Web Hooks HTTP server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "Web Hooks endpoint is running at: http://%s%s", HOOKS_LISTENER, HOOKS_PATH_EVENTS)
})
http.HandleFunc(HOOKS_PATH_EVENTS, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r.Body == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Read Web Hook data
hookTimestamp := r.Header.Get("X-Crisp-Request-Timestamp")
hookSignature := r.Header.Get("X-Crisp-Signature")
hookPayload, _ := ioutil.ReadAll(r.Body)
// Verify Web Hook payload
if client.Events.VerifyHook(CONFIG_TOKEN_SIGNING_SECRET, &hookPayload, hookTimestamp, hookSignature) != true {
fmt.Printf("Web Hooks request could not be verified with signature: %s\n", hookSignature)
w.WriteHeader(http.StatusForbidden)
return
}
// Receive Web Hook payload?
// Notice: receiver goes asynchronous internally, therefore there is no need to spawn a goroutine from there.
routed, err := client.Events.ReceiveHook(&hookPayload)
if err != nil {
// Failed processing Web Hook payload
fmt.Printf("Web Hooks payload processing error: %+v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("Web Hooks payload processed (routed=%t):\n \\-> %+v\n", routed, string(hookPayload))
// Web Hook payload received and processed
w.WriteHeader(http.StatusOK)
})
fmt.Printf("Web Hooks HTTP endpoint is: http://%s%s\n", HOOKS_LISTENER, HOOKS_PATH_EVENTS)
http.ListenAndServe(HOOKS_LISTENER, nil)
}