2022-11-21 04:39:21 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-23 05:05:39 +09:00
|
|
|
"html"
|
2022-11-21 16:17:02 +09:00
|
|
|
"math"
|
2022-11-21 04:39:21 +09:00
|
|
|
"strings"
|
2022-11-23 05:05:39 +09:00
|
|
|
"text/tabwriter"
|
2022-11-21 04:39:21 +09:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
|
|
|
tele "gopkg.in/telebot.v3"
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/cmds"
|
2022-11-21 04:39:21 +09:00
|
|
|
)
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
const (
|
|
|
|
stickerPanic = "CAACAgUAAxkBAAMjY3zoraxZGB8Xejyw86bHLSWLjVcAArMIAAL7-nhXNK7dStmRUGsrBA"
|
|
|
|
stickerLoading = "CAACAgUAAxkBAAMmY3zp5UCMVRvy1isFCPHrx-UBWX8AApYHAALP8GhXEm9ZIBjn1v8rBA"
|
|
|
|
)
|
|
|
|
|
2022-11-21 14:22:22 +09:00
|
|
|
func isFromAdmin(sender *tele.User) bool {
|
|
|
|
if sender == nil {
|
2022-11-21 04:39:21 +09:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-07 17:07:14 +09:00
|
|
|
_, ok := config.adminUidLookup[sender.ID]
|
2022-11-21 04:39:21 +09:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBot() (*tele.Bot, error) {
|
|
|
|
pref := tele.Settings{
|
|
|
|
Token: config.TGBotToken,
|
|
|
|
Poller: &tele.LongPoller{Timeout: 15 * time.Second},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := tele.NewBot(pref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, 0)
|
|
|
|
}
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
b.Use(logMiddleware)
|
|
|
|
|
2022-11-21 04:39:21 +09:00
|
|
|
// command routing
|
|
|
|
b.Handle("/start", handleStartCmd)
|
|
|
|
|
2022-11-21 14:43:00 +09:00
|
|
|
b.Handle("/me", handleUserInfoCmd)
|
|
|
|
b.Handle("/chat", handleChatInfoCmd)
|
2022-11-21 16:42:43 +09:00
|
|
|
b.Handle("/year_progress", handleYearProgressCmd)
|
2022-12-07 12:01:21 +09:00
|
|
|
b.Handle("/xr", handleExchangeRateCmd)
|
2022-11-21 14:43:00 +09:00
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
b.Handle(tele.OnText, handleGeneralMessage)
|
|
|
|
b.Handle(tele.OnSticker, handleGeneralMessage)
|
|
|
|
|
|
|
|
// admin required
|
|
|
|
adminGrp := b.Group()
|
|
|
|
adminGrp.Use(adminMiddleware)
|
|
|
|
adminGrp.Handle("/traffic", handleTrafficCmd)
|
2022-12-09 15:49:38 +09:00
|
|
|
adminGrp.Handle(&trafficBtnDays, handleTrafficBtnDays)
|
|
|
|
adminGrp.Handle(&trafficBtnMonths, handleTrafficBtnMonths)
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
adminGrp.Handle("/dig", handleDigCmd)
|
|
|
|
|
2023-03-07 17:07:14 +09:00
|
|
|
// adminGrp.Handle("/test", testCmd)
|
2022-11-23 05:05:39 +09:00
|
|
|
|
2022-11-21 04:39:21 +09:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
func adminMiddleware(next tele.HandlerFunc) tele.HandlerFunc {
|
|
|
|
return func(c tele.Context) error {
|
2022-12-09 15:49:38 +09:00
|
|
|
u := c.Sender()
|
|
|
|
if u == nil {
|
|
|
|
if cb := c.Callback(); cb != nil {
|
|
|
|
u = cb.Sender
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isFromAdmin(u) {
|
2022-11-23 05:05:39 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func logMiddleware(next tele.HandlerFunc) tele.HandlerFunc {
|
|
|
|
return func(c tele.Context) error {
|
|
|
|
upd := c.Update()
|
|
|
|
defer func() {
|
|
|
|
logger.Infow("Log middleware", "update", upd)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 04:39:21 +09:00
|
|
|
func handleStartCmd(c tele.Context) error {
|
2022-11-21 14:22:22 +09:00
|
|
|
if !isFromAdmin(c.Sender()) {
|
2022-11-21 04:39:21 +09:00
|
|
|
return c.Send("Hello, stranger :)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Send("Hi :)")
|
|
|
|
}
|
|
|
|
|
2022-11-21 14:43:00 +09:00
|
|
|
func handleUserInfoCmd(c tele.Context) error {
|
|
|
|
u := c.Sender()
|
|
|
|
if u == nil {
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply("Unknown.", tele.Silent)
|
2022-11-21 14:43:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
"```",
|
|
|
|
}
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent)
|
2022-11-21 14:43:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleChatInfoCmd(c tele.Context) error {
|
|
|
|
chat := c.Chat()
|
|
|
|
if chat == nil {
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply("Unknown.", tele.Silent)
|
2022-11-21 14:43:00 +09:00
|
|
|
}
|
|
|
|
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),
|
|
|
|
"```",
|
|
|
|
}
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent)
|
2022-11-21 14:43:00 +09:00
|
|
|
}
|
2022-11-21 16:17:02 +09:00
|
|
|
|
|
|
|
func drawBar(progress float64, length int) string {
|
2022-11-22 21:35:18 +09:00
|
|
|
barChars := []rune("·-=")
|
2022-11-21 16:17:02 +09:00
|
|
|
|
|
|
|
if length <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
step := 1 / float64(length)
|
2022-11-22 21:35:18 +09:00
|
|
|
buf := make([]rune, length+2)
|
|
|
|
buf[0], buf[length+1] = '[', ']'
|
2022-11-21 16:17:02 +09:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
fill := (progress - float64(i)*step) / step
|
|
|
|
fill = math.Min(math.Max(fill, 0), 1)
|
|
|
|
idx := int(math.Round(fill * float64(len(barChars)-1)))
|
2022-11-22 21:35:18 +09:00
|
|
|
buf[i+1] = barChars[idx]
|
2022-11-21 16:17:02 +09:00
|
|
|
}
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:42:43 +09:00
|
|
|
func handleYearProgressCmd(c tele.Context) error {
|
|
|
|
yearStart := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
|
|
|
|
yearEnd := yearStart.AddDate(1, 0, 0)
|
|
|
|
yearDur := yearEnd.Sub(yearStart)
|
|
|
|
elapsed := time.Since(yearStart)
|
|
|
|
ratio := float64(elapsed) / float64(yearDur)
|
|
|
|
|
2022-11-22 21:35:18 +09:00
|
|
|
replyText := fmt.Sprintf(
|
|
|
|
"\n%d is <b>%2.0f%%</b> complete.\n<pre>%s</pre>",
|
2022-11-26 22:05:25 +09:00
|
|
|
time.Now().Year(), ratio*100, drawBar(ratio, 20),
|
2022-11-22 21:35:18 +09:00
|
|
|
)
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply(replyText, &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent)
|
2022-11-21 16:42:43 +09:00
|
|
|
}
|
2022-11-23 05:05:39 +09:00
|
|
|
|
|
|
|
func handleGeneralMessage(_ tele.Context) error {
|
|
|
|
// Do nothing for now
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stickerFromID(id string) *tele.Sticker {
|
|
|
|
return &tele.Sticker{
|
|
|
|
File: tele.File{
|
|
|
|
FileID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDigCmd(c tele.Context) error {
|
|
|
|
msg := c.Message()
|
|
|
|
if msg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := cmds.NewDigRequest(msg.Payload)
|
|
|
|
if err != nil {
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply(
|
|
|
|
"Invalid arguments.\nUsage: `/dig <name> [type]`",
|
|
|
|
&tele.SendOptions{ParseMode: tele.ModeMarkdown},
|
|
|
|
tele.Silent,
|
|
|
|
)
|
2022-11-23 05:05:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cmds.Dig(req)
|
|
|
|
if err != nil {
|
2022-11-26 22:03:48 +09:00
|
|
|
_ = c.Reply(stickerFromID(stickerPanic), tele.Silent)
|
2022-11-23 05:05:39 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
replyBuf := &strings.Builder{}
|
|
|
|
tw := tabwriter.NewWriter(replyBuf, 0, 0, 2, ' ', 0)
|
|
|
|
// Write header
|
|
|
|
if len(resp.Records) > 0 {
|
|
|
|
_, _ = tw.Write([]byte("Name\tTTL\tType\tData\n"))
|
|
|
|
}
|
|
|
|
// Write data
|
|
|
|
for _, r := range resp.Records {
|
|
|
|
_, _ = fmt.Fprintf(tw, "%s\t%d\t%s\t%s\n", r.Name, r.TTL, r.Type, r.Data)
|
|
|
|
}
|
|
|
|
_ = tw.Flush()
|
|
|
|
|
|
|
|
replyText := []string{
|
|
|
|
fmt.Sprintf("<i>Status: <b>%s</b></i>\n", resp.Status),
|
|
|
|
fmt.Sprintf("<i>Query Time: <b>%s</b></i>\n\n", resp.QueryTime),
|
|
|
|
"<pre>",
|
|
|
|
html.EscapeString(replyBuf.String()),
|
|
|
|
"</pre>",
|
|
|
|
}
|
2022-11-26 22:03:48 +09:00
|
|
|
return c.Reply(strings.Join(replyText, ""), &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent)
|
2022-11-23 05:05:39 +09:00
|
|
|
}
|