From a6811f61fc1f671f6720c1331e4dc9d27b0d1149 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sun, 26 Nov 2017 07:55:13 +0400 Subject: [PATCH] Inline commands support (needed for pins handling) --- cmd/i2_bot/i2_bot.go | 17 ++++--- lib/migrations/5_create_locations.go | 2 +- lib/router/inline.go | 48 +++++++++++++++++++ lib/router/routerinterface/routerinterface.go | 2 + 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 lib/router/inline.go diff --git a/cmd/i2_bot/i2_bot.go b/cmd/i2_bot/i2_bot.go index 6e0346d..3950325 100644 --- a/cmd/i2_bot/i2_bot.go +++ b/cmd/i2_bot/i2_bot.go @@ -56,13 +56,18 @@ func main() { updates, _ := c.Bot.GetUpdatesChan(u) for update := range updates { - if update.Message == nil || update.Message.From == nil { - continue - } else if update.Message.Date < (int(time.Now().Unix()) - 1) { - // Ignore old messages + if update.Message != nil { + if update.Message.From != nil { + if update.Message.Date > (int(time.Now().Unix()) - 5) { + c.Router.RouteRequest(&update) + } + } + } else if update.InlineQuery != nil { + c.Router.RouteInline(&update) + } else if update.ChosenInlineResult != nil { + c.Log.Debug(update.ChosenInlineResult.ResultID) + } else { continue } - - c.Router.RouteRequest(&update) } } diff --git a/lib/migrations/5_create_locations.go b/lib/migrations/5_create_locations.go index c7682ce..ab7b967 100644 --- a/lib/migrations/5_create_locations.go +++ b/lib/migrations/5_create_locations.go @@ -36,7 +36,7 @@ func CreateLocationsUp(tx *sql.Tx) error { if err4 != nil { return err2 } - _, err5 := tx.Exec("INSERT INTO `locations` VALUES(NULL, '🏙:', 'Город', NOW());") + _, err5 := tx.Exec("INSERT INTO `locations` VALUES(NULL, '🏙', 'Город', NOW());") if err5 != nil { return err2 } diff --git a/lib/router/inline.go b/lib/router/inline.go new file mode 100644 index 0000000..d037a32 --- /dev/null +++ b/lib/router/inline.go @@ -0,0 +1,48 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package router + +import ( + "github.com/go-telegram-bot-api/telegram-bot-api" + "strings" +) + +// RouteInline routes inline requests to bot +func (r *Router) RouteInline(update *tgbotapi.Update) string { + availableCommands := make(map[string]string) + availableCommands["0"] = "🌲Лес" + availableCommands["1"] = "⛰Горы" + availableCommands["2"] = "🚣Озеро" + availableCommands["3"] = "🏙Город" + availableCommands["4"] = "🏛Катакомбы" + availableCommands["5"] = "⛪️Кладбище" + outputCommands := make(map[string]string) + for i, value := range availableCommands { + if strings.Contains(value, update.InlineQuery.Query) { + outputCommands[i] = value + } + } + + results := make([]interface{}, 0) + for i, value := range outputCommands { + article := tgbotapi.NewInlineQueryResultArticle(i, "Команда боту @PokememBroBot:", value) + article.Description = value + + results = append(results, article) + } + + inlineConf := tgbotapi.InlineConfig{ + InlineQueryID: update.InlineQuery.ID, + IsPersonal: true, + CacheTime: 0, + Results: results, + } + + _, err := c.Bot.AnswerInlineQuery(inlineConf) + if err != nil { + c.Log.Error(err.Error()) + } + + return "fail" +} diff --git a/lib/router/routerinterface/routerinterface.go b/lib/router/routerinterface/routerinterface.go index 1b27557..ac436b4 100644 --- a/lib/router/routerinterface/routerinterface.go +++ b/lib/router/routerinterface/routerinterface.go @@ -10,5 +10,7 @@ import ( // RouterInterface implements Router for importing via appcontext. type RouterInterface interface { Init() + + RouteInline(update *tgbotapi.Update) string RouteRequest(update *tgbotapi.Update) string }