Skip to content

Commit e426457

Browse files
committed
Improved responses to work with Mattermost
The responses of Plan-b-ot were not 100% according to the documentation of slack. As a result, compatibility with Mattermost was limited. Plan-b-ot is now 100% compatible to Mattermost as well as slack.
1 parent 61b619d commit e426457

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

README.md

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Plan-B-ot
2-
Plan-B-ot is an integration for [slack](https://slack.com/) which allows
3-
for Scrum Planning Poker inside the slack chat.
2+
Plan-B-ot is an integration for
3+
[Mattermost](http://www.mattermost.org/) and
4+
[slack](https://slack.com/) which allows for Scrum Planning Poker
5+
inside the Mattermost or slack chat.
46

57
Plan-B-ot is written in [go](https://golang.org/).
8+
Mattermost is an open source project: [Mattermost on GitHub](https://github.com/mattermost).
69

710
## Usage
8-
In a slack channel, use the slash command (see setup) to interact with
11+
In a channel, use the slash command (see setup) to interact with
912
the bot. If your slash command is called `/planbot`, you can issue the
1013
following commands:
1114
```
@@ -26,12 +29,12 @@ Orders plan-b-ot to print all the voting results in the specified
2629
channel (see setup).
2730

2831
## Setup
29-
### Slack
30-
You need to setup two integrations in slack: a slach command and a
32+
### Mattermost/Slack
33+
You need to setup two integrations: a slach command and an
3134
incoming web hook
3235

3336
Setup a slash command so users can interact with plan-b-ot:
34-
- Create a new slash command integration for your slack team.
37+
- Create a new slash command integration for your team.
3538
- Pick the command you want to use, e.g. `planbot`.
3639
- Specify the URL where your plan-b-ot will be running.
3740
If your server is reachable at `example.com`, your port is `8786`, and
@@ -41,7 +44,7 @@ See also bot setup.
4144
- Token: you need the token for the bot setup.
4245

4346
Setup an incoming web hook so plan-b-ot can post to your channel:
44-
- Create a new incoming web hook for your slack team.
47+
- Create a new incoming web hook for your team.
4548
- Set the `channel` that you want plan-b-ot to post to.
4649
- Webhook URL: you need the webhook URL for the bot setup.
4750

@@ -50,9 +53,15 @@ Copy `config.json.example` to `config.json`.
5053
Edit the contents of `config.json` to setup your plan-b-ot:
5154
- `Port`: The port on which plan-b-ot listens.
5255
- `Route`: The URL route for plan-b-ot.
53-
- `Token`: the token from the slack setup (slash command).
54-
- `Webhook URL`: The webhook URL from the slack setup
55-
(incoming web hook on slack's side).
56+
- `Token`: the token from the Mattermost/slack setup (slash command).
57+
- `Webhook URL`: The webhook URL from the Mattermost/slack setup
58+
(incoming web hook on Mattermost's/'slack's side).
5659

5760
Run plan-b-ot.
58-
Now the server is running and waiting for input from slack.
61+
Now the server is running and waiting for input from Mattermost/slack.
62+
63+
## Contributions
64+
Contributions are always welcome and do not have to be in a specific
65+
format.
66+
67+
Simply create a pull request on GitHub.

bot/bot.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,39 @@ import (
1212
"strconv"
1313
)
1414

15+
type CommandResponse struct {
16+
ResponseType string `json:"response_type"`
17+
Text string `json:"text"`
18+
GotoLocation string `json:"goto_location"`
19+
Attachments interface{} `json:"attachments"`
20+
}
21+
1522
// HandleRequest handles slash commands coming from slack
16-
func HandleRequest(userName string, args []string) (response string, status int) {
23+
func HandleRequest(userName string, args []string) (response CommandResponse, status int) {
1724
if len(args) < 1 {
18-
return "Please define something to do afte the slash command. E.g. `/planbot task T54`", http.StatusBadRequest
25+
return CommandResponse{Text: "Please define something to do afte the slash command. E.g. `/planbot task T54`"}, http.StatusBadRequest
1926
}
2027
action := args[0]
2128

22-
response = "Unknown Error"
29+
responseText := "Unknown Error"
2330
status = http.StatusInternalServerError
2431

2532
if action == "task" {
26-
response, status = setTask(userName, args)
33+
responseText, status = setTask(userName, args)
2734
} else if action == "vote" {
28-
response, status = setVote(userName, args)
35+
responseText, status = setVote(userName, args)
2936
} else if action == "results" {
30-
response, status = getResults()
37+
responseText, status = getResults()
3138
} else {
32-
response = fmt.Sprintf("Invalid item after the slash command: `%s`.", action)
39+
responseText = fmt.Sprintf("Invalid item after the slash command: `%s`.", action)
3340
status = http.StatusBadRequest
3441
}
3542

43+
response = CommandResponse{
44+
ResponseType: "ephemeral",
45+
Text: responseText,
46+
}
47+
3648
return response, status
3749
}
3850

@@ -131,5 +143,5 @@ func getResults() (response string, status int) {
131143
if err != nil {
132144
return err.Error(), http.StatusInternalServerError
133145
}
134-
return "Results were printed in slack channel", http.StatusOK
146+
return "Results were printed in channel", http.StatusOK
135147
}

bot/slack.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ import (
1111
"net/http"
1212
)
1313

14-
// sendToSlack sends a message to slack using a incoming web hook
14+
// sendToSlack sends a message to slack using an incoming web hook
1515
func sendToSlack(title, message, color string) error {
1616
attachment := `{
17-
"fallback": "` + title + `: ` + message + `",
18-
"color": "` + color + `",
19-
"fields": [
17+
"attachments": [
2018
{
21-
"title": "` + title + `",
22-
"value": "` + message + `",
23-
"short": false
19+
"fallback": "` + title + `: ` + message + `",
20+
"color": "` + color + `",
21+
"fields": [
22+
{
23+
"title": "` + title + `",
24+
"value": "` + message + `",
25+
"short": false
26+
}
27+
]
2428
}
2529
]
2630
}`

planbot.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package main
77

88
import (
9-
"fmt"
10-
"github.com/apheleia/plan-b-ot/bot"
9+
"encoding/json"
1110
"net/http"
1211
"strings"
12+
13+
"github.com/apheleia/plan-b-ot/bot"
1314
)
1415

1516
// Runs the server listening for the requests
@@ -36,11 +37,17 @@ func planbotHandler(w http.ResponseWriter, r *http.Request) {
3637
words := strings.Fields(text)
3738

3839
response, status := bot.HandleRequest(userName, words)
40+
responseJson, err := json.Marshal(response)
41+
if err != nil {
42+
http.Error(w, "Cannot marshal JSON in plan-b-ot", http.StatusInternalServerError)
43+
return
44+
}
3945

4046
if status != http.StatusOK {
41-
http.Error(w, response, status)
47+
http.Error(w, response.Text, status)
4248
return
4349
}
4450

45-
fmt.Fprintf(w, response)
51+
w.WriteHeader(status)
52+
w.Write(responseJson)
4653
}

0 commit comments

Comments
 (0)