79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
utils "github.com/tgckpg/golifehk/utils"
|
|
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
|
|
kmb "github.com/tgckpg/golifehk/datasources/kmb"
|
|
query "github.com/tgckpg/golifehk/query"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
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{
|
|
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 )
|
|
}
|
|
}
|
|
}
|