Refactoring codes for more tg message types
This commit is contained in:
42
main.go
42
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
tgadaptor "github.com/tgckpg/golifehk/adaptors/tg"
|
||||
cjlookup "github.com/tgckpg/golifehk/datasources/cjlookup"
|
||||
kmb "github.com/tgckpg/golifehk/datasources/kmb"
|
||||
mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
|
||||
@@ -13,15 +14,6 @@ import (
|
||||
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"
|
||||
|
||||
msg.ReplyToMessageID = update.Message.MessageID
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func main() {
|
||||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
|
||||
if err != nil {
|
||||
@@ -48,12 +40,12 @@ func main() {
|
||||
mesg, processed := utils.SystemControl(update.Message)
|
||||
if processed {
|
||||
if mesg != "" {
|
||||
botsend(bot, &update, &mesg)
|
||||
tgadaptor.BotSendText(bot, &update, &mesg)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
f_queries := []func(string, string) query.IQueryResult{
|
||||
f_queries := []func(query.QueryMessage) query.IQueryResult{
|
||||
cjlookup.Query,
|
||||
mtrbus.Query,
|
||||
kmb.Query,
|
||||
@@ -61,21 +53,35 @@ func main() {
|
||||
|
||||
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 {
|
||||
tgMesg := update.Message
|
||||
q := query.QueryMessage{Lang: "zh-Hant", Text: tgMesg.Text}
|
||||
|
||||
if tgMesg.Location != nil {
|
||||
q.Location = &query.GeoLocation{tgMesg.Location.Latitude, tgMesg.Location.Longitude}
|
||||
}
|
||||
|
||||
for _, Query := range f_queries {
|
||||
|
||||
qResult := Query(q)
|
||||
sent, err := tgadaptor.BotSend(bot, &update, qResult)
|
||||
|
||||
if sent {
|
||||
f_sent = true
|
||||
botsend(bot, &update, &mesg)
|
||||
} else if f_err == nil {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
f_err = err
|
||||
}
|
||||
|
||||
if qResult.Consumed() {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isGroup && !f_sent && f_err != nil {
|
||||
mesg := utils.MDv2Text(fmt.Sprintf("%s", f_err))
|
||||
botsend(bot, &update, &mesg)
|
||||
tgadaptor.BotSendText(bot, &update, &mesg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user