2016-03-19 19:32:51 +00:00
|
|
|
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
|
|
|
|
|
2016-03-19 15:38:18 +00:00
|
|
|
// Package slack connects to slack service
|
2016-03-10 18:37:07 +00:00
|
|
|
package slack
|
2016-03-11 02:11:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-10-31 14:07:20 +00:00
|
|
|
"errors"
|
2016-03-11 02:11:52 +00:00
|
|
|
"fmt"
|
|
|
|
"html"
|
2017-04-01 13:14:29 +00:00
|
|
|
"io"
|
2016-03-11 02:11:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-05-18 02:16:00 +00:00
|
|
|
"regexp"
|
2016-04-21 15:19:38 +00:00
|
|
|
"strconv"
|
2016-03-11 02:11:52 +00:00
|
|
|
"strings"
|
2017-10-31 10:22:36 +00:00
|
|
|
// "sync/atomic"
|
2016-04-21 15:19:38 +00:00
|
|
|
"time"
|
2016-03-11 02:11:52 +00:00
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2016-03-11 02:11:52 +00:00
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Slack struct {
|
|
|
|
config *config.Config
|
|
|
|
|
|
|
|
url string
|
|
|
|
id string
|
|
|
|
ws *websocket.Conn
|
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
lastRecieved time.Time
|
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
users map[string]string
|
|
|
|
|
2017-11-17 15:16:10 +00:00
|
|
|
myBotID string
|
|
|
|
|
2017-07-24 19:09:27 +00:00
|
|
|
emoji map[string]string
|
|
|
|
|
2017-11-09 11:21:43 +00:00
|
|
|
eventReceived func(msg.Message)
|
|
|
|
messageReceived func(msg.Message)
|
2017-10-31 18:14:45 +00:00
|
|
|
replyMessageReceived func(msg.Message, string)
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var idCounter uint64
|
|
|
|
|
|
|
|
type slackUserInfoResp struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
User struct {
|
2017-01-05 14:48:57 +00:00
|
|
|
ID string `json:"id"`
|
2016-03-11 02:11:52 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
} `json:"user"`
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
type slackChannelListItem struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
IsChannel bool `json:"is_channel"`
|
|
|
|
Created int `json:"created"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
IsArchived bool `json:"is_archived"`
|
|
|
|
IsGeneral bool `json:"is_general"`
|
|
|
|
NameNormalized string `json:"name_normalized"`
|
|
|
|
IsShared bool `json:"is_shared"`
|
|
|
|
IsOrgShared bool `json:"is_org_shared"`
|
|
|
|
IsMember bool `json:"is_member"`
|
|
|
|
Members []string `json:"members"`
|
|
|
|
Topic struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
LastSet int `json:"last_set"`
|
|
|
|
} `json:"topic"`
|
|
|
|
Purpose struct {
|
|
|
|
Value string `json:"value"`
|
2016-04-21 15:19:38 +00:00
|
|
|
Creator string `json:"creator"`
|
2017-07-25 17:58:04 +00:00
|
|
|
LastSet int `json:"last_set"`
|
|
|
|
} `json:"purpose"`
|
|
|
|
PreviousNames []interface{} `json:"previous_names"`
|
|
|
|
NumMembers int `json:"num_members"`
|
|
|
|
}
|
2016-04-21 15:19:38 +00:00
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
type slackChannelListResp struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Channels []slackChannelListItem `json:"channels"`
|
|
|
|
}
|
2016-04-21 15:19:38 +00:00
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
type slackChannelInfoResp struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Channel struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
IsChannel bool `json:"is_channel"`
|
|
|
|
Created int `json:"created"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
IsArchived bool `json:"is_archived"`
|
|
|
|
IsGeneral bool `json:"is_general"`
|
|
|
|
NameNormalized string `json:"name_normalized"`
|
|
|
|
IsReadOnly bool `json:"is_read_only"`
|
|
|
|
IsShared bool `json:"is_shared"`
|
|
|
|
IsOrgShared bool `json:"is_org_shared"`
|
|
|
|
IsMember bool `json:"is_member"`
|
|
|
|
LastRead string `json:"last_read"`
|
|
|
|
Latest struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Ts string `json:"ts"`
|
|
|
|
} `json:"latest"`
|
|
|
|
UnreadCount int `json:"unread_count"`
|
|
|
|
UnreadCountDisplay int `json:"unread_count_display"`
|
|
|
|
Members []string `json:"members"`
|
|
|
|
Topic struct {
|
2016-04-21 15:19:38 +00:00
|
|
|
Value string `json:"value"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
LastSet int64 `json:"last_set"`
|
|
|
|
} `json:"topic"`
|
2017-07-25 17:58:04 +00:00
|
|
|
Purpose struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
LastSet int `json:"last_set"`
|
|
|
|
} `json:"purpose"`
|
|
|
|
PreviousNames []string `json:"previous_names"`
|
2016-04-21 15:19:38 +00:00
|
|
|
} `json:"channel"`
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
type slackMessage struct {
|
2017-07-25 14:56:30 +00:00
|
|
|
ID uint64 `json:"id"`
|
2017-01-05 14:48:57 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
SubType string `json:"subtype"`
|
2017-07-25 14:56:30 +00:00
|
|
|
Hidden bool `json:"hidden"`
|
2017-01-05 14:48:57 +00:00
|
|
|
Channel string `json:"channel"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Username string `json:"username"`
|
2017-11-11 16:51:50 +00:00
|
|
|
BotID string `json:"bot_id"`
|
2017-01-05 14:48:57 +00:00
|
|
|
Ts string `json:"ts"`
|
2017-11-09 11:21:43 +00:00
|
|
|
ThreadTs string `json:"thread_ts"`
|
2017-01-05 14:48:57 +00:00
|
|
|
Error struct {
|
2016-03-11 02:11:52 +00:00
|
|
|
Code uint64 `json:"code"`
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
} `json:"error"`
|
|
|
|
}
|
|
|
|
|
2017-06-17 20:20:06 +00:00
|
|
|
type slackReaction struct {
|
2017-07-25 14:56:30 +00:00
|
|
|
Reaction string `json:"name"`
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
Timestamp float64 `json:"timestamp"`
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
type rtmStart struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Error string `json:"error"`
|
2017-07-25 14:56:30 +00:00
|
|
|
URL string `json:"url"`
|
2016-03-11 02:11:52 +00:00
|
|
|
Self struct {
|
2017-07-25 14:56:30 +00:00
|
|
|
ID string `json:"id"`
|
2016-03-11 02:11:52 +00:00
|
|
|
} `json:"self"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(c *config.Config) *Slack {
|
|
|
|
return &Slack{
|
2017-07-25 17:58:04 +00:00
|
|
|
config: c,
|
|
|
|
lastRecieved: time.Now(),
|
|
|
|
users: make(map[string]string),
|
|
|
|
emoji: make(map[string]string),
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func checkReturnStatus(response *http.Response) bool {
|
|
|
|
type Response struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading Slack API body: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp Response
|
|
|
|
err = json.Unmarshal(body, &resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing message response: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return resp.OK
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
func (s *Slack) RegisterEventReceived(f func(msg.Message)) {
|
2016-03-11 02:11:52 +00:00
|
|
|
s.eventReceived = f
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
func (s *Slack) RegisterMessageReceived(f func(msg.Message)) {
|
2016-03-11 02:11:52 +00:00
|
|
|
s.messageReceived = f
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:14:45 +00:00
|
|
|
func (s *Slack) RegisterReplyMessageReceived(f func(msg.Message, string)) {
|
|
|
|
s.replyMessageReceived = f
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:07:20 +00:00
|
|
|
func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string, error) {
|
|
|
|
postUrl := "https://slack.com/api/chat.postMessage"
|
|
|
|
if meMessage {
|
|
|
|
postUrl = "https://slack.com/api/chat.meMessage"
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.PostForm(postUrl,
|
2017-10-31 10:22:36 +00:00
|
|
|
url.Values{"token": {s.config.Slack.Token},
|
2017-11-03 18:01:49 +00:00
|
|
|
"as_user": {"true"},
|
2017-10-31 13:40:03 +00:00
|
|
|
"channel": {channel},
|
|
|
|
"text": {message},
|
2017-10-31 10:22:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-31 14:07:20 +00:00
|
|
|
log.Printf("Error sending Slack message: %s", err)
|
2017-10-31 10:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading Slack API body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(string(body))
|
|
|
|
|
|
|
|
type MessageResponse struct {
|
2017-10-31 13:40:03 +00:00
|
|
|
OK bool `json:"ok"`
|
|
|
|
Timestamp string `json:"ts"`
|
2018-05-02 11:02:04 +00:00
|
|
|
Message struct {
|
|
|
|
BotID string `json:"bot_id"`
|
2017-11-17 15:30:15 +00:00
|
|
|
} `json:"message"`
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
2017-10-31 10:22:36 +00:00
|
|
|
|
|
|
|
var mr MessageResponse
|
|
|
|
err = json.Unmarshal(body, &mr)
|
2016-03-11 02:11:52 +00:00
|
|
|
if err != nil {
|
2017-10-31 10:22:36 +00:00
|
|
|
log.Fatalf("Error parsing message response: %s", err)
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
2017-10-31 10:22:36 +00:00
|
|
|
|
2017-10-31 14:07:20 +00:00
|
|
|
if !mr.OK {
|
|
|
|
return "", errors.New("failure response received")
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:30:15 +00:00
|
|
|
s.myBotID = mr.Message.BotID
|
2017-11-17 15:16:10 +00:00
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
return mr.Timestamp, err
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func (s *Slack) SendMessage(channel, message string) string {
|
2016-03-11 17:48:41 +00:00
|
|
|
log.Printf("Sending message to %s: %s", channel, message)
|
2017-10-31 14:07:20 +00:00
|
|
|
identifier, _ := s.SendMessageType(channel, message, false)
|
2017-10-31 13:40:03 +00:00
|
|
|
return identifier
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func (s *Slack) SendAction(channel, message string) string {
|
2016-03-11 17:48:41 +00:00
|
|
|
log.Printf("Sending action to %s: %s", channel, message)
|
2017-10-31 14:07:20 +00:00
|
|
|
identifier, _ := s.SendMessageType(channel, "_"+message+"_", true)
|
2017-10-31 13:40:03 +00:00
|
|
|
return identifier
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 14:16:41 +00:00
|
|
|
func (s *Slack) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) {
|
2017-10-31 14:07:20 +00:00
|
|
|
resp, err := http.PostForm("https://slack.com/api/chat.postMessage",
|
|
|
|
url.Values{"token": {s.config.Slack.Token},
|
2017-11-03 18:01:49 +00:00
|
|
|
"as_user": {"true"},
|
2017-10-31 14:07:20 +00:00
|
|
|
"channel": {channel},
|
|
|
|
"text": {message},
|
|
|
|
"thread_ts": {identifier},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error sending Slack reply: %s", err)
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading Slack API body: %s", err)
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(string(body))
|
|
|
|
|
|
|
|
type MessageResponse struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
Timestamp string `json:"ts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var mr MessageResponse
|
|
|
|
err = json.Unmarshal(body, &mr)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing message response: %s", err)
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mr.OK {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return mr.Timestamp, err == nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:16:41 +00:00
|
|
|
func (s *Slack) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) {
|
|
|
|
return s.ReplyToMessageIdentifier(channel, message, replyTo.AdditionalData["RAW_SLACK_TIMESTAMP"])
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func (s *Slack) React(channel, reaction string, message msg.Message) bool {
|
2017-06-17 20:20:06 +00:00
|
|
|
log.Printf("Reacting in %s: %s", channel, reaction)
|
|
|
|
resp, err := http.PostForm("https://slack.com/api/reactions.add",
|
2017-07-25 14:56:30 +00:00
|
|
|
url.Values{"token": {s.config.Slack.Token},
|
|
|
|
"name": {reaction},
|
|
|
|
"channel": {channel},
|
|
|
|
"timestamp": {message.AdditionalData["RAW_SLACK_TIMESTAMP"]}})
|
2017-06-17 20:20:06 +00:00
|
|
|
if err != nil {
|
2017-10-31 13:40:03 +00:00
|
|
|
log.Println("reaction failed: %s", err)
|
|
|
|
return false
|
2017-10-31 10:22:36 +00:00
|
|
|
}
|
2017-10-31 13:40:03 +00:00
|
|
|
return checkReturnStatus(resp)
|
2017-10-31 10:22:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func (s *Slack) Edit(channel, newMessage, identifier string) bool {
|
2017-10-31 10:22:36 +00:00
|
|
|
log.Printf("Editing in (%s) %s: %s", identifier, channel, newMessage)
|
|
|
|
resp, err := http.PostForm("https://slack.com/api/chat.update",
|
|
|
|
url.Values{"token": {s.config.Slack.Token},
|
2017-10-31 13:40:03 +00:00
|
|
|
"channel": {channel},
|
|
|
|
"text": {newMessage},
|
|
|
|
"ts": {identifier}})
|
2017-10-31 10:22:36 +00:00
|
|
|
if err != nil {
|
2017-10-31 13:40:03 +00:00
|
|
|
log.Println("edit failed: %s", err)
|
|
|
|
return false
|
2017-10-31 10:22:36 +00:00
|
|
|
}
|
2017-10-31 13:40:03 +00:00
|
|
|
return checkReturnStatus(resp)
|
2017-10-31 10:22:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 19:09:27 +00:00
|
|
|
func (s *Slack) GetEmojiList() map[string]string {
|
|
|
|
return s.emoji
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) populateEmojiList() {
|
|
|
|
resp, err := http.PostForm("https://slack.com/api/emoji.list",
|
2017-07-25 14:56:30 +00:00
|
|
|
url.Values{"token": {s.config.Slack.Token}})
|
2017-07-24 19:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error retrieving emoji list from Slack: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading Slack API body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmojiListResponse struct {
|
2017-07-25 14:56:30 +00:00
|
|
|
OK bool `json:ok`
|
2017-07-24 19:09:27 +00:00
|
|
|
Emoji map[string]string `json:emoji`
|
|
|
|
}
|
|
|
|
|
|
|
|
var list EmojiListResponse
|
|
|
|
err = json.Unmarshal(body, &list)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error parsing emoji list: %s", err)
|
|
|
|
}
|
|
|
|
s.emoji = list.Emoji
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
func (s *Slack) receiveMessage() (slackMessage, error) {
|
2017-01-05 14:48:57 +00:00
|
|
|
var msg []byte
|
2016-03-11 02:11:52 +00:00
|
|
|
m := slackMessage{}
|
2017-01-05 14:48:57 +00:00
|
|
|
err := websocket.Message.Receive(s.ws, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error decoding WS message")
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
err2 := json.Unmarshal(msg, &m)
|
|
|
|
return m, err2
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
// I think it's horseshit that I have to do this
|
|
|
|
func slackTStoTime(t string) time.Time {
|
|
|
|
ts := strings.Split(t, ".")
|
|
|
|
sec, _ := strconv.ParseInt(ts[0], 10, 64)
|
|
|
|
nsec, _ := strconv.ParseInt(ts[1], 10, 64)
|
|
|
|
return time.Unix(sec, nsec)
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:32:53 +00:00
|
|
|
func (s *Slack) Serve() error {
|
2016-03-11 02:11:52 +00:00
|
|
|
s.connect()
|
2017-07-24 19:09:27 +00:00
|
|
|
s.populateEmojiList()
|
2016-03-11 02:11:52 +00:00
|
|
|
for {
|
|
|
|
msg, err := s.receiveMessage()
|
2017-04-01 13:14:29 +00:00
|
|
|
if err != nil && err == io.EOF {
|
|
|
|
log.Fatalf("Slack API EOF")
|
|
|
|
} else if err != nil {
|
2017-09-07 04:32:53 +00:00
|
|
|
return fmt.Errorf("Slack API error: %s", err)
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
switch msg.Type {
|
|
|
|
case "message":
|
2017-11-17 15:30:15 +00:00
|
|
|
isItMe := msg.BotID != "" && msg.BotID == s.myBotID
|
2017-11-17 15:16:10 +00:00
|
|
|
if !isItMe && !msg.Hidden && msg.ThreadTs == "" {
|
2017-07-25 17:58:04 +00:00
|
|
|
m := s.buildMessage(msg)
|
|
|
|
if m.Time.Before(s.lastRecieved) {
|
|
|
|
log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time)
|
|
|
|
} else {
|
|
|
|
s.lastRecieved = m.Time
|
2017-10-31 18:14:45 +00:00
|
|
|
s.messageReceived(m)
|
2017-07-25 17:58:04 +00:00
|
|
|
}
|
2017-10-31 18:14:45 +00:00
|
|
|
} else if msg.ThreadTs != "" {
|
|
|
|
//we're throwing away some information here by not parsing the correct reply object type, but that's okay
|
|
|
|
s.replyMessageReceived(s.buildLightReplyMessage(msg), msg.ThreadTs)
|
2017-07-25 14:56:30 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("THAT MESSAGE WAS HIDDEN: %+v", msg.ID)
|
|
|
|
}
|
2016-03-11 02:11:52 +00:00
|
|
|
case "error":
|
|
|
|
log.Printf("Slack error, code: %d, message: %s", msg.Error.Code, msg.Error.Msg)
|
|
|
|
case "": // what even is this?
|
|
|
|
case "hello":
|
|
|
|
case "presence_change":
|
|
|
|
case "user_typing":
|
|
|
|
case "reconnect_url":
|
2017-07-25 17:58:04 +00:00
|
|
|
case "desktop_notification":
|
2016-03-11 02:11:52 +00:00
|
|
|
// squeltch this stuff
|
2017-07-25 17:58:04 +00:00
|
|
|
continue
|
2016-03-11 02:11:52 +00:00
|
|
|
default:
|
|
|
|
log.Printf("Unhandled Slack message type: '%s'", msg.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 02:16:00 +00:00
|
|
|
var urlDetector = regexp.MustCompile(`<(.+)://([^|^>]+).*>`)
|
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
// Convert a slackMessage to a msg.Message
|
|
|
|
func (s *Slack) buildMessage(m slackMessage) msg.Message {
|
|
|
|
text := html.UnescapeString(m.Text)
|
2016-03-11 02:11:52 +00:00
|
|
|
|
2017-10-24 19:45:02 +00:00
|
|
|
text = fixText(s.getUser, text)
|
2016-05-18 02:16:00 +00:00
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
isCmd, text := bot.IsCmd(s.config, text)
|
|
|
|
|
2017-09-29 04:58:21 +00:00
|
|
|
isAction := m.SubType == "me_message"
|
2016-03-11 02:11:52 +00:00
|
|
|
|
2017-10-24 19:45:02 +00:00
|
|
|
u, _ := s.getUser(m.User)
|
2017-10-31 18:14:45 +00:00
|
|
|
if m.Username != "" {
|
|
|
|
u = m.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
tstamp := slackTStoTime(m.Ts)
|
|
|
|
|
|
|
|
return msg.Message{
|
|
|
|
User: &user.User{
|
|
|
|
ID: m.User,
|
|
|
|
Name: u,
|
|
|
|
},
|
|
|
|
Body: text,
|
|
|
|
Raw: m.Text,
|
|
|
|
Channel: m.Channel,
|
|
|
|
Command: isCmd,
|
|
|
|
Action: isAction,
|
|
|
|
Host: string(m.ID),
|
|
|
|
Time: tstamp,
|
|
|
|
AdditionalData: map[string]string{
|
|
|
|
"RAW_SLACK_TIMESTAMP": m.Ts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) buildLightReplyMessage(m slackMessage) msg.Message {
|
|
|
|
text := html.UnescapeString(m.Text)
|
|
|
|
|
|
|
|
text = fixText(s.getUser, text)
|
|
|
|
|
|
|
|
isCmd, text := bot.IsCmd(s.config, text)
|
|
|
|
|
|
|
|
isAction := m.SubType == "me_message"
|
|
|
|
|
|
|
|
u, _ := s.getUser(m.User)
|
2017-01-05 15:04:44 +00:00
|
|
|
if m.Username != "" {
|
|
|
|
u = m.Username
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
tstamp := slackTStoTime(m.Ts)
|
2016-04-21 15:19:38 +00:00
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
return msg.Message{
|
|
|
|
User: &user.User{
|
2017-01-05 14:48:57 +00:00
|
|
|
ID: m.User,
|
2017-01-05 15:04:44 +00:00
|
|
|
Name: u,
|
2016-03-11 02:11:52 +00:00
|
|
|
},
|
|
|
|
Body: text,
|
2016-04-01 14:20:03 +00:00
|
|
|
Raw: m.Text,
|
|
|
|
Channel: m.Channel,
|
2016-03-11 02:11:52 +00:00
|
|
|
Command: isCmd,
|
|
|
|
Action: isAction,
|
2017-07-25 14:56:30 +00:00
|
|
|
Host: string(m.ID),
|
2016-04-21 15:19:38 +00:00
|
|
|
Time: tstamp,
|
2017-06-17 20:20:06 +00:00
|
|
|
AdditionalData: map[string]string{
|
|
|
|
"RAW_SLACK_TIMESTAMP": m.Ts,
|
|
|
|
},
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
// markAllChannelsRead gets a list of all channels and marks each as read
|
|
|
|
func (s *Slack) markAllChannelsRead() {
|
|
|
|
chs := s.getAllChannels()
|
|
|
|
log.Printf("Got list of channels to mark read: %+v", chs)
|
|
|
|
for _, ch := range chs {
|
|
|
|
s.markChannelAsRead(ch.ID)
|
|
|
|
}
|
|
|
|
log.Printf("Finished marking channels read")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllChannels returns info for all channels joined
|
|
|
|
func (s *Slack) getAllChannels() []slackChannelListItem {
|
|
|
|
u := s.url + "channels.list"
|
|
|
|
resp, err := http.PostForm(u,
|
|
|
|
url.Values{"token": {s.config.Slack.Token}})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error posting user info request: %s",
|
|
|
|
err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("Error posting user info request: %d",
|
|
|
|
resp.StatusCode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var chanInfo slackChannelListResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&chanInfo)
|
|
|
|
if err != nil || !chanInfo.Ok {
|
|
|
|
log.Println("Error decoding response: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return chanInfo.Channels
|
|
|
|
}
|
|
|
|
|
|
|
|
// markAsRead marks a channel read
|
|
|
|
func (s *Slack) markChannelAsRead(slackChanId string) error {
|
|
|
|
u := s.url + "channels.info"
|
|
|
|
resp, err := http.PostForm(u,
|
|
|
|
url.Values{"token": {s.config.Slack.Token}, "channel": {slackChanId}})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error posting user info request: %s",
|
|
|
|
err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("Error posting user info request: %d",
|
|
|
|
resp.StatusCode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var chanInfo slackChannelInfoResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&chanInfo)
|
|
|
|
log.Printf("%+v, %+v", err, chanInfo)
|
|
|
|
if err != nil || !chanInfo.Ok {
|
|
|
|
log.Println("Error decoding response: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u = s.url + "channels.mark"
|
|
|
|
resp, err = http.PostForm(u,
|
|
|
|
url.Values{"token": {s.config.Slack.Token}, "channel": {slackChanId}, "ts": {chanInfo.Channel.Latest.Ts}})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error posting user info request: %s",
|
|
|
|
err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("Error posting user info request: %d",
|
|
|
|
resp.StatusCode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var markInfo map[string]interface{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&markInfo)
|
|
|
|
log.Printf("%+v, %+v", err, markInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error decoding response: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Marked %s as read", slackChanId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:11:52 +00:00
|
|
|
func (s *Slack) connect() {
|
|
|
|
token := s.config.Slack.Token
|
2017-07-25 17:58:04 +00:00
|
|
|
url := fmt.Sprintf("https://slack.com/api/rtm.connect?token=%s", token)
|
2016-03-11 02:11:52 +00:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Fatalf("Slack API failed. Code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading Slack API body: %s", err)
|
|
|
|
}
|
|
|
|
var rtm rtmStart
|
|
|
|
err = json.Unmarshal(body, &rtm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rtm.Ok {
|
|
|
|
log.Fatalf("Slack error: %s", rtm.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.url = "https://slack.com/api/"
|
2017-07-25 14:56:30 +00:00
|
|
|
s.id = rtm.Self.ID
|
2016-03-11 02:11:52 +00:00
|
|
|
|
2017-07-25 17:58:04 +00:00
|
|
|
s.markAllChannelsRead()
|
|
|
|
|
2017-07-25 14:56:30 +00:00
|
|
|
s.ws, err = websocket.Dial(rtm.URL, "", s.url)
|
2016-03-11 02:11:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get username for Slack user ID
|
2017-10-24 19:45:02 +00:00
|
|
|
func (s *Slack) getUser(id string) (string, bool) {
|
2016-03-11 02:11:52 +00:00
|
|
|
if name, ok := s.users[id]; ok {
|
2017-10-24 19:45:02 +00:00
|
|
|
return name, true
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("User %s not already found, requesting info", id)
|
|
|
|
u := s.url + "users.info"
|
|
|
|
resp, err := http.PostForm(u,
|
|
|
|
url.Values{"token": {s.config.Slack.Token}, "user": {id}})
|
|
|
|
if err != nil || resp.StatusCode != 200 {
|
|
|
|
log.Printf("Error posting user info request: %d %s",
|
|
|
|
resp.StatusCode, err)
|
2017-10-24 19:45:02 +00:00
|
|
|
return "UNKNOWN", false
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
2016-04-21 15:19:38 +00:00
|
|
|
defer resp.Body.Close()
|
2016-03-11 02:11:52 +00:00
|
|
|
var userInfo slackUserInfoResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&userInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error decoding response: ", err)
|
2017-10-24 19:45:02 +00:00
|
|
|
return "UNKNOWN", false
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
s.users[id] = userInfo.User.Name
|
2017-10-24 19:45:02 +00:00
|
|
|
return s.users[id], true
|
2016-03-11 02:11:52 +00:00
|
|
|
}
|
2016-04-21 15:19:38 +00:00
|
|
|
|
|
|
|
// Who gets usernames out of a channel
|
|
|
|
func (s *Slack) Who(id string) []string {
|
|
|
|
log.Println("Who is queried for ", id)
|
|
|
|
u := s.url + "channels.info"
|
|
|
|
resp, err := http.PostForm(u,
|
|
|
|
url.Values{"token": {s.config.Slack.Token}, "channel": {id}})
|
2016-05-11 01:15:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error posting user info request: %s",
|
|
|
|
err)
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("Error posting user info request: %d",
|
|
|
|
resp.StatusCode)
|
2016-04-21 15:19:38 +00:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var chanInfo slackChannelInfoResp
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&chanInfo)
|
|
|
|
if err != nil || !chanInfo.Ok {
|
|
|
|
log.Println("Error decoding response: ", err)
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("%#v", chanInfo.Channel)
|
|
|
|
|
|
|
|
handles := []string{}
|
|
|
|
for _, member := range chanInfo.Channel.Members {
|
2017-10-24 19:45:02 +00:00
|
|
|
u, _ := s.getUser(member)
|
|
|
|
handles = append(handles, u)
|
2016-04-21 15:19:38 +00:00
|
|
|
}
|
|
|
|
log.Printf("Returning %d handles", len(handles))
|
|
|
|
return handles
|
|
|
|
}
|