5 Commits

Author SHA1 Message Date
97c9006fef Mastodon support 2022-11-04 06:45:20 +00:00
b542fcf901 Add test coverage to core 2022-10-30 01:16:31 -04:00
454b42c2d7 Use git has as build number 2022-10-30 00:06:39 -04:00
423de55daa Fix actions merge 2022-10-29 23:58:24 -04:00
4b7c9a6ee3 Upload assets to release 2022-10-29 23:57:28 -04:00
13 changed files with 180 additions and 5 deletions

View File

@ -29,6 +29,13 @@ jobs:
- name: Build
run: |
make build VERSION="${{ env.RELEASE_VERSION }}"
- name: Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
build/birdbot
sample_config.yaml
- name: Login to Docker Hub
uses: docker/login-action@v2
with:

View File

@ -1,7 +1,7 @@
PROJECTNAME="Bird Bot"
PROJECT_BIN="birdbot"
VERSION="DEV"
BUILD_NUMBER:=$(shell date "+%s%N" | cut -b1-13)
BUILD_NUMBER:=$(shell git rev-parse --short HEAD)
# Go related variables.
GOBASE=$(shell pwd)

View File

@ -9,6 +9,7 @@ import (
"github.com/ilyakaznacheev/cleanenv"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
"github.com/yeslayla/birdbot/mastodon"
)
var Version string
@ -16,6 +17,7 @@ var Build string
type Bot struct {
session *discord.Discord
mastodon *mastodon.Mastodon
// Discord Objects
guildID string
@ -52,6 +54,13 @@ func (app *Bot) Initialize(config_path string) error {
return fmt.Errorf("discord Guild ID is not set")
}
if cfg.Mastodon.ClientID != "" && cfg.Mastodon.ClientSecret != "" &&
cfg.Mastodon.Username != "" && cfg.Mastodon.Password != "" &&
cfg.Mastodon.Server != "" {
app.mastodon = mastodon.NewMastodon(cfg.Mastodon.Server, cfg.Mastodon.ClientID, cfg.Mastodon.ClientSecret,
cfg.Mastodon.Username, cfg.Mastodon.Password)
}
app.session = discord.New(app.guildID, cfg.Discord.Token)
// Register Event Handlers
@ -116,6 +125,13 @@ func (app *Bot) onEventCreate(d *discord.Discord, event *core.Event) {
eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", app.guildID, event.ID)
app.Notify(fmt.Sprintf("%s is organizing an event '%s': %s", event.Organizer.Mention(), event.Name, eventURL))
if app.mastodon != nil {
err = app.mastodon.Toot(fmt.Sprintf("A new event has been organized '%s': %s", event.Name, eventURL))
if err != nil {
fmt.Println("Failed to send Mastodon Toot:", err)
}
}
}
func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
@ -126,6 +142,13 @@ func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
}
app.Notify(fmt.Sprintf("%s cancelled '%s' on %s, %d!", event.Organizer.Mention(), event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
if app.mastodon != nil {
err = app.mastodon.Toot(fmt.Sprintf("'%s' cancelled on %s, %d!", event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
if err != nil {
fmt.Println("Failed to send Mastodon Toot:", err)
}
}
}
func (app *Bot) onEventUpdate(d *discord.Discord, event *core.Event) {

View File

@ -2,6 +2,7 @@ package core
type Config struct {
Discord DiscordConfig `yaml:"discord"`
Mastodon MastodonConfig `yaml:"mastodon"`
}
type DiscordConfig struct {
@ -12,3 +13,11 @@ type DiscordConfig struct {
ArchiveCategory string `yaml:"archive_category" env:"DISCORD_ARCHIVE_CATEGORY"`
NotificationChannel string `yaml:"notification_channel" env:"DISCORD_NOTIFICATION_CHANNEL"`
}
type MastodonConfig struct {
Server string `yaml:"server" env:"MASTODON_SERVER"`
Username string `yaml:"user" env:"MASTODON_USER"`
Password string `yaml:"password" env:"MASTODON_PASSWORD"`
ClientID string `yaml:"client_id" env:"MASTODON_CLIENT_ID"`
ClientSecret string `yaml:"client_secret" env:"MASTODON_CLIENT_SECRET"`
}

View File

@ -10,6 +10,7 @@ import (
func TestGetChannelName(t *testing.T) {
assert := assert.New(t)
// Test Valid Address
event := Event{
Name: "Hello World",
Location: "1234 Place Rd, Ann Arbor, MI 00000",
@ -17,6 +18,8 @@ func TestGetChannelName(t *testing.T) {
}
assert.Equal("jan-5-ann-arbor-hello-world", event.Channel().Name)
// Test Unparsable Location
// lmanley: Note it'd be nice to expand support for this
event = Event{
Name: "Hello World",
Location: "Michigan Theater, Ann Arbor",
@ -24,4 +27,36 @@ func TestGetChannelName(t *testing.T) {
}
assert.Equal("jan-5-hello-world", event.Channel().Name)
// Test Short Location
event = Event{
Name: "Hello World",
Location: "Monroe, MI",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-monroe-hello-world", event.Channel().Name)
// Test Short Location
event = Event{
Name: "Hello World",
Location: "Monroe St, Monroe , MI",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-monroe-hello-world", event.Channel().Name)
// Test Remote Event
event = Event{
Name: "Hello World",
Location: REMOTE_LOCATION,
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-online-hello-world", event.Channel().Name)
}
func TestMonthPrefix(t *testing.T) {
assert := assert.New(t)
event := Event{
DateTime: time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan", event.GetMonthPrefix())
}

19
core/pointers_test.go Normal file
View File

@ -0,0 +1,19 @@
package core
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBool(t *testing.T) {
assert := assert.New(t)
// Test const
assert.True(*Bool(true))
// Test var
sample := true
assert.True(*Bool(sample))
}

23
core/user_test.go Normal file
View File

@ -0,0 +1,23 @@
package core
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUserMention(t *testing.T) {
assert := assert.New(t)
// Create user object
user := &User{
ID: "sample_id",
}
assert.Equal("<@sample_id>", user.Mention())
// Test null user
var nullUser *User = nil
assert.NotEmpty(nullUser.Mention())
}

4
go.mod
View File

@ -11,10 +11,12 @@ require (
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/mattn/go-mastodon v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

6
go.sum
View File

@ -8,10 +8,14 @@ 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/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/ilyakaznacheev/cleanenv v1.4.0 h1:Gvwxt6wAPUo9OOxyp5Xz9eqhLsAey4AtbCF5zevDnvs=
github.com/ilyakaznacheev/cleanenv v1.4.0/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-mastodon v0.0.5 h1:P0e1/R2v3ho6kM7BUW0noQm8gAqHE0p8Gq1TMapIVAc=
github.com/mattn/go-mastodon v0.0.5/go.mod h1:cg7RFk2pcUfHZw/IvKe1FUzmlq5KnLFqs7eV2PHplV8=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -22,6 +26,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

29
mastodon/mastodon.go Normal file
View File

@ -0,0 +1,29 @@
package mastodon
import (
"context"
"log"
"github.com/mattn/go-mastodon"
)
type Mastodon struct {
client *mastodon.Client
}
func NewMastodon(server string, clientID string, clientSecret string, username string, password string) *Mastodon {
m := &Mastodon{}
m.client = mastodon.NewClient(&mastodon.Config{
Server: server,
ClientID: clientID,
ClientSecret: clientSecret,
})
err := m.client.Authenticate(context.Background(), username, password)
if err != nil {
log.Print("Failed to configure Mastodon: ", err)
return nil
}
return m
}

14
mastodon/toot.go Normal file
View File

@ -0,0 +1,14 @@
package mastodon
import (
"context"
"github.com/mattn/go-mastodon"
)
func (m *Mastodon) Toot(message string) error {
_, err := m.client.PostStatus(context.Background(), &mastodon.Toot{
Status: message,
})
return err
}

View File

@ -9,3 +9,11 @@ discord:
event_category: ""
archive_category: ""
notification_channel: ""
# mastodon:
# server: https://mastodon.social
# username: my_user
# password: secret
# client_id: 1234
# client_secret: secret2