Archived
1

Pins to supergroups, managed by admins

This commit is contained in:
2017-11-14 03:44:21 +04:00
parent 5c08899d25
commit 95a9a2146a
84 changed files with 786 additions and 622 deletions

28
lib/pinner/exported.go Normal file
View File

@@ -0,0 +1,28 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package pinner
import (
"lab.pztrn.name/fat0troll/i2_bot/lib/appcontext"
"lab.pztrn.name/fat0troll/i2_bot/lib/pinner/pinnerinterface"
)
var (
c *appcontext.Context
)
// Pinner is a function-handling struct for Pinner
type Pinner struct{}
// New is a appcontext initialization function
func New(ac *appcontext.Context) {
c = ac
p := &Pinner{}
c.RegisterPinnerInterface(pinnerinterface.PinnerInterface(p))
}
// Init is an initialization function for pinner
func (p *Pinner) Init() {
c.Log.Info("Initializing Pinner...")
}

71
lib/pinner/pinner.go Normal file
View File

@@ -0,0 +1,71 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package pinner
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// PinMessageToAllChats pins message to all groups where bot exist
func (p *Pinner) PinMessageToAllChats(update *tgbotapi.Update) string {
messageToPin := update.Message.CommandArguments()
if messageToPin == "" {
return "fail"
}
groupChats, ok := c.Getters.GetAllGroupChats()
if !ok {
return "fail"
}
for i := range groupChats {
if groupChats[i].ChatType == "supergroup" {
message := messageToPin + "\n\n"
message += "© " + update.Message.From.FirstName + " " + update.Message.From.LastName
message += " (@" + update.Message.From.UserName + ")"
msg := tgbotapi.NewMessage(groupChats[i].TelegramID, message)
msg.ParseMode = "Markdown"
pinnableMessage, err := c.Bot.Send(msg)
if err != nil {
c.Log.Error(err.Error())
message := "*Ваше сообщение не отправлено.*\n\n"
message += "Обычно это связано с тем, что нарушена разметка Markdown. "
message += "К примеру, если вы хотели использовать нижнее\\_подчёркивание, то печатать его надо так — \\\\_. То же самое касается всех управляющих разметкой символов в Markdown в случае, если вы их хотите использовать как текст, а не как управляющий символ Markdown."
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
return "fail"
}
pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
ChatID: pinnableMessage.Chat.ID,
MessageID: pinnableMessage.MessageID,
DisableNotification: true,
}
_, err = c.Bot.PinChatMessage(pinChatMessageConfig)
if err != nil {
c.Log.Error(err.Error())
}
}
}
message := "*Ваше сообщение отправлено и запинено во все чаты, где сидит бот.*\n\n"
message += "Текст отправленного сообщения:\n\n"
message += messageToPin
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
return "ok"
}

View File

@@ -0,0 +1,14 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package pinnerinterface
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// PinnerInterface implements Pinner for importing via appcontex
type PinnerInterface interface {
Init()
PinMessageToAllChats(update *tgbotapi.Update) string
}