From c99648b72afd6d93c7f237b998fb310e598c2d84 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sat, 21 Oct 2017 14:14:46 +0400 Subject: [PATCH] Chats saving to database --- lib/dbmapping/chats.go | 10 ++-- lib/getters/chat.go | 56 +++++++++++++++++++ .../gettersinterface/gettersinterface.go | 14 +++-- lib/migrations/16_change_chat_type_column.go | 27 +++++++++ lib/migrations/migrations.go | 1 + lib/router/router.go | 14 ++--- 6 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 lib/getters/chat.go create mode 100644 lib/migrations/16_change_chat_type_column.go diff --git a/lib/dbmapping/chats.go b/lib/dbmapping/chats.go index 40ee88c..41e82d1 100644 --- a/lib/dbmapping/chats.go +++ b/lib/dbmapping/chats.go @@ -10,9 +10,9 @@ import ( // Chat is a struct, which represents `chats` table item in databse. type Chat struct { - ID int `db:"id"` - Name string `db:"name"` - ChatType bool `db:"chat_type"` - TelegramID int `db:"telegram_id"` - CreatedAt *time.Time `db:"created_at"` + ID int `db:"id"` + Name string `db:"name"` + ChatType string `db:"chat_type"` + TelegramID int `db:"telegram_id"` + CreatedAt time.Time `db:"created_at"` } diff --git a/lib/getters/chat.go b/lib/getters/chat.go new file mode 100644 index 0000000..4963ef6 --- /dev/null +++ b/lib/getters/chat.go @@ -0,0 +1,56 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package getters + +import ( + // stdlib + "log" + "time" + // 3rd-party + "github.com/go-telegram-bot-api/telegram-bot-api" + // local + "../dbmapping" +) + +// GetChatByID returns dbmapping.Chat instance with given ID. +func (g *Getters) GetChatByID(chatID int) (dbmapping.Chat, bool) { + chatRaw := dbmapping.Chat{} + err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), chatID) + if err != nil { + log.Println(err) + return chatRaw, false + } + + return chatRaw, true +} + +// GetOrCreateChat seeks for chat in database via Telegram update. +// In case, when there is no chat with such ID, new chat will be created. +func (g *Getters) GetOrCreateChat(telegramUpdate *tgbotapi.Update) (dbmapping.Chat, bool) { + chatRaw := dbmapping.Chat{} + err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), telegramUpdate.Message.Chat.ID) + if err != nil { + log.Printf("Chat stream not found in database.") + log.Printf(err.Error()) + + chatRaw.Name = telegramUpdate.Message.Chat.FirstName + " " + telegramUpdate.Message.Chat.LastName + chatRaw.ChatType = telegramUpdate.Message.Chat.Type + chatRaw.TelegramID = int(telegramUpdate.Message.Chat.ID) + chatRaw.CreatedAt = time.Now().UTC() + _, err = c.Db.NamedExec("INSERT INTO chats VALUES(NULL, :name, :chat_type, :telegram_id, :created_at)", &chatRaw) + if err != nil { + log.Printf(err.Error()) + return chatRaw, false + } + err2 := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=? AND chat_type=?"), chatRaw.TelegramID, chatRaw.ChatType) + if err2 != nil { + log.Println(err2) + return chatRaw, false + } + } else { + log.Printf("Chat stream found in database.") + } + + return chatRaw, true +} diff --git a/lib/getters/gettersinterface/gettersinterface.go b/lib/getters/gettersinterface/gettersinterface.go index a5b8ea7..4c0ee3d 100644 --- a/lib/getters/gettersinterface/gettersinterface.go +++ b/lib/getters/gettersinterface/gettersinterface.go @@ -4,6 +4,8 @@ package gettersinterface import ( + // 3rd-party + "github.com/go-telegram-bot-api/telegram-bot-api" // local "../../dbmapping" ) @@ -11,11 +13,13 @@ import ( // GettersInterface implements Getters for importing via appcontext. type GettersInterface interface { Init() - GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool) - GetPlayerByID(player_id int) (dbmapping.Player, bool) - GetProfile(player_id int) (dbmapping.Profile, bool) + GetOrCreateChat(update *tgbotapi.Update) (dbmapping.Chat, bool) + GetChatByID(chatID int) (dbmapping.Chat, bool) + GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) + GetPlayerByID(playerID int) (dbmapping.Player, bool) + GetProfile(playerID int) (dbmapping.Profile, bool) GetPokememes() ([]dbmapping.PokememeFull, bool) - GetBestPokememes(player_id int) ([]dbmapping.PokememeFull, bool) - GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool) + GetBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool) + GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool) PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int) } diff --git a/lib/migrations/16_change_chat_type_column.go b/lib/migrations/16_change_chat_type_column.go new file mode 100644 index 0000000..68429a5 --- /dev/null +++ b/lib/migrations/16_change_chat_type_column.go @@ -0,0 +1,27 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package migrations + +import ( + // stdlib + "database/sql" +) + +func ChangeChatTypeColumnUp(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE `chats` MODIFY `chat_type` varchar(191);") + if err != nil { + return err + } + + return nil +} + +func ChangeChatTypeColumnDown(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE `chats` MODIFY `chat_type` bool;") + if err != nil { + return err + } + + return nil +} diff --git a/lib/migrations/migrations.go b/lib/migrations/migrations.go index 2a178c2..46c97af 100644 --- a/lib/migrations/migrations.go +++ b/lib/migrations/migrations.go @@ -30,6 +30,7 @@ func (m *Migrations) Init() { goose.AddNamedMigration("13_create_weapons_and_add_wealth.go", CreateWeaponsAndAddWealthUp, CreateWeaponsAndAddWealthDown) goose.AddNamedMigration("14_fix_time_element.go", FixTimeElementUp, FixTimeElementDown) goose.AddNamedMigration("15_create_chats.go", CreateChatsUp, CreateChatsDown) + goose.AddNamedMigration("16_change_chat_type_column.go", ChangeChatTypeColumnUp, ChangeChatTypeColumnDown) } func (m *Migrations) Migrate() error { diff --git a/lib/router/router.go b/lib/router/router.go index 8504c79..b46f13e 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -25,16 +25,14 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { return "fail" } - chatID := update.Message.Chat.ID - fromID := int64(update.Message.From.ID) - log.Println(chatID) - log.Println(fromID) - if chatID == fromID { - log.Println("Private chat") - } else { - log.Println("Group") + chatRaw, ok := c.Getters.GetOrCreateChat(&update) + if !ok { + return "fail" } + log.Printf("Received message from chat ") + log.Println(chatRaw.TelegramID) + // Regular expressions var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)") var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")