Add auto push notification on users when publish is used

This commit is contained in:
Bartuccio Antoine 2019-01-02 23:52:01 +01:00
parent dd7a39a158
commit a8d8b6d69e
No known key found for this signature in database
GPG key ID: E7245548C53F904B
5 changed files with 166 additions and 26 deletions

View file

@ -0,0 +1,43 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2019-01-02 22:46:05
* @Last Modified by: Bartuccio Antoine
* @Last Modified time: 2019-01-02 22:53:48
*/
package commands
import (
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
)
// RegisterPrivate registers an user private chat
func RegisterPrivate(m *tb.Message) {
if m.Chat.Type != tb.ChatPrivate {
shared.Bot.Send(m.Chat, "Cette commande n'est disponnible qu'en messages privés")
}
if m.Sender.Username == "" {
shared.Bot.Send(m.Chat, "Vous devez avoir enregistré un username")
}
shared.Users.SetUserChat(m.Sender.Username, m.Chat)
shared.Bot.Send(m.Chat, "Votre chat privé a bien été enregistré")
}
// UnRegisterPrivate delete an user private chat
func UnRegisterPrivate(m *tb.Message) {
if m.Chat.Type != tb.ChatPrivate {
shared.Bot.Send(m.Chat, "Cette commande n'est disponnible qu'en messages privés")
}
if m.Sender.Username == "" {
shared.Bot.Send(m.Chat, "Vous devez avoir enregistré un username")
}
shared.Users.SetUserChat(m.Sender.Username, nil)
shared.Bot.Send(m.Chat, "Votre chat privé a bien été supprimé")
}

View file

@ -223,6 +223,7 @@ func Publish(m *tb.Message) {
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
savedMessages := getPublishedMessages(m.Chat.ID)
setPublishedMessages(m.Chat.ID, append(savedMessages, *m.ReplyTo))
go pushUpdates(m.Chat.ID, m.ReplyTo)
}
@ -319,6 +320,47 @@ func Retrieve(m *tb.Message) {
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
}
// ToggleUpdates activate/deactivate automatic updates from the chat it's emmited
// Command syntax : /toggleupdates
func ToggleUpdates(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup {
shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
return
}
if m.Sender.Username == "" {
shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction")
return
}
wasSuscribed := false
subAutoNotify := false
subscriptions := getUserSubscribedChats(m.Sender.Username)
for i, sub := range subscriptions {
if sub.ChatID == m.Chat.ID {
sub.AutoNotify = !sub.AutoNotify
subAutoNotify = sub.AutoNotify
subscriptions[i] = sub
wasSuscribed = true
break
}
}
if !wasSuscribed {
shared.Bot.Send(m.Chat, "Vous n'êtes pas abonné au chat : "+m.Chat.Title)
return
}
setUserSubscribedChats(m.Sender.Username, subscriptions)
if subAutoNotify {
shared.Bot.Send(m.Chat, "Les notifications automatiques sont désormais activées pour ce chat")
return
}
shared.Bot.Send(m.Chat, "Les notifications automatiques sont désormais désactivées pour ce chat")
}
// Get all users subscribed to the provided channel
func getSubscribers(chatID int64) []string {
var subscribers []string
@ -333,3 +375,19 @@ func getSubscribers(chatID int64) []string {
}
return subscribers
}
func pushUpdates(chatID int64, message *tb.Message) {
for _, username := range shared.Users.GetUsernames() {
for _, sub := range getUserSubscribedChats(username) {
if sub.ChatID != chatID || !sub.AutoNotify {
continue
}
chat, err := shared.Users.GetUserChat(username)
if err != nil {
continue
}
shared.Bot.Forward(chat, message)
}
}
}