owncast/notifications/discord/discord.go
Gabe Kangas 4a17f30da8
Outbound live stream notifications (#1663)
* First pass at browser, discord, twilio notifications

* Commit updated Javascript packages

* Remove twilio notification support

* Email notifications/smtp support

* Fix Firefox notification support, remove chrome checks

* WIP more email work

* Add support for twitter notifications

* Add stream title to discord and twitter notifications

* Update notification registration modal

* Fix hide/show email section

* Commit updated API documentation

* Commit updated Javascript packages

* Fix post-rebase missing var

* Remove unused var

* Handle unsubscribe errors for browser push

* Standardize email config prop names

* Allow overriding go live email template

* Some notifications cleanup

* Commit updated Javascript packages

* Remove email/smtp/mailjet support

* Remove more references to email notifications

Co-authored-by: Owncast <owncast@owncast.online>
2022-03-18 13:33:23 -07:00

61 lines
1.3 KiB
Go

package discord
import (
"bytes"
"encoding/json"
"net/http"
"github.com/pkg/errors"
)
// Discord is an instance of the Discord service.
type Discord struct {
name string
avatar string
webhookURL string
}
// New will create a new instance of the Discord service.
func New(name, avatar, webhook string) (*Discord, error) {
return &Discord{
name: name,
avatar: avatar,
webhookURL: webhook,
}, nil
}
// Send will send a message to a Discord channel via a webhook.
func (t *Discord) Send(content string) error {
type message struct {
Username string `json:"username"`
Content string `json:"content"`
Avatar string `json:"avatar_url"`
}
msg := message{
Username: t.name,
Content: content,
Avatar: t.avatar,
}
jsonText, err := json.Marshal(msg)
if err != nil {
return errors.Wrap(err, "error marshalling discord message to json")
}
req, err := http.NewRequest("POST", t.webhookURL, bytes.NewReader(jsonText))
if err != nil {
return errors.Wrap(err, "error creating discord webhook request")
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Wrap(err, "error executing discord webhook")
}
return resp.Body.Close()
}