103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
|
package remind
|
||
|
|
||
|
import (
|
||
|
"git.fromouter.space/hamcha/tg"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (m *Module) cmdRicordami(message *tg.APIMessage) {
|
||
|
// Supported formats:
|
||
|
// Xs/m/h/d => in X seconds/minutes/hours/days
|
||
|
// HH:MM => at HH:MM (24 hour format)
|
||
|
// HH:MM:SS => at HH:MM:SS (24 hour format)
|
||
|
// dd/mm/yyyy => same hour, specific date
|
||
|
// dd/mm/yyyy-HH:MM => specific hour, specific dat
|
||
|
// dd/mm/yyyy-HH:MM:SS => specific hour, specific date
|
||
|
|
||
|
parts := strings.SplitN(*message.Text, " ", 3)
|
||
|
|
||
|
// Little hack to allow text-less reminders with replies
|
||
|
if len(parts) == 2 && message.ReplyTo != nil {
|
||
|
parts = append(parts, "")
|
||
|
}
|
||
|
|
||
|
if len(parts) < 3 {
|
||
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||
|
ChatID: message.Chat.ChatID,
|
||
|
Text: "<b>Sintassi</b>\n/ricordami <i>[quando]</i> Messaggio\n\n<b>Formati supportati per [quando]</b>:\n 10s 10m 10h 10d (secondi/minuti/ore/giorni)\n 13:20 15:55:01 (ora dello stesso giorno, formato 24h)\n 11/02/2099 11/02/2099-11:20:01 (giorno diverso, stessa ora [1] o specifica [2])",
|
||
|
ReplyID: &message.MessageID,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
format := parts[1]
|
||
|
remindText := parts[2]
|
||
|
|
||
|
loc := defaultLocation
|
||
|
/*TODO REDO
|
||
|
if uloc, ok := tzlocs[update.User.UserID]; ok {
|
||
|
loc = uloc
|
||
|
}
|
||
|
*/
|
||
|
timestamp, err := parseDuration(format, loc)
|
||
|
if err != nil {
|
||
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||
|
ChatID: message.Chat.ChatID,
|
||
|
Text: err.Error(),
|
||
|
ReplyID: &message.MessageID,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
id := strconv.FormatInt(message.Chat.ChatID, 36) + "-" + strconv.FormatInt(message.MessageID, 36)
|
||
|
reminder := Reminder{
|
||
|
ReminderID: id,
|
||
|
TargetID: message.User.UserID,
|
||
|
When: timestamp.Unix(),
|
||
|
Text: remindText,
|
||
|
}
|
||
|
if message.ReplyTo != nil {
|
||
|
reminder.Reference = &ReminderReference{
|
||
|
Chat: message.Chat.ChatID,
|
||
|
Message: message.ReplyTo.MessageID,
|
||
|
}
|
||
|
}
|
||
|
pending.SetKey(id, reminder)
|
||
|
m.save()
|
||
|
go m.schedule(id)
|
||
|
|
||
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||
|
ChatID: message.Chat.ChatID,
|
||
|
Text: "Ok, vedrò di avvisarti " + formatWhen(timestamp, loc),
|
||
|
ReplyID: &message.MessageID,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (m *Module) schedule(id string) {
|
||
|
// 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))
|
||
|
}
|
||
|
// Remind!
|
||
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
||
|
ChatID: r.TargetID,
|
||
|
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,
|
||
|
})
|
||
|
}
|
||
|
// Delete reminder from pending list and save list to disk
|
||
|
pending.DeleteKey(id)
|
||
|
m.save()
|
||
|
}
|