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:
parent
26eb0af9ea
commit
305b3eeb76
4 changed files with 41 additions and 31 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Reference in a new issue