-
Notifications
You must be signed in to change notification settings - Fork 42
/
send_message.go
56 lines (47 loc) · 1.48 KB
/
send_message.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
package command
import (
"fmt"
"github.com/innogames/slack-bot/bot"
"github.com/innogames/slack-bot/bot/matcher"
"github.com/innogames/slack-bot/bot/msg"
)
// NewSendMessageCommand is able to send a message to any user/channel
func NewSendMessageCommand(base bot.BaseCommand) bot.Command {
return &sendMessageCommand{base}
}
type sendMessageCommand struct {
bot.BaseCommand
}
func (c *sendMessageCommand) GetMatcher() matcher.Matcher {
return matcher.NewRegexpMatcher(`send message( to)? (?P<fullChannel><(?P<type>[#@])(?P<receiver>\w+)(?i:|[^>]*)?>) (?P<text>.*)`, c.SendMessage)
}
func (c *sendMessageCommand) SendMessage(match matcher.Result, message msg.Message) {
text := match.GetString("text")
if message.GetUser() != "" {
text = fmt.Sprintf("Text from <@%s>: %s", message.User, text)
}
if match.GetString("type") == "#" {
// send to channel
newEvent := msg.Message{}
newEvent.Channel = match.GetString("receiver")
c.SlackClient.SendMessage(newEvent, text)
} else {
c.SendToUser(match.GetString("receiver"), text)
}
c.SlackClient.SendMessage(
message,
fmt.Sprintf("I'll send `%s` to %s", match.GetString("text"), match.GetString("fullChannel")),
)
}
func (c *sendMessageCommand) GetHelp() []bot.Help {
return []bot.Help{
{
Command: "send message",
Description: "sends a message to given user/channel",
Examples: []string{
"send message #dev-backend PANIC MODE!!!",
"send message to @username please take a look in #general",
},
},
}
}