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"
|
|
|
|
)
|
|
|
|
|
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)
|
2023-03-08 03:28:26 +09:00
|
|
|
|
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
|
|
|
|
2023-03-08 03:28:26 +09:00
|
|
|
b.Handle("/tr", handleTranslateCmd)
|
|
|
|
for _, tbtn := range translateBtns {
|
|
|
|
b.Handle(tbtn, handleTranslateBtn)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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() {
|
2023-03-08 05:49:26 +09:00
|
|
|
logger.Debugw("Log middleware", "update", upd)
|
2022-11-23 05:05:39 +09:00
|
|
|
}()
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-23 05:05:39 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stickerFromID(id string) *tele.Sticker {
|
|
|
|
return &tele.Sticker{
|
|
|
|
File: tele.File{
|
|
|
|
FileID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|