-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathecho.go
49 lines (38 loc) · 1.51 KB
/
echo.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
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/bcneng/candebot/bot"
"github.com/bcneng/candebot/slackx"
)
type Echo struct {
Channel string `arg:"" required:"false"`
Message string `arg:"" required:"false"`
}
func (e *Echo) Run(ctx bot.Context, slackCtx bot.SlackContext) error {
if !ctx.IsStaff(slackCtx.User) && !ctx.CLI {
return errors.New("this action is only allowed to Staff members")
}
channel := strings.TrimPrefix(e.Channel, "#")
if channel == "" || e.Message == "" {
_ = slackx.SendEphemeral(ctx.Client, slackCtx.ThreadTimestamp, slackCtx.Channel, slackCtx.User, "Channel and message are required")
return errors.New("channel and message are required")
}
// Fixes the lack of support of multi word params.
if i := strings.Index(channel, " "); i > 0 {
e.Message = channel[i:] + " " + e.Message
channel = channel[0:i]
}
channelID, err := slackx.FindChannelIDByName(ctx.Client, channel)
if err != nil {
_ = slackx.SendEphemeral(ctx.Client, slackCtx.ThreadTimestamp, slackCtx.Channel, slackCtx.User, fmt.Sprintf("Error during channel lookup. Error: %s", err.Error()))
return err
}
if err := slackx.Send(ctx.Client, "", channelID, e.Message, false); err != nil {
_ = slackx.SendEphemeral(ctx.Client, slackCtx.ThreadTimestamp, slackCtx.Channel, slackCtx.User, fmt.Sprintf("Error sending message. Error: %s", err.Error()))
return err
}
_ = slackx.SendEphemeral(ctx.Client, slackCtx.ThreadTimestamp, slackCtx.Channel, slackCtx.User, "Message echoed successfully!")
return nil
}