Simplify query calls
This commit is contained in:
parent
35b303f796
commit
a062e37a4c
@ -45,6 +45,15 @@ func ( this QueryResult ) Message() ( string, error ) {
|
|||||||
if this.Schedules == nil {
|
if this.Schedules == nil {
|
||||||
|
|
||||||
q := *this.Query
|
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 == "" {
|
if q.Key == "" {
|
||||||
sort.Sort( query.ByKey( *q.Results ) )
|
sort.Sort( query.ByKey( *q.Results ) )
|
||||||
for _, entry := range *q.Results {
|
for _, entry := range *q.Results {
|
||||||
@ -53,7 +62,7 @@ func ( this QueryResult ) Message() ( string, error ) {
|
|||||||
sb.WriteString( " " )
|
sb.WriteString( " " )
|
||||||
writeShortRoute( &this.Lang, &sb, busStop )
|
writeShortRoute( &this.Lang, &sb, busStop )
|
||||||
}
|
}
|
||||||
} else if 1 == len( *q.SearchTerms ) && 0 < len( *q.Results ) {
|
} else if 1 == len( *q.SearchTerms ) {
|
||||||
|
|
||||||
// Route listing
|
// Route listing
|
||||||
st := map[string] *BusStop{}
|
st := map[string] *BusStop{}
|
||||||
@ -101,12 +110,6 @@ func ( this QueryResult ) Message() ( string, error ) {
|
|||||||
sb.WriteString( "\n" )
|
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 {
|
} else {
|
||||||
return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
|
return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
|
||||||
}
|
}
|
||||||
|
75
main.go
75
main.go
@ -6,9 +6,19 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
|
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
|
||||||
kmb "github.com/tgckpg/golifehk/datasources/kmb"
|
kmb "github.com/tgckpg/golifehk/datasources/kmb"
|
||||||
|
query "github.com/tgckpg/golifehk/query"
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
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() {
|
func main() {
|
||||||
bot, err := tgbotapi.NewBotAPI( os.Getenv( "TELEGRAM_API_TOKEN" ) )
|
bot, err := tgbotapi.NewBotAPI( os.Getenv( "TELEGRAM_API_TOKEN" ) )
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -30,62 +40,29 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf( "[%s] %s", update.Message.From.UserName, update.Message.Text )
|
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 )
|
isGroup := ( update.Message.Chat.ID < 0 )
|
||||||
|
|
||||||
if isGroup {
|
f_queries := []func( string, string ) query.IQueryResult{
|
||||||
if err == nil {
|
mtrbus.Query,
|
||||||
msg := tgbotapi.NewMessage( update.Message.Chat.ID, mesg )
|
kmb.Query,
|
||||||
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()
|
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 {
|
if err == nil {
|
||||||
continue
|
f_sent = true
|
||||||
|
botsend( bot, &update, &mesg )
|
||||||
|
} else if f_err == nil {
|
||||||
|
f_err = err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = err2
|
if !isGroup && !f_sent && f_err != nil {
|
||||||
if isGroup {
|
mesg := fmt.Sprintf( "%s", f_err )
|
||||||
if err == nil {
|
botsend( bot, &update, &mesg )
|
||||||
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 )
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user