-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.go
80 lines (64 loc) · 2.12 KB
/
slack.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
package slacts
import (
"context"
"net/http"
"regexp"
"time"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
// SlackRequester is interface that wrap slack.Client only methods this package needs
type SlackRequester interface {
SearchMessagesContext(context.Context, string, slack.SearchParameters) (*slack.SearchMessages, error)
}
// SlackClient is api client for slack request
type SlackClient struct {
Client SlackRequester
}
// NewSlackClient returns SlackClient object.
// arg httpclient for using custom http.Client. So this can be nil.
//
// For example,
// NewSlackClient('YOUR_TOKEN', urlfetch.Client(ctx))
func NewSlackClient(token string, httpclient *http.Client) *SlackClient {
var opts []slack.Option
if httpclient != nil {
opts = []slack.Option{slack.OptionHTTPClient(httpclient)}
}
return &SlackClient{
Client: slack.New(token, opts...),
}
}
// CountQuery returns count of matches with query
func (sc *SlackClient) CountQuery(ctx context.Context, query *SlackQuery) (int, error) {
// Don't use zero value of `slack.SearchParameters`
// Slack API hasn't allowed changed empty parameter value since 08/30/2019
// This is just guest because Slack API has't announced about this
// https://api.slack.com/methods/search.messages
res, err := sc.Client.SearchMessagesContext(ctx, string(*query), slack.NewSearchParameters())
if err != nil {
return 0, errors.Errorf("failed to search message API request: %s", err)
}
return res.Total, nil
}
// SlackQuery for slack message search
type SlackQuery string
// TODO: enable to parse other format: for example 2018-01-01
var queryDateRegexp = regexp.MustCompile(`on:(\d{4}\/\d{1,2}\/\d{1,2})`)
// NewSlackQuery is initializer of Slack query
func NewSlackQuery(query string) *SlackQuery {
sq := SlackQuery(query)
return &sq
}
// Date returns date
func (q *SlackQuery) Date() (*time.Time, error) {
matches := queryDateRegexp.FindAllStringSubmatch(string(*q), -1)
if len(matches) == 0 || len(matches[0]) < 2 {
return nil, errors.New("did not find date")
}
date, err := time.Parse("2006/01/02", matches[0][1])
if err != nil {
return nil, err
}
return &date, nil
}