-
Notifications
You must be signed in to change notification settings - Fork 57
/
util.go
66 lines (53 loc) · 1.64 KB
/
util.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
// SPDX-License-Identifier: Apache-2.0
package slack
import (
"fmt"
"regexp"
"strings"
"github.com/target/flottbot/models"
)
/*
=======================================================
Utility functions (does not use 'slack-go/slack' package)
=======================================================
*/
// findKey - find the key value in the map based on its value pair.
func findKey(m map[string]string, value string) (key string, ok bool) {
for k, v := range m {
if v == value {
key = k
ok = true
return
}
}
return
}
// getMessageType - gets the type of message based on where it came from.
func getMessageType(channel string) (models.MessageType, error) {
re := regexp.MustCompile(`^(C|D|G)[A-Z0-9]{4,}$`) // match known channel ID types
match := re.FindStringSubmatch(channel)
if len(match) > 0 {
switch match[1] { // [1] grabs the first letter, [0] will grab the entire channel ID
case "D":
return models.MsgTypeDirect, nil
case "C":
return models.MsgTypeChannel, nil
case "G":
return models.MsgTypePrivateChannel, nil
default:
return models.MsgTypeUnknown, fmt.Errorf("unable to handle channel: UNKNOWN_%s", channel)
}
}
return models.MsgTypeUnknown, fmt.Errorf("unable to handle channel: UNKNOWN_%s", channel)
}
// removeBotMention - parse out the prepended bot mention in a message.
func removeBotMention(contents, botID string) (string, bool) {
mention := fmt.Sprintf("<@%s> ", botID)
wasMentioned := false
if strings.HasPrefix(contents, mention) {
contents = strings.ReplaceAll(contents, mention, "")
contents = strings.TrimSpace(contents)
wasMentioned = true
}
return contents, wasMentioned
}