24
lib/chatter/chatterinterface/chatterinterface.go
Normal file
24
lib/chatter/chatterinterface/chatterinterface.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package chatterinterface
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
|
||||
)
|
||||
|
||||
// ChatterInterface implements Chatter for importing via appcontext.
|
||||
type ChatterInterface interface {
|
||||
Init()
|
||||
|
||||
GetOrCreateChat(update *tgbotapi.Update) (dbmapping.Chat, bool)
|
||||
GetChatByID(chatID int64) (dbmapping.Chat, bool)
|
||||
GetAllPrivateChats() ([]dbmapping.Chat, bool)
|
||||
GetAllGroupChats() ([]dbmapping.Chat, bool)
|
||||
|
||||
UpdateChatTitle(chatRaw *dbmapping.Chat, newTitle string) (*dbmapping.Chat, bool)
|
||||
UpdateChatTelegramID(update *tgbotapi.Update) (*dbmapping.Chat, bool)
|
||||
|
||||
GroupsList(update *tgbotapi.Update) string
|
||||
}
|
28
lib/chatter/exported.go
Normal file
28
lib/chatter/exported.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package chatter
|
||||
|
||||
import (
|
||||
"lab.pztrn.name/fat0troll/i2_bot/lib/appcontext"
|
||||
"lab.pztrn.name/fat0troll/i2_bot/lib/chatter/chatterinterface"
|
||||
)
|
||||
|
||||
var (
|
||||
c *appcontext.Context
|
||||
)
|
||||
|
||||
// Chatter is a function-handling struct for package chatter.
|
||||
type Chatter struct{}
|
||||
|
||||
// New is an initialization function for appcontext
|
||||
func New(ac *appcontext.Context) {
|
||||
c = ac
|
||||
ct := &Chatter{}
|
||||
c.RegisterChatterInterface(chatterinterface.ChatterInterface(ct))
|
||||
}
|
||||
|
||||
// Init is a initialization function for package
|
||||
func (ct *Chatter) Init() {
|
||||
c.Log.Info("Initializing Chatter...")
|
||||
}
|
129
lib/chatter/getters.go
Normal file
129
lib/chatter/getters.go
Normal file
@@ -0,0 +1,129 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package chatter
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (ct *Chatter) getAllGroupChatsWithSquads() ([]dbmapping.ChatSquad, bool) {
|
||||
chatsSquads := []dbmapping.ChatSquad{}
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Select(&groupChats, "SELECT * FROM chats WHERE chat_type IN ('group', 'supergroup')")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return chatsSquads, false
|
||||
}
|
||||
|
||||
for i := range groupChats {
|
||||
chatSquad := dbmapping.ChatSquad{}
|
||||
squad := dbmapping.Squad{}
|
||||
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id=?"), groupChats[i].ID)
|
||||
if err != nil {
|
||||
c.Log.Debug(err)
|
||||
} else {
|
||||
chatSquad.ChatRole = "squad"
|
||||
}
|
||||
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE flood_chat_id=?"), groupChats[i].ID)
|
||||
if err != nil {
|
||||
c.Log.Debug(err)
|
||||
} else {
|
||||
chatSquad.ChatRole = "flood"
|
||||
}
|
||||
|
||||
chatSquad.Squad = squad
|
||||
chatSquad.Chat = groupChats[i]
|
||||
|
||||
chatsSquads = append(chatsSquads, chatSquad)
|
||||
}
|
||||
|
||||
return chatsSquads, true
|
||||
}
|
||||
|
||||
// GetChatByID returns dbmapping.Chat instance with given ID.
|
||||
func (ct *Chatter) GetChatByID(chatID int64) (dbmapping.Chat, bool) {
|
||||
chatRaw := dbmapping.Chat{}
|
||||
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), chatID)
|
||||
if err != nil {
|
||||
c.Log.Error(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 (ct *Chatter) GetOrCreateChat(telegramUpdate *tgbotapi.Update) (dbmapping.Chat, bool) {
|
||||
chatRaw := dbmapping.Chat{}
|
||||
c.Log.Debug("TGID: ", telegramUpdate.Message.Chat.ID)
|
||||
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), telegramUpdate.Message.Chat.ID)
|
||||
if err != nil {
|
||||
c.Log.Error("Chat stream not found in database.")
|
||||
c.Log.Error(err.Error())
|
||||
|
||||
nameOfChat := ""
|
||||
if telegramUpdate.Message.Chat.FirstName != "" {
|
||||
nameOfChat += telegramUpdate.Message.Chat.FirstName
|
||||
}
|
||||
if telegramUpdate.Message.Chat.LastName != "" {
|
||||
nameOfChat += " " + telegramUpdate.Message.Chat.LastName
|
||||
}
|
||||
if telegramUpdate.Message.Chat.Title != "" {
|
||||
if nameOfChat != "" {
|
||||
nameOfChat += " [" + telegramUpdate.Message.Chat.Title + "]"
|
||||
} else {
|
||||
nameOfChat = telegramUpdate.Message.Chat.Title
|
||||
}
|
||||
}
|
||||
|
||||
chatRaw.Name = nameOfChat
|
||||
chatRaw.ChatType = telegramUpdate.Message.Chat.Type
|
||||
chatRaw.TelegramID = 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 {
|
||||
c.Log.Error(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 {
|
||||
c.Log.Error(err2)
|
||||
return chatRaw, false
|
||||
}
|
||||
} else {
|
||||
c.Log.Info("Chat stream found in database.")
|
||||
}
|
||||
|
||||
return chatRaw, true
|
||||
}
|
||||
|
||||
// GetAllPrivateChats returns all private chats
|
||||
func (ct *Chatter) GetAllPrivateChats() ([]dbmapping.Chat, bool) {
|
||||
privateChats := []dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Select(&privateChats, "SELECT * FROM chats WHERE chat_type='private'")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return privateChats, false
|
||||
}
|
||||
|
||||
return privateChats, true
|
||||
}
|
||||
|
||||
// GetAllGroupChats returns all group chats
|
||||
func (ct *Chatter) GetAllGroupChats() ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
|
||||
err := c.Db.Select(&groupChats, "SELECT * FROM chats WHERE chat_type IN ('group', 'supergroup')")
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return groupChats, false
|
||||
}
|
||||
|
||||
return groupChats, true
|
||||
}
|
42
lib/chatter/responders.go
Normal file
42
lib/chatter/responders.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package chatter
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GroupsList lists all chats where bot exist
|
||||
func (ct *Chatter) GroupsList(update *tgbotapi.Update) string {
|
||||
groupChats, ok := ct.getAllGroupChatsWithSquads()
|
||||
if !ok {
|
||||
return "fail"
|
||||
}
|
||||
|
||||
message := "*Бот состоит в следующих групповых чатах:*\n"
|
||||
|
||||
for i := range groupChats {
|
||||
message += "---\n"
|
||||
message += "[#" + strconv.Itoa(groupChats[i].Chat.ID) + "] _" + groupChats[i].Chat.Name + "_\n"
|
||||
message += "Telegram ID: " + strconv.FormatInt(groupChats[i].Chat.TelegramID, 10) + "\n"
|
||||
if groupChats[i].ChatRole == "squad" {
|
||||
message += "Статистика отряда:\n"
|
||||
message += c.Squader.SquadStatictics(groupChats[i].Squad.ID)
|
||||
} else if groupChats[i].ChatRole == "flood" {
|
||||
message += "Является флудочатом отряда №" + strconv.Itoa(groupChats[i].Squad.ID) + "\n"
|
||||
} else {
|
||||
message += "Не является отрядом.\n"
|
||||
}
|
||||
}
|
||||
|
||||
message += "\nЧтобы создать отряд, введите команду /make\\_squad _X Y_, где _X_ — номер чата с пинами (в нём позволено писать лишь боту и командирам), а _Y_ — чат-флудилка для общения отряда."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
|
||||
return "ok"
|
||||
}
|
43
lib/chatter/updaters.go
Normal file
43
lib/chatter/updaters.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package chatter
|
||||
|
||||
import (
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
|
||||
)
|
||||
|
||||
// UpdateChatTitle updates chat title in database
|
||||
func (ct *Chatter) UpdateChatTitle(chatRaw *dbmapping.Chat, newTitle string) (*dbmapping.Chat, bool) {
|
||||
chatRaw.Name = newTitle
|
||||
_, err := c.Db.NamedExec("UPDATE chats SET name=:name WHERE id=:id", &chatRaw)
|
||||
if err != nil {
|
||||
c.Log.Error(err)
|
||||
return chatRaw, false
|
||||
}
|
||||
|
||||
return chatRaw, true
|
||||
}
|
||||
|
||||
// UpdateChatTelegramID updates chat's TelegramID when it converts to supergroup
|
||||
func (ct *Chatter) UpdateChatTelegramID(update *tgbotapi.Update) (*dbmapping.Chat, bool) {
|
||||
c.Log.Debug("Updating existing Telegram chat ID...")
|
||||
chatRaw := dbmapping.Chat{}
|
||||
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), update.Message.MigrateFromChatID)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return &chatRaw, false
|
||||
}
|
||||
if update.Message.SuperGroupChatCreated {
|
||||
chatRaw.ChatType = "supergroup"
|
||||
}
|
||||
chatRaw.TelegramID = update.Message.MigrateToChatID
|
||||
_, err = c.Db.NamedExec("UPDATE chats SET chat_type=:chat_type, telegram_id=:telegram_id WHERE id=:id", &chatRaw)
|
||||
if err != nil {
|
||||
c.Log.Error(err.Error())
|
||||
return &chatRaw, false
|
||||
}
|
||||
|
||||
return &chatRaw, true
|
||||
}
|
Reference in New Issue
Block a user