Archived
1

Pins to selected chats

Closes #6
This commit is contained in:
2017-11-26 08:31:56 +04:00
parent a6811f61fc
commit 8368a3c60b
7 changed files with 120 additions and 4 deletions

View File

@@ -26,5 +26,5 @@ func New(ac *appcontext.Context) {
func (p *Pinner) Init() {
c.Log.Info("Initializing Pinner...")
c.Cron.AddFunc("0 55 */2 * * *", p.PinBattleAlert)
c.Cron.AddFunc("0 55 4-23/2 * * *", p.PinBattleAlert)
}

View File

@@ -5,6 +5,8 @@ package pinner
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
// PinMessageToAllChats pins message to all groups where bot exist
@@ -70,6 +72,77 @@ func (p *Pinner) PinMessageToAllChats(update *tgbotapi.Update) string {
return "ok"
}
// PinMessageToSomeChats pins message to selected groups where bot exist
func (p *Pinner) PinMessageToSomeChats(update *tgbotapi.Update) string {
commandArgs := update.Message.CommandArguments()
commandArgsList := strings.Split(commandArgs, " ")
if len(commandArgsList) < 2 {
return "fail"
}
chatsToPin := commandArgsList[0]
messageToPin := commandArgsList[1]
if messageToPin == "" {
return "fail"
}
groupChats, ok := c.Chatter.GetGroupChatsByIDs(chatsToPin)
if !ok {
return "fail"
}
c.Log.Debug("Got " + strconv.Itoa(len(groupChats)) + " group chats...")
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"
}
// PinBattleAlert pins to all squads 'battle alert' at :55 of every even hour
// Even hours are in Moscow timezone
func (p *Pinner) PinBattleAlert() {

View File

@@ -12,5 +12,7 @@ type PinnerInterface interface {
Init()
PinBattleAlert()
PinMessageToSomeChats(update *tgbotapi.Update) string
PinMessageToAllChats(update *tgbotapi.Update) string
}