lurk/handler/twitter.go

79 lines
1.6 KiB
Go

package handler
import (
"context"
"fmt"
"net/http"
"go.jolheiser.com/lurk/config"
"github.com/dghubble/go-twitter/twitter"
"github.com/rs/zerolog/log"
"go.jolheiser.com/disco"
)
type Twitter struct {
Filter config.Filter
Stream *twitter.Stream
}
func (t *Twitter) Run() {
log.Debug().Msgf("setting up stream for %v", t.Filter)
demux := twitter.NewSwitchDemux()
demux.Tweet = t.Tweet
log.Debug().Msgf("streaming %v", t.Filter)
demux.HandleChan(t.Stream.Messages)
log.Debug().Msgf("disconnected from stream: %v", t.Filter)
}
func (t *Twitter) Tweet(tweet *twitter.Tweet) {
log.Debug().Msgf("new tweet for %v", t.Filter)
if t.Filter.FollowStrict {
if tweet.InReplyToStatusIDStr != "" {
log.Debug().Msg("tweet is a reply")
return
}
if tweet.RetweetedStatus != nil {
log.Debug().Msg("tweet is a retweet")
return
}
var match bool
for _, id := range t.Filter.Follows {
if id == tweet.User.IDStr {
match = true
break
}
}
if !match {
log.Debug().Msg("tweet did not match any follow IDs")
return
}
}
w := &disco.Webhook{
Username: "@" + tweet.User.ScreenName,
AvatarURL: tweet.User.ProfileImageURLHttps,
Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
}
for _, webhook := range t.Filter.Webhooks {
req, err := w.Request(context.Background(), webhook)
if err != nil {
log.Err(err).Msg("")
continue
}
resp, err := httpClient.Do(req)
if err != nil {
log.Err(err).Msg("")
continue
}
if resp.StatusCode != http.StatusNoContent {
log.Error().Msgf(resp.Status)
continue
}
}
}