-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add Rocket.chat support * fix: copy/paste errors and unnecessary comments fixed, Rocket.chat integration documentation added * fix: revert go.mod changes * fix: Rocket.chat doc link fixed Co-authored-by: nils måsén <[email protected]> Co-authored-by: Dmitry Kovalev <[email protected]> Co-authored-by: nils måsén <[email protected]>
- Loading branch information
1 parent
9c522a1
commit e68669d
Showing
12 changed files
with
381 additions
and
4 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
# Rocket.chat | ||
|
||
## URL Format | ||
|
||
## Creating a Webhook in Rocket.chat | ||
|
||
1. Open up the chat Administration by clicking on *Administration* menu | ||
![Screenshot 1](rocketchat/1.png) | ||
|
||
2. Open *Integrations* and then click *New* | ||
![Screenshot 2](rocketchat/2.png) | ||
|
||
3. Fill in the information for the webhook and click *Save*. Please don't forget to Enable your integration. | ||
![Screenshot 3](rocketchat/3.png) | ||
|
||
5. If you did everything correctly, Rocket.chat will give you the *URL* and *Token* to your newly created webhook. | ||
![Screenshot 4](rocketchat/4.png) | ||
|
||
6. Format the service URL | ||
``` | ||
rocketchat://your-domain.com/8eGdRzc9r4YYNyvge/2XYQcX9NBwJBKfQnphpebPcnXZcPEi32Nt4NKJfrnbhsbRfX | ||
└────────────────────────────────────────────────────────────────┘ | ||
token | ||
``` | ||
|
||
## Additional URL configuration | ||
|
||
Rocket.chat provides functionality to post as another user or to another channel / user, compared to the webhook configuration. | ||
<br/> | ||
To do this, you can add a *sender* and/or *channel* / *receiver* to the service URL. | ||
|
||
``` | ||
rocketchat://[email protected]/8eGdRzc9r4YYNyvge/2XYQcX9NBwJBKfQnphpebPcnXZcPEi32Nt4NKJfrnbhsbRfX/shoutrrrChannel | ||
└──────────┘ └────────────────────────────────────────────────────────────────┘ └─────────────┘ | ||
sender token channel | ||
rocketchat://[email protected]/8eGdRzc9r4YYNyvge/2XYQcX9NBwJBKfQnphpebPcnXZcPEi32Nt4NKJfrnbhsbRfX/@shoutrrrReceiver | ||
└──────────┘ └────────────────────────────────────────────────────────────────┘ └───────────────┘ | ||
sender token receiver | ||
``` | ||
|
||
## Passing parameters via code | ||
|
||
If you want to, you also have the possibility to pass parameters to the `send` function. | ||
<br/> | ||
The following example contains all parameters that are currently supported. | ||
|
||
```gotemplate | ||
params := (*types.Params)( | ||
&map[string]string{ | ||
"username": "overwriteUserName", | ||
"channel": "overwriteChannel", | ||
}, | ||
) | ||
service.Send("this is a message", params) | ||
``` | ||
|
||
This will overwrite any options, that you passed via URL. | ||
|
||
For more Rocket.chat Webhooks options see [official guide](https://docs.rocket.chat/guides/administrator-guides/integrations). |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,47 @@ | ||
package rocketchat | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/containrrr/shoutrrr/pkg/services/standard" | ||
"github.com/containrrr/shoutrrr/pkg/types" | ||
) | ||
|
||
// Service sends notifications to a pre-configured channel or user | ||
type Service struct { | ||
standard.Standard | ||
config *Config | ||
} | ||
|
||
// Initialize loads ServiceConfig from configURL and sets logger for this Service | ||
func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error { | ||
service.Logger.SetLogger(logger) | ||
service.config = &Config{} | ||
if err := service.config.SetURL(configURL); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Send a notification message to Rocket.chat | ||
func (service *Service) Send(message string, params *types.Params) error { | ||
config := service.config | ||
apiURL := buildURL(config) | ||
json, _ := CreateJSONPayload(config, message, params) | ||
res, err := http.Post(apiURL, "application/json", bytes.NewReader(json)) | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to send notification to service, response status code %s", res.Status) | ||
} | ||
return err | ||
} | ||
|
||
func buildURL(config *Config) string { | ||
return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB) | ||
} | ||
|
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,70 @@ | ||
package rocketchat | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/containrrr/shoutrrr/pkg/services/standard" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// Config for the rocket.chat service | ||
type Config struct { | ||
standard.QuerylessConfig | ||
standard.EnumlessConfig | ||
UserName string | ||
Host string | ||
TokenA string | ||
Channel string | ||
TokenB string | ||
} | ||
|
||
// GetURL returns a URL representation of it's current field values | ||
func (config *Config) GetURL() *url.URL { | ||
return &url.URL{ | ||
Host: config.Host, | ||
Path: fmt.Sprintf("hooks/%s/%s", config.TokenA, config.TokenB), | ||
Scheme: Scheme, | ||
ForceQuery: false, | ||
} | ||
} | ||
|
||
// SetURL updates a ServiceConfig from a URL representation of it's field values | ||
func (config *Config) SetURL(serviceURL *url.URL) error { | ||
|
||
UserName := serviceURL.User.Username() | ||
host := serviceURL.Hostname() | ||
|
||
path := strings.Split(serviceURL.Path, "/") | ||
|
||
if len(path) < 3 { | ||
return errors.New(NotEnoughArguments) | ||
} | ||
|
||
config.UserName = UserName | ||
config.Host = host | ||
config.TokenA = path[1] | ||
config.TokenB = path[2] | ||
if len(path) > 3 { | ||
if path[3][0:1] != "@" { | ||
config.Channel = "#" + path[3] | ||
} else { | ||
config.Channel = path[3] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const ( | ||
// Scheme is the identifying part of this service's configuration URL | ||
Scheme = "rocketchat" | ||
// NotEnoughArguments provided in the service URL | ||
NotEnoughArguments = "the apiURL does not include enough arguments" | ||
) | ||
|
||
// CreateConfigFromURL to use within the rocket.chat service | ||
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) { | ||
config := Config{} | ||
err := config.SetURL(serviceURL) | ||
return &config, err | ||
} |
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,34 @@ | ||
package rocketchat | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/containrrr/shoutrrr/pkg/types" | ||
) | ||
|
||
// JSON used within the Rocket.chat service | ||
type JSON struct { | ||
Text string `json:"text"` | ||
UserName string `json:"username,omitempty"` | ||
Channel string `json:"channel,omitempty"` | ||
} | ||
|
||
// CreateJSONPayload compatible with the rocket.chat webhook api | ||
func CreateJSONPayload(config *Config, message string, params *types.Params) ([]byte, error) { | ||
payload := JSON{ | ||
Text: message, | ||
UserName: config.UserName, | ||
Channel: config.Channel, | ||
} | ||
|
||
if params != nil { | ||
if value, found := (*params)["username"]; found { | ||
payload.UserName = value | ||
} | ||
if value, found := (*params)["channel"]; found { | ||
payload.Channel = value | ||
} | ||
} | ||
return json.Marshal(payload) | ||
} | ||
|
Oops, something went wrong.