Skip to content

Commit

Permalink
Adding GotifyBee (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
deranjer authored Nov 3, 2020
1 parent 38622f9 commit ad90277
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 0 deletions.
Binary file added assets/bees/gotifybee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions bees/gotifybee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# GotifyBee

This bee can push notification to the specified gotify application as an action to some event.

You can get your **TOKEN** for your specified gotify application by going to the **APPS** section of the gotify server instance
and unhiding the token for your desired application.

## APP

* [Android APP](https://play.google.com/store/apps/details?id=com.github.gotify&hl=en_US&gl=US)
* [Chrome extension](https://chrome.google.com/webstore/detail/gotify-push/cbegkpikakpajcaoblfkeindhhikpfmd?hl=en)

## Configuration

The **message** field is required. If the message's title is empty, it would be replaced by Gotify.

The priority of the message is option.

### Options
```json
"Bees": [
{
"Name": "gotify example",
"Class": "gotify",
"Description": "This is example of gotify",
"Options": [
{
"Name": "token",
"Value": "TOKEN"
}
]
},
]
```

### Actions

```json
"Actions": [
{
"Bee": "gotify example",
"Name": "send",
"Options": [
{
"Name": "title",
"Type": "string",
"Value": ""
},
{
"Name": "message",
"Type": "string",
"Value": ""
},
{
"Name": "priority",
"Type": "string",
"Value": ""
}
]
},
]
```
90 changes: 90 additions & 0 deletions bees/gotifybee/gotifybee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2020 deranjer
*
* 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:
* deranjer <deranjer@gmail.com>
*/

// Package gotifybee is able to send notifications on Gotify.
package gotifybee

import (
"net/http"
"net/url"
"regexp"

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

// GotifyBee is a Bee that is able to send notifications on Gotify.
type GotifyBee struct {
bees.Bee
token string
serverURL string
}

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

switch action.Name {
case "send":
var title, message, priority string
action.Options.Bind("title", &title)
action.Options.Bind("message", &message)
action.Options.Bind("priority", &priority)

if priority == "" {
priority = "0"
}
if title == "" {
title = "Gotify"
}

// the message must be plain text, so
// remove the HTML tags, such as <html></html> and so on
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
message = re.ReplaceAllString(message, "\n")

data := url.Values{
"title": {title},
"message": {message},
"priority": {priority},
}
// Build the URL for sending the message
rawURL := mod.serverURL + "message?token=" + mod.token
resp, err := http.PostForm(rawURL, data)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
mod.Logln("Gotify send message success.")
}

default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}

return outs
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *GotifyBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("token", &mod.token)
options.Bind("serverURL", &mod.serverURL)
}
126 changes: 126 additions & 0 deletions bees/gotifybee/gotifybeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2020 deranjer
*
* 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:
* deranjer <deranjer@gmail.com>
*/

// Package gotifybee is able to send notifications on Gotify.
package gotifybee

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

// GotifyBeeFactory is a factory for GotifyBees.
type GotifyBeeFactory struct {
bees.BeeFactory
}

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

return &bee
}

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

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

// Description returns the description of this Bee.
func (factory *GotifyBeeFactory) Description() string {
return "Lets you push notifications on Gotify"
}

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

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

// Options returns the options available to configure this Bee.
func (factory *GotifyBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "token",
Description: "The gotify token for the Application to send messages",
Type: "string",
Mandatory: true,
},
{
Name: "serverURL",
Description: "The URL to the gotify server, eg: http://gotify.com/ (trailing slash required!)",
Type: "string",
Mandatory: true,
},
}
return opts
}

// Events describes the available events provided by this Bee.
func (factory *GotifyBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{}
return events
}

// Actions describes the available actions provided by this Bee.
func (factory *GotifyBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "send",
Description: "Sends a message",
Options: []bees.PlaceholderDescriptor{
{
Name: "title",
Description: "Title of the message",
Type: "string",
},
{
Name: "message",
Description: "Content of the message",
Type: "string",
Mandatory: true,
},
{
Name: "priority",
Description: "Priority of the message, see https://github.com/gotify/android#message-priorities",
Type: "string",
},
},
},
}
return actions
}

func init() {
f := GotifyBeeFactory{}
bees.RegisterFactory(&f)
}
1 change: 1 addition & 0 deletions hives.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
_ "github.com/muesli/beehive/bees/fsnotifybee"
_ "github.com/muesli/beehive/bees/githubbee"
_ "github.com/muesli/beehive/bees/gitterbee"
_ "github.com/muesli/beehive/bees/gotifybee"
_ "github.com/muesli/beehive/bees/horizonboxbee"
_ "github.com/muesli/beehive/bees/htmlextractbee"
_ "github.com/muesli/beehive/bees/httpbee"
Expand Down

0 comments on commit ad90277

Please sign in to comment.