Chats saving to database
This commit is contained in:
parent
8dab6c0699
commit
c99648b72a
@ -12,7 +12,7 @@ import (
|
|||||||
type Chat struct {
|
type Chat struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
Name string `db:"name"`
|
Name string `db:"name"`
|
||||||
ChatType bool `db:"chat_type"`
|
ChatType string `db:"chat_type"`
|
||||||
TelegramID int `db:"telegram_id"`
|
TelegramID int `db:"telegram_id"`
|
||||||
CreatedAt *time.Time `db:"created_at"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
}
|
}
|
||||||
|
56
lib/getters/chat.go
Normal file
56
lib/getters/chat.go
Normal file
@ -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
|
||||||
|
}
|
@ -4,6 +4,8 @@
|
|||||||
package gettersinterface
|
package gettersinterface
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
// 3rd-party
|
||||||
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
// local
|
// local
|
||||||
"../../dbmapping"
|
"../../dbmapping"
|
||||||
)
|
)
|
||||||
@ -11,11 +13,13 @@ import (
|
|||||||
// GettersInterface implements Getters for importing via appcontext.
|
// GettersInterface implements Getters for importing via appcontext.
|
||||||
type GettersInterface interface {
|
type GettersInterface interface {
|
||||||
Init()
|
Init()
|
||||||
GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool)
|
GetOrCreateChat(update *tgbotapi.Update) (dbmapping.Chat, bool)
|
||||||
GetPlayerByID(player_id int) (dbmapping.Player, bool)
|
GetChatByID(chatID int) (dbmapping.Chat, bool)
|
||||||
GetProfile(player_id int) (dbmapping.Profile, bool)
|
GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool)
|
||||||
|
GetPlayerByID(playerID int) (dbmapping.Player, bool)
|
||||||
|
GetProfile(playerID int) (dbmapping.Profile, bool)
|
||||||
GetPokememes() ([]dbmapping.PokememeFull, bool)
|
GetPokememes() ([]dbmapping.PokememeFull, bool)
|
||||||
GetBestPokememes(player_id int) ([]dbmapping.PokememeFull, bool)
|
GetBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool)
|
||||||
GetPokememeByID(pokememe_id string) (dbmapping.PokememeFull, bool)
|
GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool)
|
||||||
PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int)
|
PossibilityRequiredPokeballs(location int, grade int, lvl int) (float64, int)
|
||||||
}
|
}
|
||||||
|
27
lib/migrations/16_change_chat_type_column.go
Normal file
27
lib/migrations/16_change_chat_type_column.go
Normal file
@ -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
|
||||||
|
}
|
@ -30,6 +30,7 @@ func (m *Migrations) Init() {
|
|||||||
goose.AddNamedMigration("13_create_weapons_and_add_wealth.go", CreateWeaponsAndAddWealthUp, CreateWeaponsAndAddWealthDown)
|
goose.AddNamedMigration("13_create_weapons_and_add_wealth.go", CreateWeaponsAndAddWealthUp, CreateWeaponsAndAddWealthDown)
|
||||||
goose.AddNamedMigration("14_fix_time_element.go", FixTimeElementUp, FixTimeElementDown)
|
goose.AddNamedMigration("14_fix_time_element.go", FixTimeElementUp, FixTimeElementDown)
|
||||||
goose.AddNamedMigration("15_create_chats.go", CreateChatsUp, CreateChatsDown)
|
goose.AddNamedMigration("15_create_chats.go", CreateChatsUp, CreateChatsDown)
|
||||||
|
goose.AddNamedMigration("16_change_chat_type_column.go", ChangeChatTypeColumnUp, ChangeChatTypeColumnDown)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Migrations) Migrate() error {
|
func (m *Migrations) Migrate() error {
|
||||||
|
@ -25,16 +25,14 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
|
|||||||
return "fail"
|
return "fail"
|
||||||
}
|
}
|
||||||
|
|
||||||
chatID := update.Message.Chat.ID
|
chatRaw, ok := c.Getters.GetOrCreateChat(&update)
|
||||||
fromID := int64(update.Message.From.ID)
|
if !ok {
|
||||||
log.Println(chatID)
|
return "fail"
|
||||||
log.Println(fromID)
|
|
||||||
if chatID == fromID {
|
|
||||||
log.Println("Private chat")
|
|
||||||
} else {
|
|
||||||
log.Println("Group")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Received message from chat ")
|
||||||
|
log.Println(chatRaw.TelegramID)
|
||||||
|
|
||||||
// Regular expressions
|
// Regular expressions
|
||||||
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
|
var durakMsg = regexp.MustCompile("(Д|д)(У|у)(Р|р)(А|а|Е|е|О|о)")
|
||||||
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
|
var huMsg = regexp.MustCompile("(Х|х)(У|у)(Й|й|Я|я|Ю|ю|Е|е)")
|
||||||
|
Reference in New Issue
Block a user