Skip to content

Commit 57e0568

Browse files
committed
Revamp with latest driver, bot account support & more
- Use the latest release of mattermost-server and switch to newer Go APIs - Use bot accounts / token instead of user account - Cleaner and more structured code - Don't panic when connection closes unexpectedly (when mattermost server dies or bot get disconnected ) - No global variables - Use env vars or .env file for configuration - Level based logging - Do not wait for one message to be handled before moving to next - Example to reply in an existing thread
1 parent a498a79 commit 57e0568

File tree

9 files changed

+1245
-599
lines changed

9 files changed

+1245
-599
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ Session.vim
3232
.agignore
3333
.ctags
3434
tags
35+
36+
.env

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
This sample Bot shows how to use the Mattermost [Go driver](https://github.com/mattermost/mattermost-server/blob/master/model/client4.go) to interact with a Mattermost server, listen to events and respond to messages. Documentation for the Go driver can be found [here](https://godoc.org/github.com/mattermost/mattermost-server/model#Client).
5+
This sample Bot shows how to use the Mattermost [Go driver](https://github.com/mattermost/mattermost-server/blob/master/model/client4.go) to interact with a Mattermost server, listen to events and respond to messages. Documentation for the Go driver can be found [here](https://pkg.go.dev/github.com/mattermost/mattermost-server/v6/model).
66

77
Highlights of APIs used in this sample:
88
- Log in to the Mattermost server
@@ -11,7 +11,7 @@ Highlights of APIs used in this sample:
1111
- Connect and listen to WebSocket events for real-time responses to messages
1212
- Post a message to a channel
1313

14-
This Bot Sample was tested with Mattermost server version 3.10.0.
14+
This Bot Sample was tested with Mattermost server version 7.5.2.
1515

1616
## Setup Server Environment
1717

@@ -22,7 +22,10 @@ This Bot Sample was tested with Mattermost server version 3.10.0.
2222

2323
3 - Run `docker-compose up -d --build` and the mattermost client will be built and will expose the port `8065` to your system's localhost
2424

25-
4 - Start the Bot.
25+
4 - Get a bot token by logging into your mattermost instance, copy `example.env` to `.env` and fill in the credentials. Alternatively, just provide your credentials as environment variables.
26+
27+
5 - Start the Bot.
28+
2629
```
2730
make run
2831
```
@@ -73,21 +76,23 @@ Learn more about the `mmctl` CLI tool in the [Mattermost documentation](https://
7376
git clone https://github.com/mattermost/mattermost-bot-sample-golang.git
7477
cd mattermost-bot-sample-golang
7578
```
76-
3 - Start the Bot.
79+
3 - Get a bot token by logging into your mattermost instance, copy `example.env` to `.env` and fill in the credentials.
80+
81+
4 - Start the Bot.
7782
```
7883
make run
7984
```
8085
You can verify the Bot is running when
81-
- `Server detected and is running version X.Y.Z` appears on the command line.
82-
- `Mattermost Bot Sample has started running` is posted in the `Debugging For Sample Bot` channel.
86+
- `Logged in to mattermost` appears on the command line.
87+
- `Hi! I am a bot.` is posted in the your specified channel.
8388

8489
## Test the Bot
8590

8691
1 - Log in to the Mattermost server as `[email protected]` and `Password1!`.
8792

88-
2 - Join the `Debugging For Sample Bot` channel.
93+
2 - Join the your specified channel.
8994

90-
3 - Post a message in the channel such as `are you running?` to see if the Bot responds. You should see a response similar to `Yes I'm running` if the Bot is running.
95+
3 - Post a message in the channel such as `hello?` to see if the Bot responds. You should see a response if the Bot is running.
9196

9297
## Stop the Bot
9398

app.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"github.com/mattermost/mattermost-server/v6/model"
5+
"github.com/rs/zerolog"
6+
)
7+
8+
// application struct to hold the dependencies for our bot
9+
type application struct {
10+
config config
11+
logger zerolog.Logger
12+
mattermostClient *model.Client4
13+
mattermostWebSocketClient *model.WebSocketClient
14+
mattermostUser *model.User
15+
mattermostChannel *model.Channel
16+
mattermostTeam *model.Team
17+
}

bot_sample.go

Lines changed: 0 additions & 241 deletions
This file was deleted.

config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"net/url"
5+
"os"
6+
7+
_ "github.com/joho/godotenv/autoload"
8+
)
9+
10+
type config struct {
11+
mattermostUserName string
12+
mattermostTeamName string
13+
mattermostToken string
14+
mattermostChannel string
15+
mattermostServer *url.URL
16+
}
17+
18+
func loadConfig() config {
19+
var settings config
20+
21+
settings.mattermostTeamName = os.Getenv("MM_TEAM")
22+
settings.mattermostUserName = os.Getenv("MM_USERNAME")
23+
settings.mattermostToken = os.Getenv("MM_TOKEN")
24+
settings.mattermostChannel = os.Getenv("MM_CHANNEL")
25+
settings.mattermostServer, _ = url.Parse(os.Getenv("MM_SERVER"))
26+
27+
return settings
28+
}

example.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MM_TEAM="test-team"
2+
MM_TOKEN="XXXXXXXXXXXXXXX"
3+
MM_CHANNEL="town-square"
4+
MM_SERVER="http://localhost:8065"
5+
MM_USERNAME="yourbotuser"

go.mod

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,14 @@ module github.com/mattermost/mattermost-bot-sample-golang
33
go 1.15
44

55
require (
6-
github.com/NYTimes/gziphandler v1.1.1 // indirect
7-
github.com/go-gorp/gorp v2.0.0+incompatible // indirect
8-
github.com/google/uuid v1.3.0 // indirect
9-
github.com/gorilla/websocket v1.5.0 // indirect
6+
github.com/joho/godotenv v1.4.0
107
github.com/klauspost/compress v1.15.2 // indirect
11-
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
12-
github.com/mattermost/gorp v2.0.1-0.20190301154413-3b31e9a39d05+incompatible // indirect
138
github.com/mattermost/ldap v3.0.4+incompatible // indirect
14-
github.com/mattermost/mattermost-server/v5 v5.39.3
15-
github.com/mattermost/viper v1.0.4 // indirect
16-
github.com/minio/minio-go/v6 v6.0.40 // indirect
17-
github.com/minio/minio-go/v7 v7.0.24 // indirect
9+
github.com/mattermost/mattermost-server/v6 v6.7.2
1810
github.com/pelletier/go-toml v1.9.5 // indirect
19-
github.com/rs/xid v1.4.0 // indirect
20-
github.com/segmentio/analytics-go v3.1.0+incompatible // indirect
21-
github.com/spf13/afero v1.2.2 // indirect
22-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
23-
go.uber.org/atomic v1.9.0 // indirect
24-
go.uber.org/multierr v1.8.0 // indirect
25-
go.uber.org/zap v1.21.0 // indirect
11+
github.com/rs/zerolog v1.15.0
2612
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
2713
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
2814
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
2915
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
30-
gopkg.in/ini.v1 v1.66.4 // indirect
31-
gopkg.in/olivere/elastic.v5 v5.0.82 // indirect
3216
)

0 commit comments

Comments
 (0)