-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
132 lines (111 loc) · 2.85 KB
/
proxy.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
package slack
import (
"fmt"
"github.com/botopolis/bot"
"github.com/nlopes/slack"
)
type proxy struct {
*Adapter
RTM *slack.RTM
formatter formatter
}
func newProxy(a *Adapter) *proxy {
return &proxy{
Adapter: a,
RTM: a.Client.NewRTM(),
formatter: formatter{a.Store},
}
}
func (p *proxy) onConnect(ev *slack.ConnectedEvent) {
p.Store.Load(ev.Info)
if err := p.Store.Update(); err != nil {
p.Adapter.Robot.Logger.Error("slack:", err)
}
p.BotID = ev.Info.User.ID
p.Name = ev.Info.User.Name
user, err := p.Client.GetUserInfo(p.BotID)
if err != nil {
p.Adapter.Robot.Logger.Error("slack: Unable to fetch bot user information.", err)
return
}
if user.Profile.RealName != "" {
p.Name = user.Profile.RealName
}
}
func (p *proxy) Send(m bot.Message) error {
if m.Params == nil {
p.RTM.SendMessage(p.RTM.NewOutgoingMessage(m.Text, m.Room))
return nil
}
if pm, ok := m.Params.(slack.PostMessageParameters); ok {
_, _, err := p.Client.PostMessage(m.Room, m.Text, pm)
return err
}
return nil
}
func (p *proxy) React(m bot.Message) error {
msg := m.Envelope.(slack.Message)
msgRef := slack.NewRefToMessage(msg.Channel, msg.Timestamp)
return p.RTM.AddReaction(m.Text, msgRef)
}
func (p *proxy) SetTopic(room, topic string) error {
_, err := p.Client.SetChannelTopic(room, topic)
return err
}
func (p *proxy) Connect() chan bot.Message {
go p.RTM.ManageConnection()
ch := make(chan bot.Message, 32)
go p.Forward(p.RTM.IncomingEvents, ch)
return ch
}
func (p *proxy) Disconnect() {
if p.RTM != nil {
p.RTM.Disconnect()
}
}
func (p *proxy) Forward(in <-chan slack.RTMEvent, out chan<- bot.Message) {
defer close(out)
for msg := range in {
switch ev := msg.Data.(type) {
case *slack.HelloEvent:
case *slack.ConnectedEvent:
p.onConnect(ev)
p.Robot.Logger.Debugf("slack: Connected as %s: %d", ev.Info.User.ID, ev.ConnectionCount)
case *slack.MessageEvent:
out <- p.translate(ev)
case *slack.RTMError:
p.Robot.Logger.Errorf("slack: RTM Error: %s", ev.Error())
case *slack.ConnectionErrorEvent:
p.Robot.Logger.Error("slack: Connection Error: %s", ev.Error())
case *slack.InvalidAuthEvent:
p.Robot.Logger.Error("slack: Invalid Credentials")
return
}
}
}
func (p *proxy) translate(ev *slack.MessageEvent) bot.Message {
user, _ := p.Store.UserByID(ev.User)
channel, _ := p.Store.ChannelByID(ev.Channel)
// Prepend the bots name whenever a direct message is parsed
if ev.Channel[0] == 'D' {
ev.Text = fmt.Sprintf("@%s %s", p.Name, ev.Text)
}
m := bot.Message{
User: user.Name,
Room: channel.Name,
Text: p.formatter.Format(ev),
Topic: ev.Topic,
Envelope: slack.Message(*ev),
}
switch ev.SubType {
case "channel_join":
m.Type = bot.Enter
case "channel_leave":
m.Type = bot.Leave
case "channel_topic":
m.Type = bot.Topic
default:
m.Type = bot.DefaultMessage
}
return m
}