Add /schedule command to send a message in certain time

calls v1.1.0
Bohdan Horbeshko 2 years ago
parent 6d7fb5bee4
commit a769e80808

@ -57,6 +57,7 @@ var chatCommands = map[string]command{
"d": command{"[n]", "delete your last message(s)"},
"s": command{"edited message", "edit your last message"},
"silent": command{"message", "send a message without sound"},
"schedule": command{"{online | 2006-01-02T15:04:05 | 15:04:05} message", "schedules a message either to timestamp or to whenever the user goes online"},
"add": command{"@username", "add @username to your chat list"},
"join": command{"https://t.me/invite_link", "join to chat via invite link"},
"group": command{"title", "create groupchat «title» with current user"},
@ -471,6 +472,86 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} else {
return "Message processing error", true
}
// schedule a message to timestamp or to going online
case "schedule":
if len(args) < 2 {
return "Not enough arguments", true
}
var state client.MessageSchedulingState
var result string
due := args[0]
if due == "online" {
state = &client.MessageSchedulingStateSendWhenOnline{}
result = due
} else {
if c.Session.Timezone == "" {
due += "Z"
} else {
due += c.Session.Timezone
}
switch 0 {
default:
// try bare time first
timestamp, err := time.Parse("15:04:05Z07:00", due)
if err == nil {
now := time.Now().In(c.Session.TimezoneToLocation())
// combine timestamp's time with today's date
timestamp = time.Date(
now.Year(),
now.Month(),
now.Day(),
timestamp.Hour(),
timestamp.Minute(),
timestamp.Second(),
0,
timestamp.Location(),
)
diff := timestamp.Sub(now)
if diff < 0 { // set to tomorrow
timestamp = timestamp.AddDate(0, 0, 1)
}
state = &client.MessageSchedulingStateSendAtDate{
SendDate: int32(timestamp.Unix()),
}
result = timestamp.Format(time.RFC3339)
break
}
timestamp, err = time.Parse(time.RFC3339, due)
if err == nil {
// 2038 doomsday again
state = &client.MessageSchedulingStateSendAtDate{
SendDate: int32(timestamp.Unix()),
}
result = timestamp.Format(time.RFC3339)
break
}
return "Invalid schedule time specifier", true
}
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 1), "")
if content != nil {
_, err := c.client.SendMessage(&client.SendMessageRequest{
ChatId: chatID,
InputMessageContent: content,
Options: &client.MessageSendOptions{
SchedulingState: state,
},
})
if err != nil {
return err.Error(), true
}
return "Scheduled to " + result, true
} else {
return "Message processing error", true
}
// add @contact
case "add":
if len(args) < 1 {

Loading…
Cancel
Save