Fix crash on non existing names, add inline query
This commit is contained in:
parent
e1ac4dbfcd
commit
5aab5c3b63
3
go.mod
3
go.mod
|
@ -8,6 +8,7 @@ require (
|
||||||
github.com/dghubble/oauth1 v0.6.0
|
github.com/dghubble/oauth1 v0.6.0
|
||||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||||
github.com/jinzhu/gorm v1.9.12
|
github.com/jinzhu/gorm v1.9.12
|
||||||
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
|
||||||
github.com/sahilm/fuzzy v0.1.0
|
github.com/sahilm/fuzzy v0.1.0
|
||||||
|
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -34,6 +34,8 @@ github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
|
||||||
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
|
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
|
||||||
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||||
|
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
|
||||||
|
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
||||||
|
|
|
@ -100,6 +100,9 @@ func sendReply(tweet *twitter.Tweet, betriebsstellen []database.Betriebsstelle)
|
||||||
|
|
||||||
func handleTelegram() {
|
func handleTelegram() {
|
||||||
for update := range updates {
|
for update := range updates {
|
||||||
|
if update.InlineQuery != nil {
|
||||||
|
telegram.DoInlineQuery(update)
|
||||||
|
}
|
||||||
if update.Message == nil {
|
if update.Message == nil {
|
||||||
// ignore any non-Message Updates
|
// ignore any non-Message Updates
|
||||||
continue
|
continue
|
||||||
|
@ -109,19 +112,28 @@ func handleTelegram() {
|
||||||
if update.Message.IsCommand() {
|
if update.Message.IsCommand() {
|
||||||
switch update.Message.Command() {
|
switch update.Message.Command() {
|
||||||
case "start", "help":
|
case "start", "help":
|
||||||
reply := "Willkommen beim DB 640 Telegram Bot!\n\nEinfach den gewünschten DB 640 Betriebsstellencode schicken, und der Bot antwortet mit der zugehörigen Betriebsstelle!\n\nZum Beispiel: Nb -> Wiener Neustadt Hbf (in Nb)"
|
reply := `Willkommen beim DB 640 Telegram Bot!
|
||||||
|
|
||||||
|
Einfach den gewünschten DB 640 Betriebsstellencode schicken, und der Bot antwortet mit der zugehörigen Betriebsstelle!
|
||||||
|
|
||||||
|
Zum Beispiel: Nb -> Wiener Neustadt Hbf (in Nb)
|
||||||
|
|
||||||
|
Alternativ nach Codes suchen:
|
||||||
|
/find Name
|
||||||
|
Zum Beispiel: /find Matzleinsdorf gibt die entsprechenden Betriebsstelle(n) und Code(s) aus.`
|
||||||
telegram.SendReply(reply, update)
|
telegram.SendReply(reply, update)
|
||||||
continue
|
continue
|
||||||
case "find":
|
case "find":
|
||||||
if update.Message.CommandArguments() == "" {
|
if update.Message.CommandArguments() == "" {
|
||||||
reply := "Benutze /find <Name> um einen Code für eine Betriebsstelle zu finden!"
|
reply := "Benutze \"/find Name\" um einen Code für eine Betriebsstelle zu finden!"
|
||||||
telegram.SendReply(reply, update)
|
telegram.SendReply(reply, update)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var bs database.Betriebsstellen
|
var bs database.Betriebsstellen
|
||||||
var reply string
|
var reply string
|
||||||
if database.Db.Find(&bs, "name LIKE ?", "%"+update.Message.CommandArguments()+"%").Error != gorm.ErrRecordNotFound {
|
if err := database.Db.Find(&bs, "name LIKE ?", "%"+update.Message.CommandArguments()+"%").Error; err != nil || len(bs) == 0 {
|
||||||
|
reply = "Keine Betriebsstelle mit Namen '" + update.Message.CommandArguments() + "' gefunden!"
|
||||||
|
} else {
|
||||||
results := fuzzy.FindFrom(update.Message.CommandArguments(), bs)
|
results := fuzzy.FindFrom(update.Message.CommandArguments(), bs)
|
||||||
for i, r := range results {
|
for i, r := range results {
|
||||||
reply = reply + bs[r.Index].Code + ": " + bs[r.Index].Name + "\n"
|
reply = reply + bs[r.Index].Code + ": " + bs[r.Index].Name + "\n"
|
||||||
|
@ -130,8 +142,6 @@ func handleTelegram() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reply = reply[0 : len(reply)-1]
|
reply = reply[0 : len(reply)-1]
|
||||||
} else {
|
|
||||||
reply = "Keine Betriebsstelle mit Namen '" + update.Message.CommandArguments() + "' gefunden!"
|
|
||||||
}
|
}
|
||||||
err := telegram.SendReply(reply, update)
|
err := telegram.SendReply(reply, update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"git.1750studios.com/ToddShepard/DB640/internal/database"
|
"git.1750studios.com/ToddShepard/DB640/internal/database"
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
uuid "github.com/nu7hatch/gouuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateChan is telegram UpdatesChannel
|
// UpdateChan is telegram UpdatesChannel
|
||||||
|
@ -74,3 +75,28 @@ func SendAll(msg string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DoInlineQuery does the inline query search and returns results
|
||||||
|
func DoInlineQuery(update tgbotapi.Update) {
|
||||||
|
var config tgbotapi.InlineConfig
|
||||||
|
var results []interface{}
|
||||||
|
var betriebsstellen []database.Betriebsstelle
|
||||||
|
|
||||||
|
config.InlineQueryID = update.InlineQuery.ID
|
||||||
|
config.Results = results
|
||||||
|
|
||||||
|
err := database.Db.Limit(50).Find(&betriebsstellen, "Code LIKE ?", update.InlineQuery.Query+"%").Error
|
||||||
|
if err != nil || len(betriebsstellen) == 0 {
|
||||||
|
bot.AnswerInlineQuery(config)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, bs := range betriebsstellen {
|
||||||
|
msg := bs.Code + ": " + bs.Name
|
||||||
|
id, _ := uuid.NewV4()
|
||||||
|
results = append(results, tgbotapi.NewInlineQueryResultArticle(id.String(), msg, msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
config.Results = results
|
||||||
|
bot.AnswerInlineQuery(config)
|
||||||
|
log.Printf("[TELEGRAM Inline] %s: %s", update.InlineQuery.From.String(), update.InlineQuery.Query)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue