Skip to content

Commit

Permalink
Merge pull request #43 from IceWreck/master
Browse files Browse the repository at this point in the history
Revamp with latest driver, bot account support & more
  • Loading branch information
crspeller authored Jun 5, 2023
2 parents a498a79 + 76ae035 commit a715fa2
Show file tree
Hide file tree
Showing 10 changed files with 1,289 additions and 632 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ Session.vim
.agignore
.ctags
tags

.env
94 changes: 56 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,111 @@

## Overview

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).
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).

Highlights of APIs used in this sample:
- Log in to the Mattermost server
- Create a channel
- Modify user attributes
- Connect and listen to WebSocket events for real-time responses to messages
- Post a message to a channel

This Bot Sample was tested with Mattermost server version 3.10.0.
- Log in to the Mattermost server
- Create a channel
- Modify user attributes
- Connect and listen to WebSocket events for real-time responses to messages
- Post a message to a channel

This Bot Sample was tested with Mattermost server version 7.5.2.

## Setup Server Environment

### Via Docker And Docker-Compose
1 - Ensure [Docker](https://www.docker.com/get-started) and [Docker-Compose](https://docs.docker.com/compose/install/) are installed for your system

2 - Run `./add_users.sh`. The login information for the Mattermost client will be printed
1 - Ensure [Docker](https://www.docker.com/get-started) and [Docker-Compose](https://docs.docker.com/compose/install/) are installed on your system.

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

3 - Run `./add_users.sh`. The login information for the Mattermost client will be printed.

4 - Log into your mattermost instance using these credentials and create a bot account following [these](https://developers.mattermost.com/integrate/reference/bot-accounts/) instructions.

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
5 - Copy `example.env` to `.env` and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

5 - Start the Bot.

4 - Start the Bot.
```
make run
```
You can verify the Bot is running when
- `Server detected and is running version X.Y.Z` appears on the command line.
- `Mattermost Bot Sample has started running` is posted in the `Debugging For Sample Bot` channel.

You can verify the Bot is running when

- `Logged in to mattermost` appears on the command line.
- `Hi! I am a bot.` is posted in your specified channel.

See "Test the Bot" for testing instructions

### Via Direct System Install/Setup
1 - [Install](http://docs.mattermost.com/install/requirements.html) or [upgrade](https://docs.mattermost.com/administration/upgrade.html) to Mattermost server version 3.10+, and verify that the Mattermost server is running on [http://localhost:8065](http://localhost:8065).

1 - [Install](http://docs.mattermost.com/install/requirements.html) or [upgrade](https://docs.mattermost.com/administration/upgrade.html) to Mattermost server version 3.10+, and verify that the Mattermost server is running on [http://localhost:8065](http://localhost:8065).

On the commands below, if you are running Mattermost server version 5.0 or later, use `./bin/mmctl`. If you are running version 4.10 or earlier, use `./bin/platform`.

Learn more about the `mmctl` CLI tool in the [Mattermost documentation](https://docs.mattermost.com/administration/mmctl-cli-tool.html).

2 - Create a team for the Bot to run. If you have an existing team, you may skip this step and replace `team_name` with your existing team in subsequent steps.
2 - Create a team for the Bot to run. If you have an existing team, you may skip this step and replace `botsample` with your existing team in subsequent steps.

```
./bin/mmctl team create --name botsample --display_name "Sample Bot playground" --email "[email protected]"
```
3 - Create the user account the Bot will run as.
```
./bin/mmctl user create --email="[email protected]" --password="Password1!" --username="samplebot"
```
4 - Create a second user, `bill`, which we will use to log in and interact with the Bot.

3 - Create a user, `bill`, which we will use to log in and interact with the Bot.

```
./bin/mmctl user create --email="[email protected]" --password="Password1!" --username="bill"
```
5 - (Optional) Give `bill` `system_admin` permissions.

4 - (Optional) Give `bill` `system_admin` permissions.

```
./bin/mmctl roles system_admin bill
```
6 - Add users to the team
```
./bin/mmctl team add botsample samplebot bill
```
7 - Verify the e-mail address
```
./bin/mmctl user verify samplebot
```
8 - Log in to [http://localhost:8065](http://localhost:8065) as `bill` and verify the account was created successfully. Then, navigate to the `botsample` team you created in step 2 to interact with the Bot.

5 - Log in to [http://localhost:8065](http://localhost:8065) as `bill` and verify the account was created successfully.

6 - Create a bot account following [these](https://developers.mattermost.com/integrate/reference/bot-accounts/) instructions.

7 - Copy `example.env` to `.env` and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

## Setup Bot Development Environment

1 - Follow the [Developer Machine Setup](https://docs.mattermost.com/developer/dev-setup.html) instructions to setup the bot development environment.

2 - Clone the GitHub repository to run the sample.

```
git clone https://github.com/mattermost/mattermost-bot-sample-golang.git
cd mattermost-bot-sample-golang
```
3 - Start the Bot.

3 - Log into your mattermost instance using these credentials and create a bot account following [these](https://developers.mattermost.com/integrate/reference/bot-accounts/) instructions.

4 - Copy `example.env` to `.env` and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

5 - Run with

```
make run
```
You can verify the Bot is running when
- `Server detected and is running version X.Y.Z` appears on the command line.
- `Mattermost Bot Sample has started running` is posted in the `Debugging For Sample Bot` channel.

You can verify the Bot is running when

- `Logged in to mattermost` appears on the command line.
- `Hi! I am a bot.` is posted in your specified channel.

## Test the Bot

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

2 - Join the `Debugging For Sample Bot` channel.
2 - Join your specified channel.

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.
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.

## Stop the Bot

1 - In the terminal window, press `CTRL+C` to stop the bot. You should see `Mattermost Bot Sample has stopped running` posted in the `Debugging For Sample Bot` channel.
1 - In the terminal window, press `CTRL+C` to stop the bot.
4 changes: 1 addition & 3 deletions add_users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ USER_PASS="Password1!"
USER_NAME="bill"
docker exec test_mm bash -c 'until mmctl --local system status 2> /dev/null; do echo "waiting for server to become available"; sleep 5; done'
docker exec test_mm mmctl --local team create --name botsample display_name "Sample Bot playground" --email "[email protected]"
docker exec test_mm mmctl --local user create --email="[email protected]" --password="Password1!" --username="samplebot"
docker exec test_mm mmctl --local user create --email="${USER_EMAIL}" --password="${USER_PASS}" --username="${USER_NAME}"
docker exec test_mm mmctl --local roles system_admin bill
docker exec test_mm mmctl --local team users add botsample samplebot bill
docker exec test_mm mmctl --local user verify samplebot
docker exec test_mm mmctl --local team users add botsample bill

echo "Default user credentials"
echo "Email: ${USER_EMAIL} - Password: ${USER_PASS}"
17 changes: 17 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"github.com/mattermost/mattermost-server/v6/model"
"github.com/rs/zerolog"
)

// application struct to hold the dependencies for our bot
type application struct {
config config
logger zerolog.Logger
mattermostClient *model.Client4
mattermostWebSocketClient *model.WebSocketClient
mattermostUser *model.User
mattermostChannel *model.Channel
mattermostTeam *model.Team
}
Loading

0 comments on commit a715fa2

Please sign in to comment.