Skip to content

Commit

Permalink
Added MastodonBee (muesli#192)
Browse files Browse the repository at this point in the history
Added support for the mastodon social network. You'll need to register an app in mastodon in order to use the Bee. There's a code snippet in /bees/mastodonbee/README.md for that.

Currently you can toot on mastodon and you'll get an event for each toot sent.
  • Loading branch information
penguwin authored and muesli committed Jun 9, 2018
1 parent 994f1c3 commit f6ac3ac
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 0 deletions.
Binary file added assets/bees/mastodonbee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions bees/mastodonbee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Mastodonbee

You'll need to register and authorize the 'beehive' application in mastodon in order to use the mastodonbee.


You can use following code snippet:

```
package main
import (
"context"
"fmt"
"log"
"github.com/mattn/go-mastodon"
)
func main() {
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: "https://server.address", // Enter _your_ server address here
ClientName: "", // Name for the client
Scopes: "read write follow", // Permission scopes
Website: "", // Optional
})
if err != nil {
log.Fatal(err)
}
// Prints out the ClientID and ClientSecret which you'll need to configure
// beehive propberly
fmt.Printf("client-id : %s\n", app.ClientID)
fmt.Printf("client-secret: %s\n", app.ClientSecret)
}
```
125 changes: 125 additions & 0 deletions bees/mastodonbee/mastodonbee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2018 Nicolas Martin
* 2018 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nicolas Martin <[email protected]>
* Christian Muehlhaeuser <[email protected]>
*/

// Package mastodonbee is a Bee that can connect to mastodon.
package mastodonbee

import (
"context"

mastodon "github.com/mattn/go-mastodon"

"github.com/muesli/beehive/bees"
)

// mastodonBee is a Bee that can connect to mastodon.
type mastodonBee struct {
bees.Bee

server string
clientID string
clientSecret string
email string
password string

client *mastodon.Client

evchan chan bees.Event
}

// Action triggers the action passed to it.
func (mod *mastodonBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}

switch action.Name {
case "toot":
var text string
action.Options.Bind("text", &text)

// Post status toot on mastodon
status, err := mod.client.PostStatus(context.Background(), &mastodon.Toot{
Status: text,
})
if err != nil {
mod.LogErrorf("Error sending toot: %v", err)
}

// Handle back 'toot_sent' event
ev := bees.Event{
Bee: mod.Name(),
Name: "toot_sent",
Options: []bees.Placeholder{
{
Name: "text",
Value: status.Content,
Type: "string",
},
},
}
mod.evchan <- ev
}
return outs
}

// Run executes the Bee's event loop.
func (mod *mastodonBee) Run(eventChan chan bees.Event) {
// Create the new api client
c := mastodon.NewClient(&mastodon.Config{
Server: mod.server,
ClientID: mod.clientID,
ClientSecret: mod.clientSecret,
})
// authorize it
err := c.Authenticate(context.Background(), mod.email, mod.password)
if err != nil {
mod.LogErrorf("Authorization failed, make sure the mastodon credentials are correct: %s", err)
return
}
// try to get user account
acc, err := c.GetAccountCurrentUser(context.Background())
if err != nil {
mod.LogErrorf("Failed to get current user account: %v", err)
}
mod.Logf("Successfully logged in: %s", acc.URL)

// set client
mod.client = c

// set eventchan
mod.evchan = eventChan

select {
case <-mod.SigChan:
return
}
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *mastodonBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)

options.Bind("server", &mod.server)
options.Bind("client_id", &mod.clientID)
options.Bind("client_secret", &mod.clientSecret)
options.Bind("email", &mod.email)
options.Bind("password", &mod.password)
}
146 changes: 146 additions & 0 deletions bees/mastodonbee/mastodonbeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2018 Nicolas Martin
* 2018 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nicolas Martin <[email protected]>
* Christian Muehlhaeuser <[email protected]>
*/

package mastodonbee

import "github.com/muesli/beehive/bees"

// mastodonBeeFactory is a factory for mastodonBees.
type mastodonBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *mastodonBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := mastodonBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)

return &bee
}

// ID returns the ID of this Bee.
func (factory *mastodonBeeFactory) ID() string {
return "mastodonbee"
}

// Name returns the name of this Bee.
func (factory *mastodonBeeFactory) Name() string {
return "mastodon"
}

// Description returns the description of this Bee.
func (factory *mastodonBeeFactory) Description() string {
return "Interact with mastodon"
}

// Image returns the filename of an image for this Bee.
func (factory *mastodonBeeFactory) Image() string {
return factory.ID() + ".png"
}

// LogoColor returns the preferred logo background color (used by the admin interface).
func (factory *mastodonBeeFactory) LogoColor() string {
return "#003b66"
}

// Options returns the options available to configure this Bee.
func (factory *mastodonBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "server",
Description: "URL for the desired mastodon server",
Type: "string",
Mandatory: true,
},
{
Name: "client_id",
Description: "Client id for the mastodon client",
Type: "string",
Mandatory: true,
},
{
Name: "client_secret",
Description: "Client secret for the mastodon client",
Type: "string",
Mandatory: true,
},
{
Name: "email",
Description: "User account email",
Type: "string",
Mandatory: true,
},
{
Name: "password",
Description: "User account password",
Type: "string",
Mandatory: true,
},
}
return opts
}

// Events describes the available events provided by this Bee.
func (factory *mastodonBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Name: "toot_sent",
Description: "A toot has been sent",
Options: []bees.PlaceholderDescriptor{
{
Name: "text",
Description: "Text of the toot that has been sent",
Type: "string",
},
},
},
}
return events
}

// Actions describes the available actions provided by this Bee.
func (factory *mastodonBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "toot",
Description: "Post a new status toot",
Options: []bees.PlaceholderDescriptor{
{
Name: "text",
Description: "Text of the status to toot, may not be longer than 500 characters",
Type: "string",
Mandatory: true,
},
},
},
}
return actions
}

func init() {
f := mastodonBeeFactory{}
bees.RegisterFactory(&f)
}
1 change: 1 addition & 0 deletions hives.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
_ "github.com/muesli/beehive/bees/ircbee"
_ "github.com/muesli/beehive/bees/jabberbee"
_ "github.com/muesli/beehive/bees/jenkinsbee"
_ "github.com/muesli/beehive/bees/mastodonbee"
_ "github.com/muesli/beehive/bees/mumblebee"
_ "github.com/muesli/beehive/bees/nagiosbee"
_ "github.com/muesli/beehive/bees/openweathermapbee"
Expand Down

0 comments on commit f6ac3ac

Please sign in to comment.