From 305b3eeb767eaa4c7c609a1f6d3287401600b34c Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Thu, 1 Apr 2021 13:14:00 +0200 Subject: [PATCH] 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 --- README.md | 2 ++ config.go | 8 +++++--- notifier_telegram.go | 35 +++++++++++++++++++---------------- notifier_twitter.go | 27 +++++++++++++++------------ 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 4dea477..fbd23ef 100644 --- a/README.md +++ b/README.md @@ -131,10 +131,12 @@ Options: * `access_token`: authentication token generated for your Twitter account * `access_token_secret`: authentication token secret generated for your Twitter account * `hashtags`: list of key/value used to append hashtags to each tweet. Key is the pattern to match in the product name, value is the string to append to the tweet. For example, `{"twitter": {"hashtags": [{"rtx 3090": "#nvidia #rtx3090"}]}}` will detect `rtx 3090` to append `#nvidia #rtx3090` at the end of the tweet. + * `enable_replies`: reply to original message when product is not available anymore * `telegram` (optional): * `channel_name`: send message to a channel (ex: `@channel`) * `chat_id`: send message to a chat (ex: `1234`) * `token`: key returned by BotFather + * `enable_replies`: reply to original message when product is not available anymore * `include_regex` (optional): include products with a name matching this regexp * `exclude_regex` (optional): exclude products with a name matching this regexp * `browser_address` (optional): set headless browser address (ex: `http://127.0.0.1:9222`) diff --git a/config.go b/config.go index 3de35bb..ddfd31e 100644 --- a/config.go +++ b/config.go @@ -25,13 +25,15 @@ type TwitterConfig struct { AccessToken string `json:"access_token"` AccessTokenSecret string `json:"access_token_secret"` Hashtags []map[string]string `json:"hashtags"` + EnableReplies bool `json:"enable_replies"` } // TelegramConfig to store Telegram API key type TelegramConfig struct { - Token string `json:"token"` - ChatID int64 `json:"chat_id"` - ChannelName string `json:"channel_name"` + Token string `json:"token"` + ChatID int64 `json:"chat_id"` + ChannelName string `json:"channel_name"` + EnableReplies bool `json:"enable_replies"` } // ApiConfig to store HTTP API configuration diff --git a/notifier_telegram.go b/notifier_telegram.go index d102ced..c0d8894 100644 --- a/notifier_telegram.go +++ b/notifier_telegram.go @@ -19,11 +19,11 @@ type TelegramMessage struct { // TelegramNotifier to manage notifications to Twitter type TelegramNotifier struct { - db *gorm.DB - bot *telegram.BotAPI - chatID int64 - channelName string - timeout int + db *gorm.DB + bot *telegram.BotAPI + chatID int64 + channelName string + enableReplies bool } // NewTelegramNotifier to create a Notifier with Telegram capabilities @@ -42,10 +42,11 @@ func NewTelegramNotifier(config *TelegramConfig, db *gorm.DB) (*TelegramNotifier log.Debugf("connected to telegram as %s", bot.Self.UserName) return &TelegramNotifier{ - db: db, - bot: bot, - chatID: config.ChatID, - channelName: config.ChannelName, + db: db, + bot: bot, + chatID: config.ChatID, + channelName: config.ChannelName, + enableReplies: config.EnableReplies, }, nil } @@ -92,15 +93,17 @@ func (n *TelegramNotifier) NotifyWhenNotAvailable(productURL string, duration ti return nil } - // format message - text := fmt.Sprintf("And it's gone (%s)", duration) + if n.enableReplies { + // format message + text := fmt.Sprintf("And it's gone (%s)", duration) - // send reply on telegram - _, err := n.sendMessage(text, m.MessageID) - if err != nil { - return fmt.Errorf("failed to reply on telegram: %s", err) + // send reply on telegram + _, err := n.sendMessage(text, m.MessageID) + if err != nil { + return fmt.Errorf("failed to reply on telegram: %s", err) + } + log.Infof("reply to telegram message %d sent", m.MessageID) } - log.Infof("reply to telegram message %d sent", m.MessageID) // remove message from database trx = n.db.Unscoped().Delete(&m) diff --git a/notifier_twitter.go b/notifier_twitter.go index 161bbd4..560e056 100644 --- a/notifier_twitter.go +++ b/notifier_twitter.go @@ -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)