From 79591ea69415864c5c36878d749a2b72d7bb3e82 Mon Sep 17 00:00:00 2001 From: johlanse Date: Mon, 21 Feb 2022 17:42:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20tg=E6=A8=A1=E5=BC=8F=E5=A4=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=94=AF=E6=8C=81=E5=BC=B9=E5=87=BA=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=94=A8=E6=88=B7=E8=BF=9B=E8=A1=8C=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core.go | 1 + lib/tg.go | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/core.go b/lib/core.go index cd76551..5a03f9e 100644 --- a/lib/core.go +++ b/lib/core.go @@ -121,6 +121,7 @@ func (c *Core) Login() ([]Cookie, error) { log.Errorln(i) } }() + c.Push("text", "开始添加用户") page, err := (*c.context).NewPage() if err != nil { diff --git a/lib/tg.go b/lib/tg.go index a0eb194..93cdddb 100644 --- a/lib/tg.go +++ b/lib/tg.go @@ -84,6 +84,13 @@ func (t *Telegram) Init() { go func() { for { update := <-channel + if update.Message == nil { + update.Message = &tgbotapi.Message{Text: update.CallbackQuery.Data} + t.bot.Send(tgbotapi.NewDeleteMessage(config.TG.ChatID, update.CallbackQuery.Message.MessageID)) + log.Infoln(update.CallbackQuery.Data) + } + log.Infoln("收到tg消息 ", update) + handles.Range(func(key, value interface{}) bool { if strings.Split(update.Message.Text, " ")[0] == key.(string) { go func() { @@ -128,9 +135,13 @@ func (t *Telegram) SendPhoto(image []byte) { } } -func (t *Telegram) SendMsg(message string) { +func (t *Telegram) SendMsg(message string) int { msg := tgbotapi.NewMessage(t.ChatId, message) - t.bot.Send(msg) + messa, err := t.bot.Send(msg) + if err != nil { + return 0 + } + return messa.MessageID } func login(bot *Telegram, args []string) { @@ -272,7 +283,7 @@ func study(bot *Telegram, args []string) { bot.SendMsg("未发现用户信息,请输入/login进行用户登录") return default: - if 0 <= len(args) { + if 0 < len(args) { i, err := strconv.Atoi(args[0]) if err != nil { bot.SendMsg(err.Error()) @@ -280,7 +291,17 @@ func study(bot *Telegram, args []string) { } cookies = users[i].Cookies } else { - bot.SendMsg("存在多名用户,未输入用户序号") + msgID := bot.SendMsg("存在多名用户,未输入用户序号") + markup := tgbotapi.InlineKeyboardMarkup{} + for i, user := range users { + markup.InlineKeyboard = append(markup.InlineKeyboard, append([]tgbotapi.InlineKeyboardButton{}, tgbotapi.NewInlineKeyboardButtonData(user.Nick, "/study "+strconv.Itoa(i)))) + } + + replyMarkup := tgbotapi.NewEditMessageReplyMarkup(config.TG.ChatID, msgID, markup) + _, err := bot.bot.Send(replyMarkup) + if err != nil { + return + } return } }