-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
146 lines (122 loc) · 3.67 KB
/
client.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
143
144
145
146
package url
import (
"context"
"errors"
"fmt"
"net/url"
"time"
seabird "github.com/seabird-chat/seabird-go"
"github.com/seabird-chat/seabird-go/pb"
)
type Client struct {
*seabird.Client
callbacks map[string][]URLCallback
messageCallbacks []MessageCallback
ignoredBackends map[string]bool
}
func NewClient(seabirdCoreUrl, seabirdCoreToken string, rawIgnoredBackends []string) (*Client, error) {
client, err := seabird.NewClient(seabirdCoreUrl, seabirdCoreToken)
if err != nil {
return nil, err
}
ignoredBackends := make(map[string]bool)
for _, backend := range rawIgnoredBackends {
ignoredBackends[backend] = true
}
return &Client{
Client: client,
callbacks: make(map[string][]URLCallback),
ignoredBackends: ignoredBackends,
}, nil
}
func (c *Client) Register(p Provider) {
for k, v := range p.GetCallbacks() {
c.callbacks[k] = append(c.callbacks[k], v)
}
if cb := p.GetMessageCallback(); cb != nil {
c.messageCallbacks = append(c.messageCallbacks, cb)
}
}
// TODO: currently Reply in seabird-go doesn't expose tags, so we copy all the
// Reply variants here and make sure to set the proper tags.
func (c *Client) Reply(source *pb.ChannelSource, msg string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := c.Inner.SendMessage(ctx, &pb.SendMessageRequest{
ChannelId: source.GetChannelId(),
Text: msg,
Tags: map[string]string{
"proxy/skip": "1",
"proxy/internal-tag": "1",
},
})
return err
}
func (c *Client) Replyf(source *pb.ChannelSource, format string, args ...interface{}) error {
return c.Reply(source, fmt.Sprintf(format, args...))
}
func (c *Client) MentionReply(source *pb.ChannelSource, msg string) error {
return c.Reply(source, fmt.Sprintf("%s: %s", source.GetUser().GetDisplayName(), msg))
}
func (c *Client) MentionReplyf(source *pb.ChannelSource, format string, args ...interface{}) error {
return c.MentionReply(source, fmt.Sprintf(format, args...))
}
func (c *Client) Run() error {
events, err := c.StreamEvents(map[string]*pb.CommandMetadata{
"isitdown": {
Name: "isitdown",
ShortHelp: "<website>",
FullHelp: "Checks if given website is down",
},
})
if err != nil {
return err
}
defer events.Close()
for event := range events.C {
// Skip any events we sent
if event.Tags["proxy/internal-tag"] == "1" {
continue
}
// Skip any events others asked to be skipped
if event.Tags["url/skip"] == "1" {
continue
}
switch v := event.GetInner().(type) {
case *pb.Event_Command:
if v.Command.Command == "isitdown" {
c.isItDownCallback(v.Command)
}
case *pb.Event_Message:
fmt.Printf("%+v\n", v)
id, err := url.Parse(v.Message.Source.ChannelId)
if err != nil {
fmt.Printf("failed to parse channel id %q: %s\n", v.Message.Source.ChannelId, err)
continue
}
if c.ignoredBackends[id.Scheme] {
fmt.Printf("message refers to ignored backend %s\n", id.Scheme)
continue
}
c.messageCallback(v.Message.Source, v.Message.Text)
case *pb.Event_SendMessage:
fmt.Printf("%+v\n", v)
id, err := url.Parse(v.SendMessage.ChannelId)
if err != nil {
fmt.Printf("failed to parse channel id %q: %s\n", v.SendMessage.ChannelId, err)
continue
}
if c.ignoredBackends[id.Scheme] {
fmt.Printf("message refers to ignored backend %s\n", id.Scheme)
continue
}
// We construct a bogus ChannelSource here to make the interface
// simpler. Thankfully, we only use .Reply/.Replyf so we only need
// the channelId here.
c.messageCallback(&pb.ChannelSource{
ChannelId: v.SendMessage.ChannelId,
}, v.SendMessage.Text)
}
}
return errors.New("event stream closed")
}