catbase/slack/slack.go

294 lines
6.5 KiB
Go
Raw Normal View History

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"
"fmt"
"html"
"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"
"sync/atomic"
2016-04-21 15:19:38 +00:00
"time"
2016-03-11 02:11:52 +00:00
"github.com/velour/catbase/bot"
"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
users map[string]string
eventReceived func(msg.Message)
messageReceived func(msg.Message)
2016-03-11 02:11:52 +00:00
}
var idCounter uint64
type slackUserInfoResp struct {
Ok bool `json:"ok"`
User struct {
Id string `json:"id"`
Name string `json:"name"`
} `json:"user"`
}
2016-04-21 15:19:38 +00:00
type slackChannelInfoResp struct {
Ok bool `json:"ok"`
Channel struct {
Id string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
Creator string `json:"creator"`
Members []string `json:"members"`
Topic struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int64 `json:"last_set"`
} `json:"topic"`
} `json:"channel"`
}
2016-03-11 02:11:52 +00:00
type slackMessage struct {
Id uint64 `json:"id"`
Type string `json:"type"`
SubType string `json:"subtype"`
Channel string `json:"channel"`
Text string `json:"text"`
User string `json:"user"`
2016-04-21 15:19:38 +00:00
Ts string `json:"ts"`
2016-03-11 02:11:52 +00:00
Error struct {
Code uint64 `json:"code"`
Msg string `json:"msg"`
} `json:"error"`
}
type rtmStart struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Url string `json:"url"`
Self struct {
Id string `json:"id"`
} `json:"self"`
}
func New(c *config.Config) *Slack {
return &Slack{
config: c,
users: make(map[string]string),
}
}
func (s *Slack) RegisterEventReceived(f func(msg.Message)) {
2016-03-11 02:11:52 +00:00
s.eventReceived = f
}
func (s *Slack) RegisterMessageReceived(f func(msg.Message)) {
2016-03-11 02:11:52 +00:00
s.messageReceived = f
}
func (s *Slack) SendMessageType(channel, messageType, subType, message string) error {
m := slackMessage{
Id: atomic.AddUint64(&idCounter, 1),
Type: messageType,
SubType: subType,
Channel: channel,
Text: message,
}
err := websocket.JSON.Send(s.ws, m)
if err != nil {
log.Printf("Error sending Slack message: %s", err)
}
return err
}
func (s *Slack) SendMessage(channel, message string) {
2016-03-11 17:48:41 +00:00
log.Printf("Sending message to %s: %s", channel, message)
2016-03-11 02:11:52 +00:00
s.SendMessageType(channel, "message", "", message)
}
func (s *Slack) SendAction(channel, message string) {
2016-03-11 17:48:41 +00:00
log.Printf("Sending action to %s: %s", channel, message)
2016-03-11 02:11:52 +00:00
s.SendMessageType(channel, "message", "me_message", "_"+message+"_")
}
func (s *Slack) receiveMessage() (slackMessage, error) {
m := slackMessage{}
err := websocket.JSON.Receive(s.ws, &m)
return m, err
}
func (s *Slack) Serve() {
s.connect()
for {
msg, err := s.receiveMessage()
if err != nil {
2016-03-19 15:38:18 +00:00
log.Fatalf("Slack API error: %s", err)
2016-03-11 02:11:52 +00:00
}
switch msg.Type {
case "message":
s.messageReceived(s.buildMessage(msg))
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":
// squeltch this stuff
default:
log.Printf("Unhandled Slack message type: '%s'", msg.Type)
}
}
}
2016-05-18 02:16:00 +00:00
var urlDetector = regexp.MustCompile(`<(.+)://([^|^>]+).*>`)
// Convert a slackMessage to a msg.Message
func (s *Slack) buildMessage(m slackMessage) msg.Message {
log.Printf("DEBUG: msg: %#v", m)
text := html.UnescapeString(m.Text)
2016-03-11 02:11:52 +00:00
2016-05-18 02:16:00 +00:00
// remove <> from URLs, URLs may also be <url|description>
text = urlDetector.ReplaceAllString(text, "${1}://${2}")
2016-03-11 02:11:52 +00:00
isCmd, text := bot.IsCmd(s.config, text)
isAction := strings.HasPrefix(text, "/me ")
if isAction {
text = text[3:]
}
u := s.getUser(m.User)
2016-03-11 02:11:52 +00:00
2016-04-21 15:19:38 +00:00
// I think it's horseshit that I have to do this
ts := strings.Split(m.Ts, ".")
sec, _ := strconv.ParseInt(ts[0], 10, 64)
nsec, _ := strconv.ParseInt(ts[1], 10, 64)
tstamp := time.Unix(sec, nsec)
return msg.Message{
User: &user.User{
Name: u,
2016-03-11 02:11:52 +00:00
},
Body: text,
Raw: m.Text,
Channel: m.Channel,
2016-03-11 02:11:52 +00:00
Command: isCmd,
Action: isAction,
Host: string(m.Id),
2016-04-21 15:19:38 +00:00
Time: tstamp,
2016-03-11 02:11:52 +00:00
}
}
func (s *Slack) connect() {
token := s.config.Slack.Token
url := fmt.Sprintf("https://slack.com/api/rtm.start?token=%s", token)
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/"
s.id = rtm.Self.Id
s.ws, err = websocket.Dial(rtm.Url, "", s.url)
if err != nil {
log.Fatal(err)
}
}
// Get username for Slack user ID
func (s *Slack) getUser(id string) string {
if name, ok := s.users[id]; ok {
return name
}
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)
return "UNKNOWN"
}
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)
return "UNKNOWN"
}
s.users[id] = userInfo.User.Name
return s.users[id]
}
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}})
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 {
handles = append(handles, s.getUser(member))
}
log.Printf("Returning %d handles", len(handles))
return handles
}