-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.go
78 lines (66 loc) · 1.63 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
package infrastructure
import (
"errors"
"fmt"
"os"
"strings"
"github.com/sirupsen/logrus"
"github.com/shintaro-uchiyama/slack-suite/functions/slack_event/domain"
"github.com/slack-go/slack"
)
var _ domain.SlackInterface = (*Slack)(nil)
type Slack struct {
client *slack.Client
}
func NewSlack(slackAccessToken string) *Slack {
return &Slack{
client: slack.New(slackAccessToken),
}
}
type SlackMessage struct {
Title string
Body string
}
func (s Slack) GetMessage(channel string, timestamp string) (domain.SlackMessage, error) {
conversationReplies, _, _, err := s.client.GetConversationReplies(
&slack.GetConversationRepliesParameters{
ChannelID: channel,
Inclusive: true,
Timestamp: timestamp,
},
)
if err != nil {
return domain.SlackMessage{}, fmt.Errorf("fetch conversation history error: %w", err)
}
if len(conversationReplies) == 0 {
return domain.SlackMessage{}, errors.New("message not found")
}
conversationReply := conversationReplies[0]
logrus.Info(conversationReply)
text := conversationReply.Text
title, body := text, text
index := strings.Index(text, "\n")
if index > -1 {
title = text[:index]
}
var linkUrl string
if conversationReply.ThreadTimestamp != "" {
linkUrl = fmt.Sprintf(
"%s/%s/p%s?thread_ts=%s&cid=%s",
os.Getenv("SLACK_URL"),
channel,
strings.Replace(timestamp, ".", "", -1),
conversationReply.ThreadTimestamp,
channel,
)
} else {
linkUrl = fmt.Sprintf(
"%s/%s/p%s",
os.Getenv("SLACK_URL"),
channel,
strings.Replace(timestamp, ".", "", -1),
)
}
body = fmt.Sprintf("%s \n %s", body, linkUrl)
return *domain.NewSlackMessage(title, body), nil
}