Fix channel names

This commit is contained in:
Ian Gulliver
2022-10-28 15:53:28 +00:00
parent b612e4f3cd
commit c4bb8a1a8d

View File

@@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strings"
)
type SlackClient struct {
@@ -43,12 +45,18 @@ type simpleResponse struct {
Error string `json:"error"`
}
type Purpose struct {
Value string `json:"value"`
}
type Channel struct {
Id string `json:"id"`
Name string `json:"name"`
IsChannel bool `json:"is_channel"`
IsGroup bool `json:"is_group"`
IsIm bool `json:"is_im"`
Id string `json:"id"`
Name string `json:"name"`
IsChannel bool `json:"is_channel"`
IsGroup bool `json:"is_group"`
IsIm bool `json:"is_im"`
IsMpIm bool `json:"is_mpim"`
Purpose *Purpose `json:"purpose"`
}
type Item struct {
@@ -222,11 +230,25 @@ func (sc *SlackClient) GetTitle(item *Item, user *User, channel *Channel) (strin
switch {
case channel.IsIm:
return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil
case channel.IsMpIm:
return fmt.Sprintf("[%s -> %s] %s", user.Name, sc.getTaggedNamesString(channel.Purpose.Value), item.Message.Text), nil
case channel.IsChannel:
return fmt.Sprintf("[%s -> #%s] %s", user.Name, channel.Name, item.Message.Text), nil
default:
return "", fmt.Errorf("unknown channel type: %#v", channel)
}
}
var taggedNamesRE = regexp.MustCompile(`@[a-zA-Z0-9]+[!a-zA-Z0-9]`)
func (sc *SlackClient) getTaggedNames(in string) []string {
return taggedNamesRE.FindAllString(in, -1)
}
func (sc *SlackClient) getTaggedNamesString(in string) string {
return fmt.Sprintf("{%s}", strings.Join(sc.getTaggedNames(in), ","))
}
func (sc *SlackClient) addAuth(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", sc.token))
}