golifehk/main.go

92 lines
2.4 KiB
Go
Raw Normal View History

2022-09-13 13:42:51 +00:00
package main
import (
2022-09-16 20:33:47 +00:00
"fmt"
2022-09-13 13:42:51 +00:00
"log"
2022-09-14 14:54:51 +00:00
"os"
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
2022-09-16 20:33:47 +00:00
kmb "github.com/tgckpg/golifehk/datasources/kmb"
2022-09-14 14:54:51 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
2022-09-13 13:42:51 +00:00
)
func main() {
2022-09-14 14:54:51 +00:00
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 )
2022-09-16 20:33:47 +00:00
mesg, err := mtrbus.Query( "zh-Hant", update.Message.Text ).Message()
2022-09-25 11:09:03 +00:00
isGroup := ( update.Message.Chat.ID < 0 )
if isGroup {
2022-09-16 20:33:47 +00:00
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 ) )
}
2022-09-14 14:54:51 +00:00
2022-09-15 12:25:29 +00:00
msg.ReplyToMessageID = update.Message.MessageID
2022-09-16 20:33:47 +00:00
bot.Send( msg )
}
2022-09-14 14:54:51 +00:00
2022-09-16 20:33:47 +00:00
mesg, err2 := kmb.Query( "zh-Hant", update.Message.Text ).Message()
if err != nil && err2 != nil {
continue
}
err = err2
2022-09-25 11:09:03 +00:00
if isGroup {
2022-09-16 20:33:47 +00:00
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
2022-09-15 12:25:29 +00:00
bot.Send( msg )
}
2022-09-14 14:54:51 +00:00
}
2022-09-13 13:42:51 +00:00
}