/
events.go
118 lines (103 loc) · 2.43 KB
/
events.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
package rtm
import (
"fmt"
)
type Event interface {
Type() string
}
type BasicEvent struct {
TypeStr string
RawEvent map[string]interface{}
}
func (e BasicEvent) Type() string {
return e.TypeStr
}
type Hello struct{}
func (h Hello) Type() string { return "hello" }
type Message interface {
Event
Subtype() string
Text() string
User() string
Channel() string
Timestamp() string
}
type BasicMessage struct {
channel string
user string
text string
timestamp string
subtype string
}
func (m BasicMessage) Type() string { return "message" }
func (m BasicMessage) Subtype() string { return m.subtype }
func (m BasicMessage) Text() string {
return m.text
}
func (m BasicMessage) User() string {
return m.user
}
func (m BasicMessage) Channel() string {
return m.channel
}
func (m BasicMessage) Timestamp() string {
return m.timestamp
}
type ChannelCreated struct {
ID string
Name string
Created int
Creator string
}
func (c ChannelCreated) Type() string { return "channel_created" }
type ChannelRename struct {
ID string
Name string
Created int
}
func (c ChannelRename) Type() string { return "channel_rename" }
func toEvent(rawEvent map[string]interface{}) Event {
fmt.Println(rawEvent)
rawEventType, ok := rawEvent["type"]
if !ok {
return BasicEvent{"invalid", rawEvent}
}
switch eventType := rawEventType.(string); eventType {
case "hello":
return Hello{}
case "message":
return BasicMessage{
channel: getStringField(rawEvent, "channel"),
user: getStringField(rawEvent, "user"),
text: getStringField(rawEvent, "text"),
timestamp: getStringField(rawEvent, "ts"),
subtype: getStringField(rawEvent, "subtype"),
}
case "channel_created":
channelInfo := rawEvent["channel"].(map[string]interface{})
return ChannelCreated{
ID: channelInfo["id"].(string),
Name: channelInfo["name"].(string),
Created: int(channelInfo["created"].(float64)),
Creator: channelInfo["creator"].(string),
}
case "channel_rename":
channelInfo := rawEvent["channel"].(map[string]interface{})
return ChannelRename{
ID: channelInfo["id"].(string),
Name: channelInfo["name"].(string),
Created: int(channelInfo["created"].(float64)),
}
default:
return BasicEvent{eventType, rawEvent}
}
}
func getStringField(m map[string]interface{}, key string) string {
val := m[key]
switch val.(type) {
case string:
return val.(string)
default:
return ""
}
}