golifehk/main.go

90 lines
2.4 KiB
Go

package main
import (
"fmt"
"log"
"os"
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
kmb "github.com/tgckpg/golifehk/datasources/kmb"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
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 )
mesg, err := mtrbus.Query( "zh-Hant", update.Message.Text ).Message()
if update.Message.Chat.IsGroup() {
if err == nil {
msg := tgbotapi.NewMessage( update.Message.Chat.ID, mesg )
msg.ParseMode = "MarkdownV2"
msg.ReplyToMessageID = update.Message.MessageID
bot.Send( msg )
}
} else {
var msg tgbotapi.MessageConfig
if err == nil {
msg = tgbotapi.NewMessage( update.Message.Chat.ID, mesg )
msg.ParseMode = "MarkdownV2"
} else {
msg = tgbotapi.NewMessage( update.Message.Chat.ID, fmt.Sprintf( "%s", err ) )
}
msg.ReplyToMessageID = update.Message.MessageID
bot.Send( msg )
}
mesg, err2 := kmb.Query( "zh-Hant", update.Message.Text ).Message()
if err != nil && err2 != nil {
continue
}
err = err2
if update.Message.Chat.IsGroup() {
if err == nil {
msg := tgbotapi.NewMessage( update.Message.Chat.ID, mesg )
msg.ParseMode = "MarkdownV2"
msg.ReplyToMessageID = update.Message.MessageID
bot.Send( msg )
}
} else {
var msg tgbotapi.MessageConfig
if err == nil {
msg = tgbotapi.NewMessage( update.Message.Chat.ID, mesg )
msg.ParseMode = "MarkdownV2"
} else {
msg = tgbotapi.NewMessage( update.Message.Chat.ID, fmt.Sprintf( "%s", err ) )
}
msg.ReplyToMessageID = update.Message.MessageID
bot.Send( msg )
}
}
}