Skip to content

Commit

Permalink
feat: add once cron
Browse files Browse the repository at this point in the history
  • Loading branch information
uerax committed Feb 19, 2025
1 parent e1da759 commit a06a942
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ func NewTask() *Task {
}
}

func (t *Task) Once(itv string, msg string) {
i, err := strconv.ParseInt(itv, 10, 64)
if err != nil {
return
}

time.Sleep(time.Duration(i) * time.Hour)

m := fmt.Sprintf("%d 小时了, %s", i, msg)

t.C <- m

}

func (t *Task) AddTask(itv string, msg string) {
i, err := strconv.ParseInt(itv, 10, 64)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions tg/aio.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (t *Aio) NewBot(token string, local string) {
t.bot = bot
t.local = local

tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("/once"),
)

t.CryptoApi = crypto.NewCryptoMonitor()
t.ChatGPTApi = chatgpt.NewChatGPT()
t.VpsApi = vps.NewVpsMonitor()
Expand Down
17 changes: 17 additions & 0 deletions tg/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ func chatid(id int64) {
}

// Cron
func onceCron(args string) {
arg := strings.Split(args, " ")
if len(arg) == 1 {
arg = append(arg, "4")
arg = append(arg, "吃药")
}
if len(arg) == 2 {
arg = append(arg, "吃药")
}
if len(arg) != 3 {
log.Printf("onceCron 参数有误: %s", args)
go api.DeleteAfterSendMessage("参数有误")
return
}
go api.Cron.Once(arg[1], arg[2])
}

func addCron(args string) {
arg := strings.Split(args, " ")
if len(arg) != 2 {
Expand Down
4 changes: 4 additions & 0 deletions tg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func Server() {
if update.Message.Photo != nil {
cutouts(update.Message.Chat.ID, update.Message.Photo)
}
case "once":
onceCron(update.Message.Text)
case "delete_cron":
deleteCron(update.Message.Text)
case "add_cron":
Expand Down Expand Up @@ -309,6 +311,8 @@ func Server() {
case "chatid":
chatid(update.Message.Chat.ID)
// Cron
case "once":
onceCron(update.Message.Text)
case "add_cron":
Cmd = "add_cron"
tips(update.Message.Chat.ID, "每隔多久一次提醒,单位/秒 例: `15 提醒内容(必填)`")
Expand Down

0 comments on commit a06a942

Please sign in to comment.