82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
cjlookup "github.com/tgckpg/golifehk/datasources/cjlookup"
|
|
kmb "github.com/tgckpg/golifehk/datasources/kmb"
|
|
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
|
|
query "github.com/tgckpg/golifehk/query"
|
|
utils "github.com/tgckpg/golifehk/utils"
|
|
)
|
|
|
|
func botsend(bot *tgbotapi.BotAPI, update *tgbotapi.Update, mesg *string) {
|
|
var msg tgbotapi.MessageConfig
|
|
msg = tgbotapi.NewMessage(update.Message.Chat.ID, *mesg)
|
|
msg.ParseMode = "MarkdownV2"
|
|
|
|
msg.ReplyToMessageID = update.Message.MessageID
|
|
bot.Send(msg)
|
|
}
|
|
|
|
func main() {
|
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
bot.Debug = true
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
for update := range updates {
|
|
if update.Message == nil {
|
|
continue
|
|
}
|
|
|
|
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
|
isGroup := (update.Message.Chat.ID < 0)
|
|
|
|
mesg, processed := utils.SystemControl(update.Message)
|
|
if processed {
|
|
if mesg != "" {
|
|
botsend(bot, &update, &mesg)
|
|
}
|
|
continue
|
|
}
|
|
|
|
f_queries := []func(string, string) query.IQueryResult{
|
|
cjlookup.Query,
|
|
mtrbus.Query,
|
|
kmb.Query,
|
|
}
|
|
|
|
var f_sent bool = false
|
|
var f_err error = nil
|
|
for _, Query := range f_queries {
|
|
var err error
|
|
mesg, err = Query("zh-Hant", update.Message.Text).Message()
|
|
|
|
if err == nil {
|
|
f_sent = true
|
|
botsend(bot, &update, &mesg)
|
|
} else if f_err == nil {
|
|
f_err = err
|
|
}
|
|
}
|
|
|
|
if !isGroup && !f_sent && f_err != nil {
|
|
mesg := utils.MDv2Text(fmt.Sprintf("%s", f_err))
|
|
botsend(bot, &update, &mesg)
|
|
}
|
|
}
|
|
}
|