Skip to content

Commit

Permalink
Merge pull request #10 from gender-equality-community/override_messages
Browse files Browse the repository at this point in the history
Override messages
  • Loading branch information
jspc authored Oct 1, 2022
2 parents 89eaff9 + 2f4f969 commit fbb6e75
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 248 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v -covermode=count -coverprofile=coverage.out ./...
run: make test

- name: gosec
run: |
Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ COSIGN_SECRET ?= cosign.key

default: app

app: *.go go.mod go.sum config.gen.go
app: *.go go.mod go.sum
go build -o $@ -ldflags="-s -w -linkmode=external -X main.LogLevel=$(LOGLVL)" -buildmode=pie -trimpath
upx $@

config.gen.go: config.toml gen/main.go
go run gen/main.go -f $< > $@

.PHONY: docker-build docker-push
docker-build:
docker build --label "tag=$(TAG)" --label "bom=https://github.com/gender-equality-community/gec-bot/releases/download/$(TAG)/bom.json" --build-arg logLevel=$(LOGLVL) -t $(IMG):$(TAG) .
Expand All @@ -21,3 +18,9 @@ docker-push:

.image:
echo $(IMG):$(TAG) > $@

.PHONY: test
test: export DATABASE = /tmp/testing.db
test: export REDIS_ADDR = redis://redis.example.com:6379
test:
go test -v -covermode=count -coverprofile=coverage.out ./...
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ The GEC Bot does two things:

![Infrastructure diagram showing how GEC components talk to one another](doc/gec-bots-arch.svg)

## Configuration

### Required environment variables

```bash
$ export REDIS_ADDR=redis:6379
$ export DATABASE=/tmp/database.db # Created if it doesn't exist
```

### Auto-responses

This bot provides some default messages, defined in [`config.go`](config.go)

1. Greeting response is sent when a recipient sends a message sends us a greeting
1. Thank You response is sent when a recipient sends us a message and is capped at a max of 1 per 30 mins
1. Disclaimer response is sent to ensure recipients don't send us stuff we can't deal with.

These can be overrided with the following respective environment variables:

```bash
$ export GREETING="Hello <3"
$ export THANK_YOU="Thanks! We'll get back to you"
$ export DISCLAIMER="Be aware that there's stuff we can't do!"
```

## Anonymisation

Expand All @@ -38,13 +62,6 @@ We then check whether this ID is already present in our database. This gives key
overhand-subdivide-thaw
promotion-basically-unreal
clumsily-tag-gizmo
return-lyricist-sixtieth
helmet-gothic-linguist
frugality-pediatric-overstate
subzero-plastic-sadness
sliding-dairy-sleet
endurance-ferry-election
unlatch-childhood-gristle
```

These are used to group messages from a recipient later on, through slack.
Expand All @@ -60,8 +77,7 @@ This application passes messages along via redis streams; these are lightweight,

## Deployment

Deployments are manual for now.

See the [Gender Equality Community kubernetes operator](https://github.com/gender-equality-community/gec-operator)

## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fgender-equality-community%2Fgec-bot.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fgender-equality-community%2Fgec-bot?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fgender-equality-community%2Fgec-bot.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fgender-equality-community%2Fgec-bot?ref=badge_large)
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func TestClient_Handle(t *testing.T) {
}{
{"Messages from me should be ignored", new(dummyClient), new(dummyRedis), &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: true, Sender: dummyJid()}, ID: "123"}}, false, ""},
{"Redis errors should bomb out, but not mark read", new(dummyClient), &dummyRedis{err: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}}, false, ""},
{"Greetings should be recognised as such", new(dummyClient), new(dummyRedis), &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("Hello!")}}, true, "Hello, and welcome to the Anonymous GEC Advisor. What's on your mind?"},
{"Greetings should be recognised as such", new(dummyClient), new(dummyRedis), &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("Hello!")}}, true, greetingResponse},
{"Where we've already messaged someone, don't message again", new(dummyClient), &dummyRedis{idExists: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("I would like to talk to somebody please")}}, true, ""},
{"Where we've not messaged someone recently, message again", new(dummyClient), &dummyRedis{idExists: false}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("I would like to talk to somebody please")}}, true, "Thank you for your message, please provide as much information as you're comfortable sharing and we'll get back to you as soon as we can."},
{"Brand new recipients should get a new ID", new(dummyClient), &dummyRedis{noJID: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("Hello!")}}, true, "Hello, and welcome to the Anonymous GEC Advisor. What's on your mind?"},
{"Messages using new format should work fine", new(dummyClient), &dummyRedis{noJID: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{ExtendedTextMessage: &waProto.ExtendedTextMessage{Text: stringRef("Hi!")}}}, true, "Hello, and welcome to the Anonymous GEC Advisor. What's on your mind?"},
{"Where we've not messaged someone recently, message again", new(dummyClient), &dummyRedis{idExists: false}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("I would like to talk to somebody please")}}, true, thankyouResponse},
{"Brand new recipients should get a new ID", new(dummyClient), &dummyRedis{noJID: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{Conversation: stringRef("Hello!")}}, true, greetingResponse},
{"Messages using new format should work fine", new(dummyClient), &dummyRedis{noJID: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{ExtendedTextMessage: &waProto.ExtendedTextMessage{Text: stringRef("Hi!")}}}, true, greetingResponse},
{"Messages where no message is found should be dropped", new(dummyClient), &dummyRedis{noJID: true}, &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: false, Sender: dummyJid()}, ID: "123"}, Message: &waProto.Message{}}, false, ""},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down
13 changes: 0 additions & 13 deletions config.gen.go

This file was deleted.

44 changes: 44 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "os"

var (
// Greeting response is sent when a recipient sends a message sends us a greeting
greetingResponse = Lookup("GREETING", "Hello, my name is Ada. What's on your mind?")

// Thank You response is sent when a recipient sends us a message and is capped at a max of 1 per 30 mins
thankyouResponse = Lookup("THANK_YOU", "Thank you for your message, please provide as much information as you're comfortable sharing and I'll get back to you as soon as I can.")

// Disclaimer response is sent to ensure recipients don't send us stuff we can't deal with.
disclaimerResponse = Lookup("DISCLAIMER", "DISCLAIMER: This is not an incident reporting service. If you believe you're being subjected to bullying, harassment, or misconduct then I cannot escalate on your behalf but I can advise you on your next steps.")

// DB is the location, on disk, of the database to use
db = MustLookup("DATABASE")

// Redis Address is the network address of the redis instance to use
redisAddr = MustLookup("REDIS_ADDR")
)

// Lookup accepts an environment variable name and a default value.
// If the variable exists and is set, then Lookup returns that,
// otherwise Lookup returns the default value
func Lookup(v, d string) string {
s, ok := os.LookupEnv(v)
if ok {
return s
}
return d
}

// MustLookup accepts and enviroment variable nae and returns
// the value of that.
//
// Should the var be unset or empty, MustLookup panics
func MustLookup(v string) (s string) {
s = os.Getenv(v)
if s == "" {
panic("Environment variable " + v + " is empty or unset")
}

return
}
28 changes: 0 additions & 28 deletions config.toml

This file was deleted.

33 changes: 33 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"os"
"testing"
)

func TestLookup(t *testing.T) {
got := Lookup("__AN_UNSET_KEY_HOPEFULLY", "<3")
if got != "<3" {
t.Errorf("unexpected value %q", got)
}
}

func TestLookup_Exists(t *testing.T) {
os.Setenv("__THIS_KEY_HAS_A_VALUE", "<3")

got := Lookup("__THIS_KEY_HAS_A_VALUE", "blahblahblah")
if got != "<3" {
t.Errorf("unexpected value %q", got)
}
}

func TestMustLookup(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Error("expected panic, received none")
}
}()

MustLookup("__AN_UNSET_KEY_HOPEFULLY")
}
76 changes: 0 additions & 76 deletions gen/main.go

This file was deleted.

79 changes: 0 additions & 79 deletions gen/main_test.go

This file was deleted.

4 changes: 0 additions & 4 deletions gen/testdata/config.toml

This file was deleted.

Empty file removed gen/testdata/empty.toml
Empty file.
Loading

0 comments on commit fbb6e75

Please sign in to comment.