This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
forked from bcicen/slackcat
/
slackecho.go
127 lines (116 loc) · 2.95 KB
/
slackecho.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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/bluele/slack"
)
type SlackEcho struct {
api *slack.Slack
opts *slack.ChatPostMessageOpt
queue *StreamQ
shutdown chan os.Signal
channelName string
channelId string
}
func newSlackEcho(token, channelName string) (*SlackEcho, error) {
se := &SlackEcho{
api: slack.New(token),
opts: &slack.ChatPostMessageOpt{AsUser: true},
queue: newStreamQ(),
shutdown: make(chan os.Signal, 1),
channelName: channelName,
}
err := se.lookupSlackId()
if err != nil {
return nil, err
}
signal.Notify(se.shutdown, os.Interrupt)
return se, nil
}
func (se *SlackEcho) trap() {
sigcount := 0
for sig := range se.shutdown {
if sigcount > 0 {
exitErr(fmt.Errorf("aborted"))
}
output(fmt.Sprintf("got signal: %s", sig.String()))
output("press ctrl+c again to exit immediately")
sigcount++
go se.exit()
}
}
func (se *SlackEcho) exit() {
for {
if se.queue.isEmpty() {
os.Exit(0)
} else {
output("flushing remaining messages to Slack...")
time.Sleep(3 * time.Second)
}
}
}
//Lookup Slack id for channel, group, or im
func (se *SlackEcho) lookupSlackId() error {
api := se.api
channel, err := api.FindChannelByName(se.channelName)
if err == nil {
se.channelId = channel.Id
return nil
}
group, err := api.FindGroupByName(se.channelName)
if err == nil {
se.channelId = group.Id
return nil
}
im, err := api.FindImByName(se.channelName)
if err == nil {
se.channelId = im.Id
return nil
}
fmt.Println(err)
return fmt.Errorf("No such channel, group, or im")
}
func (se *SlackEcho) addToStreamQ(lines chan string) {
for line := range lines {
se.queue.add(line)
}
se.exit()
}
//TODO: handle messages with length exceeding maximum for Slack chat
func (se *SlackEcho) processStreamQ(noop bool, pre bool) {
if !(se.queue.isEmpty()) {
msglines := se.queue.flush()
if noop {
output(fmt.Sprintf("skipped posting of %s message lines to %s", strconv.Itoa(len(msglines)), se.channelName))
} else {
se.postMsg(msglines, pre, "\n")
}
}
time.Sleep(3 * time.Second)
se.processStreamQ(noop, pre)
}
func (se *SlackEcho) postMsg(msglines []string, pre bool, sep string){
fmtStr := "%s"
if pre {
fmtStr = "```%s```"
}
msg := fmt.Sprintf(fmtStr, strings.Join(msglines, sep))
err := se.api.ChatPostMessage(se.channelId, msg, se.opts)
failOnError(err, "", true)
output(fmt.Sprintf("posted %s message lines to %s", strconv.Itoa(strings.Count(msg, "\n") + 1), se.channelName))
}
func (se *SlackEcho) postLines(lines chan string, noop bool, pre bool) {
msglines := []string{}
for line := range lines {
msglines = append(msglines, line)
}
if noop {
output(fmt.Sprintf("skipped posting of %s message lines to %s", strconv.Itoa(len(msglines)), se.channelName))
} else {
se.postMsg(msglines, pre, "\n")
}
}