lurk/handler/twitter.go

63 lines
1.3 KiB
Go
Raw Normal View History

package handler
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"go.jolheiser.com/lurk/config"
"github.com/dghubble/go-twitter/twitter"
"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),
}
if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil {
log.Err(err).Msg("")
}
}