Added cj & jyutping lookup
This commit is contained in:
119
main.go
119
main.go
@@ -1,78 +1,81 @@
|
||||
package main
|
||||
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"
|
||||
"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"
|
||||
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 )
|
||||
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, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
bot.Debug = true
|
||||
bot.Debug = true
|
||||
|
||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
|
||||
updates := bot.GetUpdatesChan(u)
|
||||
updates := bot.GetUpdatesChan(u)
|
||||
|
||||
for update := range updates {
|
||||
if update.Message == nil {
|
||||
continue
|
||||
}
|
||||
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 )
|
||||
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
|
||||
}
|
||||
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,
|
||||
}
|
||||
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()
|
||||
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 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 )
|
||||
}
|
||||
}
|
||||
if !isGroup && !f_sent && f_err != nil {
|
||||
mesg := utils.MDv2Text(fmt.Sprintf("%s", f_err))
|
||||
botsend(bot, &update, &mesg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user