forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_api.go
67 lines (56 loc) · 1.53 KB
/
event_api.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
package slack
import (
"encoding/json"
"reflect"
"io"
"fmt"
)
type APIEventManager struct {
}
func NewAPIEventManager() *APIEventManager {
return &APIEventManager{
}
}
func (eapi *APIEventManager) ReceiveIncomingEvent(data io.ReadCloser, apiEvent * ApiEvent) error{
rawEvent := json.RawMessage{}
err := json.NewDecoder(data).Decode(&rawEvent)
if err != nil {
err = fmt.Errorf("JSON marshalling error")
return err
} else if len(rawEvent) == 0 {
err = fmt.Errorf("Received empty event")
return err
}
event := &Event{}
err = json.Unmarshal(rawEvent, event)
if err != nil {
err = fmt.Errorf("JSON marshalling error")
return err
}
v, exists := eventAPIMapping[event.Type]
if !exists {
err = fmt.Errorf("EAPI Error, received unmapped event %q: %s\n", event.Type, string(rawEvent))
return err
}
t := reflect.TypeOf(v)
recvEvent := reflect.New(t).Interface()
err = json.Unmarshal(rawEvent, recvEvent)
if err != nil {
err = fmt.Errorf("EAPI Error, could not unmarshall event %q: %s\n", event.Type, string(rawEvent))
return err
}
apiEvent.Type = event.Type
apiEvent.Data = recvEvent
return nil
}
// Subset of events supported by the Slack Events API
var eventAPIMapping = map[string]interface{}{
"event_callback": EventCallbackEvent{},
"url_verification" : URLVerificationEvent{},
}
// URLVerificationEvent represents the slack event for verifying the url with a challenge
type URLVerificationEvent struct {
Type string `json:"type"`
Challenge string `json:"challenge"`
Token string `json:"token"`
}