-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
] | ||
}, | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters