Remove twitter notification configuration (#2598)
This commit is contained in:
committed by
GitHub
parent
392da72c8b
commit
59e5cfefd4
@@ -2,15 +2,12 @@ package notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/notifications/browser"
|
||||
"github.com/owncast/owncast/notifications/discord"
|
||||
"github.com/owncast/owncast/notifications/twitter"
|
||||
"github.com/owncast/owncast/utils"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -20,7 +17,6 @@ type Notifier struct {
|
||||
datastore *data.Datastore
|
||||
browser *browser.Browser
|
||||
discord *discord.Discord
|
||||
twitter *twitter.Twitter
|
||||
}
|
||||
|
||||
// Setup will perform any pre-use setup for the notifier.
|
||||
@@ -68,9 +64,6 @@ func New(datastore *data.Datastore) (*Notifier, error) {
|
||||
if err := notifier.setupDiscord(); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
if err := notifier.setupTwitter(); err != nil {
|
||||
log.Errorln(err)
|
||||
}
|
||||
|
||||
return ¬ifier, nil
|
||||
}
|
||||
@@ -147,36 +140,6 @@ func (n *Notifier) notifyDiscord() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) setupTwitter() error {
|
||||
if twitterConfig := data.GetTwitterConfiguration(); twitterConfig.Enabled {
|
||||
if t, err := twitter.New(twitterConfig.APIKey, twitterConfig.APISecret, twitterConfig.AccessToken, twitterConfig.AccessTokenSecret, twitterConfig.BearerToken); err == nil {
|
||||
n.twitter = t
|
||||
} else if err != nil {
|
||||
return errors.Wrap(err, "error creating twitter notifier")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notifier) notifyTwitter() {
|
||||
goLiveMessage := data.GetTwitterConfiguration().GoLiveMessage
|
||||
streamTitle := data.GetStreamTitle()
|
||||
if streamTitle != "" {
|
||||
goLiveMessage += "\n" + streamTitle
|
||||
}
|
||||
tagString := ""
|
||||
for _, tag := range utils.ShuffleStringSlice(data.GetServerMetadataTags()) {
|
||||
tagString = fmt.Sprintf("%s #%s", tagString, tag)
|
||||
}
|
||||
tagString = strings.TrimSpace(tagString)
|
||||
|
||||
message := fmt.Sprintf("%s\n%s\n\n%s", goLiveMessage, data.GetServerURL(), tagString)
|
||||
|
||||
if err := n.twitter.Notify(message); err != nil {
|
||||
log.Errorln("error sending twitter message", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Notify will fire the different notification channels.
|
||||
func (n *Notifier) Notify() {
|
||||
if n.browser != nil {
|
||||
@@ -186,8 +149,4 @@ func (n *Notifier) Notify() {
|
||||
if n.discord != nil {
|
||||
n.notifyDiscord()
|
||||
}
|
||||
|
||||
if n.twitter != nil {
|
||||
n.notifyTwitter()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/dghubble/oauth1"
|
||||
"github.com/g8rswimmer/go-twitter/v2"
|
||||
)
|
||||
|
||||
/*
|
||||
1. developer.twitter.com. Apply to be a developer if needed.
|
||||
2. Projects and apps -> Your project name
|
||||
3. Settings.
|
||||
4. Scroll down to"User authentication settings" Edit
|
||||
5. Enable OAuth 1.0a with Read/Write permissions.
|
||||
6. Fill out the form with your information. Callback can be anything.
|
||||
7. Go to your project "Keys and tokens"
|
||||
8. Generate API key and secret.
|
||||
9. Generate access token and secret. Verify it says "Read and write permissions."
|
||||
10. Generate bearer token.
|
||||
*/
|
||||
|
||||
type authorize struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
func (a authorize) Add(req *http.Request) {
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
|
||||
}
|
||||
|
||||
// Twitter is an instance of the Twitter notifier.
|
||||
type Twitter struct {
|
||||
apiKey string
|
||||
apiSecret string
|
||||
accessToken string
|
||||
accessTokenSecret string
|
||||
bearerToken string
|
||||
}
|
||||
|
||||
// New returns a new instance of the Twitter notifier.
|
||||
func New(apiKey, apiSecret, accessToken, accessTokenSecret, bearerToken string) (*Twitter, error) {
|
||||
if apiKey == "" || apiSecret == "" || accessToken == "" || accessTokenSecret == "" || bearerToken == "" {
|
||||
return nil, errors.New("missing some or all of the required twitter configuration values")
|
||||
}
|
||||
|
||||
return &Twitter{
|
||||
apiKey: apiKey,
|
||||
apiSecret: apiSecret,
|
||||
accessToken: accessToken,
|
||||
accessTokenSecret: accessTokenSecret,
|
||||
bearerToken: bearerToken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Notify will send a notification to Twitter with the supplied text.
|
||||
func (t *Twitter) Notify(text string) error {
|
||||
config := oauth1.NewConfig(t.apiKey, t.apiSecret)
|
||||
token := oauth1.NewToken(t.accessToken, t.accessTokenSecret)
|
||||
httpClient := config.Client(oauth1.NoContext, token)
|
||||
|
||||
client := &twitter.Client{
|
||||
Authorizer: authorize{
|
||||
Token: t.bearerToken,
|
||||
},
|
||||
Client: httpClient,
|
||||
Host: "https://api.twitter.com",
|
||||
}
|
||||
|
||||
req := twitter.CreateTweetRequest{
|
||||
Text: text,
|
||||
}
|
||||
|
||||
_, err := client.CreateTweet(context.Background(), req)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user