tgbot_misaka_5882f7/bot.go

130 lines
2.6 KiB
Go
Raw Normal View History

2022-11-21 04:39:21 +09:00
package main
import (
2022-11-21 16:17:02 +09:00
"math"
2022-11-21 04:39:21 +09:00
"time"
"github.com/go-errors/errors"
tele "gopkg.in/telebot.v3"
)
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)
}
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)
2023-03-08 03:28:26 +09:00
b.Handle("/year_progress", handleYearProgressCmd)
b.Handle("/xr", handleExchangeRateCmd)
2022-11-21 14:43:00 +09:00
2023-03-08 03:28:26 +09:00
b.Handle("/tr", handleTranslateCmd)
for _, tbtn := range translateBtns {
b.Handle(tbtn, handleTranslateBtn)
}
2023-04-21 16:43:05 +09:00
b.Handle("/kanji", handleKanjiCmd)
2023-03-08 03:28:26 +09:00
b.Handle(tele.OnText, handleGeneralMessage)
b.Handle(tele.OnSticker, handleGeneralMessage)
// admin required
adminGrp := b.Group()
adminGrp.Use(adminMiddleware)
adminGrp.Handle("/traffic", handleTrafficCmd)
adminGrp.Handle(&trafficBtnDays, handleTrafficBtnDays)
adminGrp.Handle(&trafficBtnMonths, handleTrafficBtnMonths)
adminGrp.Handle("/dig", handleDigCmd)
2022-11-21 04:39:21 +09:00
return b, nil
}
func adminMiddleware(next tele.HandlerFunc) tele.HandlerFunc {
return func(c tele.Context) error {
u := c.Sender()
if u == nil {
if cb := c.Callback(); cb != nil {
u = cb.Sender
}
}
if !isFromAdmin(u) {
return nil
}
return next(c)
}
}
func logMiddleware(next tele.HandlerFunc) tele.HandlerFunc {
return func(c tele.Context) error {
upd := c.Update()
defer func() {
2023-03-08 05:49:26 +09:00
logger.Debugw("Log middleware", "update", upd)
}()
return next(c)
}
}
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)
}
2023-03-20 19:12:41 +09:00
func handleGeneralMessage(c tele.Context) error {
if thread := matchAssistantConversation(c.Bot().Me, c.Message()); thread != nil {
return handleAssistantConversation(c, thread)
}
return nil
}
func stickerFromID(id string) *tele.Sticker {
return &tele.Sticker{
File: tele.File{
FileID: id,
},
}
}
2023-03-20 22:45:59 +09:00
func setTyping(c tele.Context) chan error {
resultCh := make(chan error, 1)
go func() {
defer close(resultCh)
resultCh <- c.Bot().Notify(c.Chat(), tele.Typing)
}()
return resultCh
}