127 lines
2.7 KiB
Go
Raw Normal View History

2016-09-25 22:34:02 +02:00
package service
import (
"fmt"
"log"
2016-09-27 22:05:44 +02:00
"strconv"
"time"
2016-09-25 22:34:02 +02:00
"github.com/nlopes/slack"
)
type SlackService struct {
Client *slack.Client
RTM *slack.RTM
Channels []slack.Channel
}
type Channel struct {
ID string
Name string
}
func NewSlackService(token string) *SlackService {
svc := new(SlackService)
svc.Client = slack.New(token)
svc.RTM = svc.Client.NewRTM()
go svc.RTM.ManageConnection()
return svc
}
func (s *SlackService) Connect() {
}
func (s *SlackService) GetChannels() []Channel {
var chans []Channel
slackChans, err := s.Client.GetChannels(true)
if err != nil {
chans = append(chans, Channel{})
}
s.Channels = slackChans
for _, slackChan := range slackChans {
chans = append(chans, Channel{slackChan.ID, slackChan.Name})
}
return chans
}
2016-09-28 22:10:04 +02:00
func (s *SlackService) SendMessage(channel string, user string, message string) {
2016-09-27 22:05:44 +02:00
// https://godoc.org/github.com/nlopes/slack#PostMessageParameters
2016-09-28 22:10:04 +02:00
postParams := slack.PostMessageParameters{
Username: user,
}
2016-09-27 22:05:44 +02:00
// https://godoc.org/github.com/nlopes/slack#Client.PostMessage
s.Client.PostMessage(channel, message, postParams)
}
2016-09-25 22:34:02 +02:00
func (s *SlackService) GetMessages(channel string, count int) []string {
2016-09-25 22:34:02 +02:00
// https://api.slack.com/methods/channels.history
historyParams := slack.HistoryParameters{
Count: count,
2016-09-25 22:34:02 +02:00
Inclusive: false,
Unreads: false,
}
// https://godoc.org/github.com/nlopes/slack#History
history, err := s.Client.GetChannelHistory(channel, historyParams)
if err != nil {
log.Fatal(err)
return []string{""}
}
2016-09-27 22:05:44 +02:00
// Here we will construct the messages and format them with a username.
// Because we need to call the API again for an username because we only
// will get an user ID from a message, we will storage user ID's and names
// in a map.
2016-09-25 22:34:02 +02:00
var messages []string
2016-09-27 22:05:44 +02:00
users := make(map[string]string)
2016-09-25 22:34:02 +02:00
for _, message := range history.Messages {
var name string
2016-09-27 22:05:44 +02:00
name, ok := users[message.User]
if !ok {
user, err := s.Client.GetUserInfo(message.User)
if err == nil {
name = user.Name
users[message.User] = user.Name
} else {
2016-09-28 22:10:04 +02:00
name = message.Username
users[message.User] = name
2016-09-27 22:05:44 +02:00
}
2016-09-25 22:34:02 +02:00
}
2016-09-27 22:05:44 +02:00
// TODO: refactor this to CreateMessage
// Parse the time we get from slack which is a Unix time float
floatTime, err := strconv.ParseFloat(message.Timestamp, 64)
if err != nil {
floatTime = 0.0
}
intTime := int64(floatTime)
msg := fmt.Sprintf(
"[%s] <%s> %s",
time.Unix(intTime, 0).Format("15:04"),
name,
message.Text,
)
2016-09-25 22:34:02 +02:00
messages = append(messages, msg)
}
// Reverse the order of the messages, we want the newest in
// the last place
var messagesReversed []string
for i := len(messages) - 1; i >= 0; i-- {
messagesReversed = append(messagesReversed, messages[i])
}
return messagesReversed
2016-09-25 22:34:02 +02:00
}