Archived
1

/send_all feature for administrators

This commit is contained in:
Vladimir Hodakov 2017-10-22 14:04:14 +04:00
parent 88c9853c77
commit 7975ea54c1
7 changed files with 95 additions and 4 deletions

View File

@ -54,3 +54,16 @@ func (g *Getters) GetOrCreateChat(telegramUpdate *tgbotapi.Update) (dbmapping.Ch
return chatRaw, true
}
// GetAllPrivateChats returns all private chats
func (g *Getters) GetAllPrivateChats() ([]dbmapping.Chat, bool) {
privateChats := []dbmapping.Chat{}
err := c.Db.Select(&privateChats, "SELECT * FROM chats WHERE chat_type='private'")
if err != nil {
log.Println(err)
return privateChats, false
}
return privateChats, true
}

View File

@ -15,8 +15,10 @@ type GettersInterface interface {
Init()
GetOrCreateChat(update *tgbotapi.Update) (dbmapping.Chat, bool)
GetChatByID(chatID int) (dbmapping.Chat, bool)
GetAllPrivateChats() ([]dbmapping.Chat, bool)
GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool)
GetPlayerByID(playerID int) (dbmapping.Player, bool)
PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool
GetProfile(playerID int) (dbmapping.Profile, bool)
GetPokememes() ([]dbmapping.PokememeFull, bool)
GetBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool)

View File

@ -50,3 +50,21 @@ func (g *Getters) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) {
return playerRaw, true
}
// PlayerBetterThan return true, if profile is more or equal powerful than
// provided power level
func (g *Getters) PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool {
var isBetter = false
switch playerRaw.Status {
case "owner":
isBetter = true
case "admin":
if powerLevel != "owner" {
isBetter = true
}
default:
isBetter = false
}
return isBetter
}

View File

@ -48,6 +48,9 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
var meMsg = regexp.MustCompile("/me\\z")
var bestMsg = regexp.MustCompile("/best\\z")
// Owner commands
var sendAllMsg = regexp.MustCompile("/send_all(.+)")
// Forwards
var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)")
var profileMsg = regexp.MustCompile(`(Онлайн: )(\d+)\n(Турнир через)(.+)\n\n(.*)\n(Элементы)(.+)\n(.*)\n\n(.+)(Уровень)(.+)\n`)
@ -97,7 +100,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
}
// Help
case helpMsg.MatchString(text):
c.Talkers.HelpMessage(update)
c.Talkers.HelpMessage(update, &playerRaw)
// Pokememes info
case pokedexMsg.MatchString(text):
if strings.HasSuffix(text, "1") {
@ -125,6 +128,13 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string {
// Suggestions
case bestMsg.MatchString(text):
c.Talkers.BestPokememesList(update, playerRaw)
// Admin commands
case sendAllMsg.MatchString(text):
if c.Getters.PlayerBetterThan(&playerRaw, "admin") {
c.Talkers.AdminBroadcastMessage(update)
} else {
c.Talkers.AnyMessageUnauthorized(update)
}
// Easter eggs
case huMsg.MatchString(text):
c.Talkers.MatMessage(update)

41
lib/talkers/broadcast.go Normal file
View File

@ -0,0 +1,41 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package talkers
import ( // stdlib
// 3rd party
"strings"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// AdminBroadcastMessage sends message to all private chats with bot
func (t *Talkers) AdminBroadcastMessage(update tgbotapi.Update) string {
broadcastingMessageBody := strings.Replace(update.Message.Text, "/send_all", "", 1)
privateChats, ok := c.Getters.GetAllPrivateChats()
if !ok {
return "fail"
}
for i := range privateChats {
chat := privateChats[i]
broadcastingMessage := "*Привет, " + chat.Name + "!*\n\n"
broadcastingMessage += "*Важное сообщение от администратора " + update.Message.From.FirstName + " " + update.Message.From.LastName + "* (@" + update.Message.From.UserName + ")\n\n"
broadcastingMessage += broadcastingMessageBody
msg := tgbotapi.NewMessage(int64(chat.TelegramID), broadcastingMessage)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
}
message := "Сообщение всем отправлено. Надеюсь, пользователи бота за него тебя не убьют.\n"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
return "ok"
}

View File

@ -8,17 +8,22 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
// local
"../config"
"../dbmapping"
)
// HelpMessage gives user all available commands
func (t *Talkers) HelpMessage(update tgbotapi.Update) {
func (t *Talkers) HelpMessage(update tgbotapi.Update, playerRaw *dbmapping.Player) {
message := "*Бот Инстинкта Enchanched.*\n\n"
message += "Текущая версия: *" + config.VERSION + "*\n\n"
message += "Список команд:\n\n"
message += "Список команд\n\n"
message += "+ /me посмотреть свой сохраненный профиль в боте\n"
message += "+ /best посмотреть лучших покемонов для поимки\n"
message += "+ /pokedeks получить список известных боту покемемов\n"
if c.Getters.PlayerBetterThan(playerRaw, "admin") {
message += "+ /send\\_all _текст_ — отправить сообщение всем пользователям бота\n"
}
message += "+ /help выводит данное сообщение\n"
message += "\n\n"
message += "Связаться с автором: @fat0troll\n"

View File

@ -15,7 +15,7 @@ type TalkersInterface interface {
Init()
HelloMessageUnauthorized(update tgbotapi.Update)
HelloMessageAuthorized(update tgbotapi.Update, playerRaw dbmapping.Player)
HelpMessage(update tgbotapi.Update)
HelpMessage(update tgbotapi.Update, playerRaw *dbmapping.Player)
PokememesList(update tgbotapi.Update, page int)
PokememeInfo(update tgbotapi.Update, playerRaw dbmapping.Player) string
BestPokememesList(update tgbotapi.Update, playerRaw dbmapping.Player) string
@ -30,6 +30,8 @@ type TalkersInterface interface {
AnyMessageUnauthorized(update tgbotapi.Update)
GetterError(update tgbotapi.Update)
AdminBroadcastMessage(update tgbotapi.Update) string
DurakMessage(update tgbotapi.Update)
MatMessage(update tgbotapi.Update)
}