Add 'flush-reminders' command to remind module #1
3 changed files with 64 additions and 21 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
_data
|
||||
*.swp
|
||||
runlocal.sh
|
||||
|
|
|
@ -18,11 +18,15 @@ import (
|
|||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// For now receiving anything in this channel means 'cancel'
|
||||
type ReminderChannel chan interface{}
|
||||
|
||||
type Reminder struct {
|
||||
TargetID int64
|
||||
When int64
|
||||
Text string
|
||||
Reference *ReminderReference
|
||||
Channel ReminderChannel`json:"-"`
|
||||
}
|
||||
|
||||
type ReminderReference struct {
|
||||
|
@ -65,7 +69,7 @@ func (m *Module) Initialize(options modules.ModuleOptions) error {
|
|||
}
|
||||
for id, reminder := range reminders {
|
||||
pending.SetKey(id, reminder)
|
||||
go m.schedule(id)
|
||||
go m.schedule(id, reminder.Channel)
|
||||
}
|
||||
log.Printf("[remind] Loaded %d pending reminders\n", len(reminders))
|
||||
|
||||
|
@ -128,6 +132,7 @@ func (m *Module) OnUpdate(update tg.APIUpdate) {
|
|||
TargetID: message.User.UserID,
|
||||
When: timestamp.Unix(),
|
||||
Text: remindText,
|
||||
Channel: make(ReminderChannel),
|
||||
}
|
||||
if message.ReplyTo != nil {
|
||||
reminder.Reference = &ReminderReference{
|
||||
|
@ -137,7 +142,7 @@ func (m *Module) OnUpdate(update tg.APIUpdate) {
|
|||
}
|
||||
pending.SetKey(id, reminder)
|
||||
m.save()
|
||||
go m.schedule(id)
|
||||
go m.schedule(id, reminder.Channel)
|
||||
|
||||
whenday := "più tardi"
|
||||
_, todaym, todayd := time.Now().Date()
|
||||
|
@ -186,17 +191,50 @@ func (m *Module) OnUpdate(update tg.APIUpdate) {
|
|||
ReplyID: &message.MessageID,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if utils.IsCommand(message, m.name, "flush-reminders") {
|
||||
deleted := 0
|
||||
for key, reminder := range pending.Copy() {
|
||||
if reminder.TargetID == message.User.UserID {
|
||||
reminder.Channel <- struct{}{}
|
||||
pending.DeleteKey(key)
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
if deleted > 0 {
|
||||
var txt string
|
||||
if deleted == 1 {
|
||||
txt = "Ho cancellato l'unico reminder che avevi in coda."
|
||||
} else {
|
||||
txt = fmt.Sprintf("Ho cancellato tutti i <b>%d</b> reminder dalla tua coda.", deleted)
|
||||
}
|
||||
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||||
ChatID: message.Chat.ChatID,
|
||||
Text: txt,
|
||||
ReplyID: &message.MessageID,
|
||||
})
|
||||
} else {
|
||||
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||||
ChatID: message.Chat.ChatID,
|
||||
Text: "Non ho trovato nessun reminder da cancellare!",
|
||||
ReplyID: &message.MessageID,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Module) schedule(id string) {
|
||||
func (m *Module) schedule(id string, channel ReminderChannel) {
|
||||
// Get reminder
|
||||
r := pending.GetKey(id)
|
||||
remaining := r.When - time.Now().Unix()
|
||||
if remaining > 0 {
|
||||
// Wait remaining time
|
||||
time.Sleep(time.Second * time.Duration(remaining))
|
||||
}
|
||||
|
||||
select {
|
||||
case <-channel:
|
||||
// Interrupted
|
||||
case <-time.After(time.Second * time.Duration(remaining)):
|
||||
// Remind!
|
||||
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||||
ChatID: r.TargetID,
|
||||
|
@ -210,6 +248,8 @@ func (m *Module) schedule(id string) {
|
|||
MessageID: r.Reference.Message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Delete reminder from pending list and save list to disk
|
||||
pending.DeleteKey(id)
|
||||
m.save()
|
||||
|
@ -258,7 +298,6 @@ func scanMixedDelay(str string) (bool, time.Time, error) {
|
|||
nextIndex := strings.IndexRune(remaining, sep)
|
||||
remaining = remaining[nextIndex+1:]
|
||||
}
|
||||
fmt.Printf("tot: %s", now.Sub(time.Now()))
|
||||
return true, now, nil
|
||||
}
|
||||
|
||||
|
|
4
run.sh
4
run.sh
|
@ -6,5 +6,7 @@ export CLESSY_EMOJI_PATH=_data
|
|||
export CLESSY_UNSPLASH_FONT=_data/gill.ttf
|
||||
export CLESSY_MEME_FONT=_data/impact.ttf
|
||||
export CLESSY_SNAPCHAT_FONT=_data/source.ttf
|
||||
shift
|
||||
shift
|
||||
mkdir -p _data/pics
|
||||
go run .
|
||||
go run . $@
|
||||
|
|
Loading…
Reference in a new issue