From a78802a7dd5d01117efc1c62b6501265e275c21e Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Mon, 21 Nov 2022 13:22:22 +0800 Subject: [PATCH 1/3] refactor: use Sender() instead of Update() to get user info --- bot.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bot.go b/bot.go index 5b0a49c..5142d73 100644 --- a/bot.go +++ b/bot.go @@ -11,12 +11,12 @@ import ( "git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/stats" ) -func isFromAdmin(upd tele.Update) bool { - if upd.Message == nil || upd.Message.Sender == nil { +func isFromAdmin(sender *tele.User) bool { + if sender == nil { return false } - _, ok := config.AdminUIDs[upd.Message.Sender.ID] + _, ok := config.AdminUIDs[sender.ID] return ok } @@ -39,8 +39,7 @@ func initBot() (*tele.Bot, error) { } func handleStartCmd(c tele.Context) error { - upd := c.Update() - if !isFromAdmin(upd) { + if !isFromAdmin(c.Sender()) { return c.Send("Hello, stranger :)") } @@ -48,8 +47,7 @@ func handleStartCmd(c tele.Context) error { } func handleTrafficCmd(c tele.Context) error { - upd := c.Update() - if !isFromAdmin(upd) { + if !isFromAdmin(c.Sender()) { return nil } From a42cb2db322bc7af6323aca61266c4b7fe21b5f5 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Mon, 21 Nov 2022 13:43:00 +0800 Subject: [PATCH 2/3] feat: add /me and /chat command --- bot.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/bot.go b/bot.go index 5142d73..c5015c9 100644 --- a/bot.go +++ b/bot.go @@ -35,6 +35,9 @@ func initBot() (*tele.Bot, error) { b.Handle("/start", handleStartCmd) b.Handle("/traffic", handleTrafficCmd) + b.Handle("/me", handleUserInfoCmd) + b.Handle("/chat", handleChatInfoCmd) + return b, nil } @@ -118,3 +121,56 @@ func fmtTraffic(r stats.VnstatTrafficRecord) string { return fmt.Sprintf("%.2f GiB", float64(biggest)/1024/1024/1024) } + +func handleUserInfoCmd(c tele.Context) error { + u := c.Sender() + if u == nil { + return c.Reply("Unknown.") + } + + replyText := []string{ + `*User Info*`, + "```", + fmt.Sprintf(`ID: %d`, u.ID), + fmt.Sprintf(`Username: %s`, u.Username), + fmt.Sprintf(`FirstName: %s`, u.FirstName), + fmt.Sprintf(`LastName: %s`, u.LastName), + fmt.Sprintf(`LanguageCode: %s`, u.LanguageCode), + fmt.Sprintf(`IsBot: %t`, u.IsBot), + fmt.Sprintf(`IsPremium: %t`, u.IsPremium), + "```", + } + return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}) +} + +func handleChatInfoCmd(c tele.Context) error { + chat := c.Chat() + if chat == nil { + return c.Reply("Unknown.") + } + loc := "" + if chat.ChatLocation != nil { + loc = chat.ChatLocation.Address + } + + replyText := []string{ + `*Chat Info*`, + "```", + fmt.Sprintf(`ID: %d`, chat.ID), + fmt.Sprintf(`Type: %s`, chat.Type), + fmt.Sprintf(`Title: %s`, chat.Title), + fmt.Sprintf(`FirstName: %s`, chat.FirstName), + fmt.Sprintf(`LastName: %s`, chat.LastName), + fmt.Sprintf(`Username: %s`, chat.Username), + fmt.Sprintf(`SlowMode: %d`, chat.SlowMode), + fmt.Sprintf(`StickerSet: %s`, chat.StickerSet), + fmt.Sprintf(`CanSetStickerSet: %t`, chat.CanSetStickerSet), + fmt.Sprintf(`LinkedChatID: %d`, chat.LinkedChatID), + fmt.Sprintf(`ChatLocation: %s`, loc), + fmt.Sprintf(`Private: %t`, chat.Private), + fmt.Sprintf(`Protected: %t`, chat.Protected), + fmt.Sprintf(`NoVoiceAndVideo: %t`, chat.NoVoiceAndVideo), + "```", + } + return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}) +} From b429137d20490304ec7770632dd789aa28e231cc Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Mon, 21 Nov 2022 13:43:21 +0800 Subject: [PATCH 3/3] build: update makefile --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f86aeac..f5f96df 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ pack: build tar --owner root --group root -c tgbot_misaka_5882f7 | zstdmt -15v > tgbot_misaka_5882f7.tar.zst send: pack - wh send tgbot_misaka_5882f7.tar.zst + wh send tgbot_misaka_5882f7.tar.zst --force-relay -.PHONY: build pack send +clean: + rm -f tgbot_misaka_5882f7 tgbot_misaka_5882f7.* + +.PHONY: build pack send clean