Add 'flush-reminders' command to remind module #1
3 changed files with 64 additions and 21 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
_data
|
_data
|
||||||
|
*.swp
|
||||||
|
runlocal.sh
|
||||||
|
|
|
@ -18,11 +18,15 @@ import (
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// For now receiving anything in this channel means 'cancel'
|
||||||
|
type ReminderChannel chan interface{}
|
||||||
|
|
||||||
type Reminder struct {
|
type Reminder struct {
|
||||||
TargetID int64
|
TargetID int64
|
||||||
When int64
|
When int64
|
||||||
Text string
|
Text string
|
||||||
Reference *ReminderReference
|
Reference *ReminderReference
|
||||||
|
Channel ReminderChannel`json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReminderReference struct {
|
type ReminderReference struct {
|
||||||
|
@ -65,7 +69,7 @@ func (m *Module) Initialize(options modules.ModuleOptions) error {
|
||||||
}
|
}
|
||||||
for id, reminder := range reminders {
|
for id, reminder := range reminders {
|
||||||
pending.SetKey(id, reminder)
|
pending.SetKey(id, reminder)
|
||||||
go m.schedule(id)
|
go m.schedule(id, reminder.Channel)
|
||||||
}
|
}
|
||||||
log.Printf("[remind] Loaded %d pending reminders\n", len(reminders))
|
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,
|
TargetID: message.User.UserID,
|
||||||
When: timestamp.Unix(),
|
When: timestamp.Unix(),
|
||||||
Text: remindText,
|
Text: remindText,
|
||||||
|
Channel: make(ReminderChannel),
|
||||||
}
|
}
|
||||||
if message.ReplyTo != nil {
|
if message.ReplyTo != nil {
|
||||||
reminder.Reference = &ReminderReference{
|
reminder.Reference = &ReminderReference{
|
||||||
|
@ -137,7 +142,7 @@ func (m *Module) OnUpdate(update tg.APIUpdate) {
|
||||||
}
|
}
|
||||||
pending.SetKey(id, reminder)
|
pending.SetKey(id, reminder)
|
||||||
m.save()
|
m.save()
|
||||||
go m.schedule(id)
|
go m.schedule(id, reminder.Channel)
|
||||||
|
|
||||||
whenday := "più tardi"
|
whenday := "più tardi"
|
||||||
_, todaym, todayd := time.Now().Date()
|
_, todaym, todayd := time.Now().Date()
|
||||||
|
@ -186,30 +191,65 @@ func (m *Module) OnUpdate(update tg.APIUpdate) {
|
||||||
ReplyID: &message.MessageID,
|
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
|
// Get reminder
|
||||||
r := pending.GetKey(id)
|
r := pending.GetKey(id)
|
||||||
remaining := r.When - time.Now().Unix()
|
remaining := r.When - time.Now().Unix()
|
||||||
if remaining > 0 {
|
|
||||||
// Wait remaining time
|
select {
|
||||||
time.Sleep(time.Second * time.Duration(remaining))
|
case <-channel:
|
||||||
}
|
// Interrupted
|
||||||
// Remind!
|
case <-time.After(time.Second * time.Duration(remaining)):
|
||||||
m.client.SendTextMessage(tg.ClientTextMessageData{
|
// Remind!
|
||||||
ChatID: r.TargetID,
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||||||
Text: "<b>Heyla! Mi avevi chiesto di ricordarti questo:</b>\n" + r.Text,
|
ChatID: r.TargetID,
|
||||||
ReplyID: nil,
|
Text: "<b>Heyla! Mi avevi chiesto di ricordarti questo:</b>\n" + r.Text,
|
||||||
})
|
ReplyID: nil,
|
||||||
if r.Reference != nil {
|
|
||||||
m.client.ForwardMessage(tg.ClientForwardMessageData{
|
|
||||||
ChatID: r.TargetID,
|
|
||||||
FromChatID: r.Reference.Chat,
|
|
||||||
MessageID: r.Reference.Message,
|
|
||||||
})
|
})
|
||||||
|
if r.Reference != nil {
|
||||||
|
m.client.ForwardMessage(tg.ClientForwardMessageData{
|
||||||
|
ChatID: r.TargetID,
|
||||||
|
FromChatID: r.Reference.Chat,
|
||||||
|
MessageID: r.Reference.Message,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete reminder from pending list and save list to disk
|
// Delete reminder from pending list and save list to disk
|
||||||
pending.DeleteKey(id)
|
pending.DeleteKey(id)
|
||||||
m.save()
|
m.save()
|
||||||
|
@ -258,7 +298,6 @@ func scanMixedDelay(str string) (bool, time.Time, error) {
|
||||||
nextIndex := strings.IndexRune(remaining, sep)
|
nextIndex := strings.IndexRune(remaining, sep)
|
||||||
remaining = remaining[nextIndex+1:]
|
remaining = remaining[nextIndex+1:]
|
||||||
}
|
}
|
||||||
fmt.Printf("tot: %s", now.Sub(time.Now()))
|
|
||||||
return true, now, nil
|
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_UNSPLASH_FONT=_data/gill.ttf
|
||||||
export CLESSY_MEME_FONT=_data/impact.ttf
|
export CLESSY_MEME_FONT=_data/impact.ttf
|
||||||
export CLESSY_SNAPCHAT_FONT=_data/source.ttf
|
export CLESSY_SNAPCHAT_FONT=_data/source.ttf
|
||||||
|
shift
|
||||||
|
shift
|
||||||
mkdir -p _data/pics
|
mkdir -p _data/pics
|
||||||
go run .
|
go run . $@
|
||||||
|
|
Loading…
Reference in a new issue