Simplify query calls

This commit is contained in:
斟酌 鵬兄 2022-10-18 16:20:58 +08:00
parent 35b303f796
commit a062e37a4c
2 changed files with 36 additions and 56 deletions

View File

@ -45,6 +45,15 @@ func ( this QueryResult ) Message() ( string, error ) {
if this.Schedules == nil {
q := *this.Query
if len( *q.Results ) == 0 {
terms := make( []string, len(*q.SearchTerms), len(*q.SearchTerms) )
for i, term := range *q.SearchTerms {
terms[i] = term.Org
}
return "", fmt.Errorf( "Not Found: \"%s\"", strings.Join( terms, "\", \"" ) )
}
if q.Key == "" {
sort.Sort( query.ByKey( *q.Results ) )
for _, entry := range *q.Results {
@ -53,7 +62,7 @@ func ( this QueryResult ) Message() ( string, error ) {
sb.WriteString( " " )
writeShortRoute( &this.Lang, &sb, busStop )
}
} else if 1 == len( *q.SearchTerms ) && 0 < len( *q.Results ) {
} else if 1 == len( *q.SearchTerms ) {
// Route listing
st := map[string] *BusStop{}
@ -101,12 +110,6 @@ func ( this QueryResult ) Message() ( string, error ) {
sb.WriteString( "\n" )
}
} else if 1 < len( *q.SearchTerms ) && len( *q.Results ) == 0 {
terms := make( []string, len(*q.SearchTerms), len(*q.SearchTerms) )
for i, term := range *q.SearchTerms {
terms[i] = term.Org
}
return "", fmt.Errorf( "Not Found: \"%s\"", strings.Join( terms, "\", \"" ) )
} else {
return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
}

75
main.go
View File

@ -6,9 +6,19 @@ import (
"os"
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 {
@ -30,62 +40,29 @@ func main() {
}
log.Printf( "[%s] %s", update.Message.From.UserName, update.Message.Text )
mesg, err := mtrbus.Query( "zh-Hant", update.Message.Text ).Message()
isGroup := ( update.Message.Chat.ID < 0 )
if 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 )
f_queries := []func( string, string ) query.IQueryResult{
mtrbus.Query,
kmb.Query,
}
mesg, err2 := kmb.Query( "zh-Hant", update.Message.Text ).Message()
var f_sent bool = false
var f_err error = nil
for _, Query := range f_queries {
mesg, err := Query( "zh-Hant", update.Message.Text ).Message()
if err != nil && err2 != nil {
continue
if err == nil {
f_sent = true
botsend( bot, &update, &mesg )
} else if f_err == nil {
f_err = err
}
}
err = err2
if 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 )
if !isGroup && !f_sent && f_err != nil {
mesg := fmt.Sprintf( "%s", f_err )
botsend( bot, &update, &mesg )
}
}
}