Archived
1
0
Fork 0

Enable replies on Twitter and Telegram

By default, when a product is available, a notification is sent. When that same
product is not available, a reply is sent to the original message. With tons of
notifications, replies might be seen as flooding. This commit adds an option to
explicitly enable replies on Twitter and Telegram notifiers. By default, reply
messages are disabled.

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-04-01 13:14:00 +02:00
commit 305b3eeb76
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
4 changed files with 41 additions and 31 deletions

View file

@ -26,10 +26,11 @@ type Tweet struct {
// TwitterNotifier to manage notifications to Twitter
type TwitterNotifier struct {
db *gorm.DB
client *twitter.Client
user *twitter.User
hashtagsMap []map[string]string
db *gorm.DB
client *twitter.Client
user *twitter.User
hashtagsMap []map[string]string
enableReplies bool
}
// NewTwitterNotifier creates a TwitterNotifier
@ -57,7 +58,7 @@ func NewTwitterNotifier(c *TwitterConfig, db *gorm.DB) (*TwitterNotifier, error)
}
log.Debugf("connected to twitter as @%s", user.ScreenName)
return &TwitterNotifier{client: client, user: user, hashtagsMap: c.Hashtags, db: db}, nil
return &TwitterNotifier{client: client, user: user, hashtagsMap: c.Hashtags, db: db, enableReplies: c.EnableReplies}, nil
}
// create a brand new tweet
@ -147,15 +148,17 @@ func (c *TwitterNotifier) NotifyWhenNotAvailable(productURL string, duration tim
return nil
}
// format message
message := fmt.Sprintf("And it's gone (%s)", duration)
if c.enableReplies {
// format message
message := fmt.Sprintf("And it's gone (%s)", duration)
// close thread on twitter
_, err := c.replyToTweet(tweet.TweetID, message)
if err != nil {
return fmt.Errorf("failed to create reply tweet: %s", err)
// close thread on twitter
_, err := c.replyToTweet(tweet.TweetID, message)
if err != nil {
return fmt.Errorf("failed to create reply tweet: %s", err)
}
log.Infof("reply to tweet %d sent", tweet.TweetID)
}
log.Infof("reply to tweet %d sent", tweet.TweetID)
// remove tweet from database
trx = c.db.Unscoped().Delete(&tweet)