Skip to content

Commit

Permalink
implemented mattermost (#57)
Browse files Browse the repository at this point in the history
* Added mattermost as a messageprovider
  • Loading branch information
svenwiltink authored Jan 10, 2019
1 parent fe5e7f2 commit fb55948
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 49 deletions.
4 changes: 4 additions & 0 deletions cmd/go-musicbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"github.com/svenwiltink/go-musicbot/pkg/bot/messageprovider/mattermost"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -63,6 +64,9 @@ func chooseMessageProvider(config *bot.Config) bot.MessageProvider {
case "rocketchat":
log.Println("loading the rocketchat message provider")
return rocketchat.New(config)
case "mattermost":
log.Println("loading the mattermost message provider")
return mattermost.New(config)
default:
log.Fatalf("unsupported message plugin: %s", config.MessagePlugin)
}
Expand Down
12 changes: 9 additions & 3 deletions dist/config.json.example
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

{
"irc" : {
"irc": {
"server": "irc.freenode.net:6667",
"channel": "#banaan",
"nick": "iSlave",
"realname": "swiltink",
"pass": "",
"ssl": false
},
"rocketchat" : {
"rocketchat": {
"server": "chat.sveniltink.nl:443",
"channel": "#banaan",
"username": "musicbot",
"pass": "",
"ssl": true
},
"mattermost": {
"server": "chat.svenwiltink.nl",
"teamname": "svenwiltink",
"channel": "muziek",
"privateAccessToken": "<token>",
"ssl": true
},
"youtube": {
"apikey": "api key"
},
Expand Down
27 changes: 11 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
module github.com/svenwiltink/go-musicbot

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61
github.com/DexterLB/mpvipc v0.0.0-20180427115922-c65680788f71
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/vcs v1.12.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fluffle/goirc v0.0.0-20180216210456-fc1dfa1ceb88
github.com/golang/dep v0.5.0 // indirect
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1 // indirect
github.com/golang/mock v1.1.1 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/gorilla/websocket v1.3.0 // indirect
github.com/jmank88/nuts v0.3.0 // indirect
github.com/mitchellh/gox v0.4.0 // indirect
github.com/mitchellh/iochan v1.0.0 // indirect
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443 // indirect
github.com/gorilla/websocket v1.3.0
github.com/mattermost/mattermost-server v5.6.2+incompatible
github.com/nicksnyder/go-i18n v1.10.0 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353 // indirect
github.com/stretchr/testify v1.2.2
github.com/svenwiltink/ddpgo v0.0.0-20180824111114-cbfd6602d7b7 // indirect
github.com/svenwiltink/rocketchatgo v0.0.0-20180824111422-519bc8c4b617
github.com/svenwiltink/youtube-dl v0.0.0-20180530195755-08954d006d6a
github.com/vansante/go-event-emitter v0.0.0-20180601122726-d1e9ab59df60
golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1 // indirect
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1 // indirect
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/net v0.0.0-20180824045131-faa378e6dbae // indirect
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
golang.org/x/tools v0.0.0-20190108222858-421f03a57a64 // indirect
google.golang.org/api v0.0.0-20180824000442-943e5aafc110
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
53 changes: 23 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 h1:N5Vqww5QISEHsWHOWDEx4PzdIay3Cg0Jp7zItq2ZAro=
github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:GnKXcK+7DYNy/8w2Ex//Uql4IgfaU82Cd5rWKb7ah00=
github.com/DexterLB/mpvipc v0.0.0-20180427115922-c65680788f71 h1:duOThrR+sOMPDX15Xf4Z9lqrrCw1HKLwmuV1IRtvmu8=
github.com/DexterLB/mpvipc v0.0.0-20180427115922-c65680788f71/go.mod h1:nMVB54ifXmC1hpgfq7gTpotbv891pd2wAX/whuUj1q4=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/vcs v1.12.0 h1:bt9Hb4XlfmEfLnVA0MVz2NO0GFuMN5vX8iOWW38Xde4=
github.com/Masterminds/vcs v1.12.0/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 h1:o64h9XF42kVEUuhuer2ehqrlX8rZmvQSU0+Vpj1rF6Q=
github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:Rp8e0DCtEKwXFOC6JPJQVTz8tuGoGvw6Xfexggh/ed0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fluffle/goirc v0.0.0-20180216210456-fc1dfa1ceb88 h1:u4MUgj6o/rJ/KkV+v4gk5PQhX844bGzbCv2LY3mMXeI=
github.com/fluffle/goirc v0.0.0-20180216210456-fc1dfa1ceb88/go.mod h1:XSqSOq5nTMabnZQgLQMKdkleBiUntueKLNTb0PpSOZw=
github.com/golang/dep v0.5.0 h1:zlzYpxEIi3Y/q28hhayOCnSNsWyr3g6gl5I1nLP4cXM=
github.com/golang/dep v0.5.0/go.mod h1:6RZ2Wai7dSWk7qL55sDYk+8UPFqcW7all2KDBraPPFA=
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1 h1:6DVPu65tee05kY0/rciBQ47ue+AnuY8KTayV6VHikIo=
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.3.0 h1:r/LXc0VJIMd0rCMsc6DxgczaQtoCwCLatnfXmSYcXx8=
github.com/gorilla/websocket v1.3.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/jmank88/nuts v0.3.0 h1:UZUboV1LXVkBUTHLRTEZrDfAL7QYgj9jEsBCiJHrxEM=
github.com/jmank88/nuts v0.3.0/go.mod h1:kTf5cyoLibZUQg9Lns/gteKO1d/5XrhacD1QVKviAKk=
github.com/mitchellh/gox v0.4.0 h1:lfGJxY7ToLJQjHHwi0EX6uYBdK78egf954SQl13PQJc=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443 h1:+2OJrU8cmOstEoh0uQvYemRGVH1O6xtO2oANUWHFnP0=
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443/go.mod h1:JbxfV1Iifij2yhRjXai0oFrbpxszXHRx1E5RuM26o4Y=
github.com/mattermost/mattermost-server v5.6.2+incompatible h1:T0AcQeu4IhnyKL8/BZrHQZUjOWfaVWMVD2H4Gvllbig=
github.com/mattermost/mattermost-server v5.6.2+incompatible/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y=
github.com/nicksnyder/go-i18n v1.10.0 h1:5AzlPKvXBH4qBzmZ09Ua9Gipyruv6uApMcrNZdo96+Q=
github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353 h1:tnWWLf0nI2TI62Wd/ZOea4XYqE+y1sf2pdm+VItsc0c=
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353/go.mod h1:5HStXbIikwtDAgAIqiQIqVgMn7mlvZa6PTpwiAVYGYg=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/svenwiltink/ddpgo v0.0.0-20180824111114-cbfd6602d7b7 h1:2f5roDAVFMbJiQArGhO2A8mdOn56pGElJhqFRbeMFEE=
Expand All @@ -52,17 +40,22 @@ github.com/svenwiltink/youtube-dl v0.0.0-20180530195755-08954d006d6a h1:LF3iaylk
github.com/svenwiltink/youtube-dl v0.0.0-20180530195755-08954d006d6a/go.mod h1:DC+SfNAhNsFGkgpLCJvawoJOptQEofclz1adZWf1uT8=
github.com/vansante/go-event-emitter v0.0.0-20180601122726-d1e9ab59df60 h1:8EkIFZId9Zsvz9w66j0Bp4tawfj4Fp2XZP1/PLBrmyE=
github.com/vansante/go-event-emitter v0.0.0-20180601122726-d1e9ab59df60/go.mod h1:DC2i7ES4CtpdPHgm/BvbemeJKxKyAWSYpO24qdkqT/s=
golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1 h1:rJm0LuqUjoDhSk2zO9ISMSToQxGz7Os2jRiOL8AWu4c=
golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180824045131-faa378e6dbae h1:wghBFWo7bWmJJ1nmDDkVEIOBJBT/KMgVsM1iqi/csro=
golang.org/x/net v0.0.0-20180824045131-faa378e6dbae/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/tools v0.0.0-20190108222858-421f03a57a64 h1:9Y3iftuqayHi0EqSzJ3MrPoNIHHcIvicTPdfepyP5tE=
golang.org/x/tools v0.0.0-20190108222858-421f03a57a64/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/api v0.0.0-20180824000442-943e5aafc110 h1:F4z2BCxuYaett4vn5uqLd1ZDm00311hXZCRoRFjSK2U=
google.golang.org/api v0.0.0-20180824000442-943e5aafc110/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
9 changes: 9 additions & 0 deletions pkg/bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
Master string `json:"master"`
Irc IRCConfig `json:"irc"`
Rocketchat RocketchatConfig `json:"rocketchat"`
Mattermost MattermostConfig `json:"mattermost"`
Youtube YoutubeConfig `json:"youtube"`
MessagePlugin string `json:"messageplugin"`
CommandPrefix string `json:"commandprefix"`
Expand All @@ -42,6 +43,14 @@ type RocketchatConfig struct {
Ssl bool `json:"ssl"`
}

type MattermostConfig struct {
Server string `json:"server"`
Teamname string `json:"teamname"`
PrivateAccessToken string `json:"privateAccessToken"`
Channel string `json:"channel"`
Ssl bool `json:"ssl"`
}

type YoutubeConfig struct {
APIKey string `json:"apiKey"`
}
Expand Down
189 changes: 189 additions & 0 deletions pkg/bot/messageprovider/mattermost/mattermost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package mattermost

import (
"fmt"
"github.com/gorilla/websocket"
mattermost "github.com/mattermost/mattermost-server/model"
"github.com/svenwiltink/go-musicbot/pkg/bot"
"log"
"strings"
"time"
)

type MessageProvider struct {
Config *bot.Config
MessageChannel chan bot.Message

team *mattermost.Team
channel *mattermost.Channel

client *mattermost.Client4
websocketClient *mattermost.WebSocketClient
}

func (provider *MessageProvider) Start() error {

protocol := "http://"
if provider.Config.Mattermost.Ssl {
protocol = "https://"
}

provider.client = mattermost.NewAPIv4Client(protocol + provider.Config.Mattermost.Server)
provider.client.SetOAuthToken(provider.Config.Mattermost.PrivateAccessToken)

team, response := provider.client.GetTeamByName(provider.Config.Mattermost.Teamname, "")
if response.Error != nil {
return fmt.Errorf("unable to get team by name %s: %+v", provider.Config.Mattermost.Teamname, response.Error)
}

provider.team = team

channel, response := provider.client.GetChannelByName(provider.Config.Mattermost.Channel, team.Id, "")
if response.Error != nil {
return fmt.Errorf("unable to get channel by name %s: %+v", provider.Config.Mattermost.Teamname, response.Error)
}

provider.channel = channel

err := provider.connect()
if err != nil {
return err
}

go provider.pingLoop()
go provider.startReadLoop()

return nil
}

func (provider *MessageProvider) SendReplyToMessage(message bot.Message, reply string) error {
post := &mattermost.Post{
ChannelId: message.Target,
Message: reply,
}

_, response := provider.client.CreatePost(post)
if response.Error != nil {
log.Printf("unable to post message %+v: %+v", post, response)
return fmt.Errorf("unable to post message %+v: %+v", post, response)
}

return nil
}

func (provider *MessageProvider) BroadcastMessage(message string) error {
post := &mattermost.Post{
ChannelId: provider.channel.Id,
Message: message,
}

_, response := provider.client.CreatePost(post)
if response.Error != nil {
log.Printf("unable to post message %+v: %+v", post, response)
return fmt.Errorf("unable to post message %+v: %+v", post, response)
}

return nil
}

func (provider *MessageProvider) GetMessageChannel() chan bot.Message {
return provider.MessageChannel
}

func (provider *MessageProvider) connect() error {

protocol := "ws://"
if provider.Config.Mattermost.Ssl {
protocol = "wss://"
}

connection, appErr := mattermost.NewWebSocketClient(protocol+provider.Config.Mattermost.Server, provider.Config.Mattermost.PrivateAccessToken)
if appErr != nil {
return fmt.Errorf("unable to connect to mattermost websocker: %+v", appErr)
}

provider.websocketClient = connection

connection.Listen()

return nil
}

func (provider *MessageProvider) startReadLoop() {
log.Println("starting mattermost read loop")
for {
for event := range provider.websocketClient.EventChannel {
if event.Event == mattermost.WEBSOCKET_EVENT_POSTED {

postData, exists := event.Data["post"]
if !exists {
log.Printf("invalid postdata from event %+v", event)
continue
}

var jsonString string

switch t := postData.(type) {
case string:
jsonString = postData.(string)
default:
log.Printf("invalid data type %s for event %+v", t, event)
continue
}

post := mattermost.PostFromJson(strings.NewReader(jsonString))
provider.handleMessage(post)
}
}
log.Println("mattermost eventchannel closed. Probably disconnected D:")
}

}

func (provider *MessageProvider) handleMessage(post *mattermost.Post) {
channel, response := provider.client.GetChannel(post.ChannelId, "")
if response.Error != nil {
log.Printf("unable to get channel by id %s: %+v", post.ChannelId, response)
return
}

// ignore all messaged not from the channel or direct
if channel.Name != provider.Config.Mattermost.Channel && channel.Type != mattermost.CHANNEL_DIRECT {
log.Printf("ignoring message from channel %s", channel.Name)
}

author, response := provider.client.GetUser(post.UserId, "")
if response.Error != nil {
log.Printf("unable to get user by id %s: %+v", post.ChannelId, response)
return
}

msg := bot.Message{
Target: post.ChannelId,
Message: post.Message,
IsPrivate: channel.Type == mattermost.CHANNEL_DIRECT,
Sender: bot.Sender{
Name: author.Username,
NickName: author.Nickname,
},
}

provider.MessageChannel <- msg
}

func (provider *MessageProvider) pingLoop() {
ticker := time.NewTicker(10 * time.Second)
for range ticker.C {
err := provider.websocketClient.Conn.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
log.Printf("unable to ping: %+v", err)
}
}
}

func New(config *bot.Config) *MessageProvider {
return &MessageProvider{
MessageChannel: make(chan bot.Message),
Config: config,
}
}

0 comments on commit fb55948

Please sign in to comment.