Add auto push notification on users when publish is used

This commit is contained in:
Antoine Bartuccio 2019-01-02 23:52:01 +01:00
parent dd7a39a158
commit a8d8b6d69e
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 166 additions and 26 deletions

View File

@ -1,8 +1,8 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-23 15:24:22
* @Last Modified by: klmp200
* @Last Modified time: 2018-11-14 00:33:00
* @Last Modified by: Bartuccio Antoine
* @Last Modified time: 2019-01-02 22:58:03
*/
package main
@ -19,25 +19,28 @@ import (
func main() {
registeredCommands := map[string]func(*tb.Message){
tb.OnText: commands.OnText,
"/hello": commands.Hello,
"/sponge": commands.Sponge,
"/git": commands.Git,
"/framapad": commands.Framapad,
"/setgender": commands.SetGender,
"/gender": commands.Gender,
"/roll": commands.Dice,
"/trump": commands.LastTrumpTweet,
"/trends": commands.TwitterTrends,
"/chaos": commands.TwitterSJW,
"/apero": commands.AperoTime,
"/quote": commands.Quote,
"/subscribe": commands.Subscribe,
"/unsubscribe": commands.Unsubscribe,
"/listsubscribers": commands.ListSubscribers,
"/publish": commands.Publish,
"/unpublish": commands.Unpublish,
"/retrieve": commands.Retrieve,
tb.OnText: commands.OnText,
"/registerprivate": commands.RegisterPrivate,
"/unregisterprivate": commands.UnRegisterPrivate,
"/hello": commands.Hello,
"/sponge": commands.Sponge,
"/git": commands.Git,
"/framapad": commands.Framapad,
"/setgender": commands.SetGender,
"/gender": commands.Gender,
"/roll": commands.Dice,
"/trump": commands.LastTrumpTweet,
"/trends": commands.TwitterTrends,
"/chaos": commands.TwitterSJW,
"/apero": commands.AperoTime,
"/quote": commands.Quote,
"/subscribe": commands.Subscribe,
"/unsubscribe": commands.Unsubscribe,
"/listsubscribers": commands.ListSubscribers,
"/publish": commands.Publish,
"/unpublish": commands.Unpublish,
"/retrieve": commands.Retrieve,
"/toggleupdates": commands.ToggleUpdates,
}
if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil {

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)
}
}
}

View File

@ -36,7 +36,7 @@ The publish module intend to be a "multi-pin" feature for chats, allowing users
### Publish
**Description** : Publish a message to Alfred for this group chat and sends this message via MP to every subscriber of the group chat. The message can then be retrieved using the *retrieve* command.
**Description** : Publish a message to Alfred for this group chat and sends this message via MP to every subscriber of the group chat if the have not mutted it. The message can then be retrieved using the *retrieve* command.
**Usage location** : Group chat only
@ -44,8 +44,6 @@ The publish module intend to be a "multi-pin" feature for chats, allowing users
**Command syntax** : /publish
*note* : MP not implemented
### Unpublish
**Description** : Remove a published message from Alfred for this group chat
@ -65,3 +63,12 @@ The publish module intend to be a "multi-pin" feature for chats, allowing users
**Command syntax** : /retrieve
### ToggleUpdates
**Description**: Activate/Deactivate automatic notifications from publish in the group chat where the command is used. By default, this is enabled.
**Usage location** : Group Chat
**Command syntax** : /toggleupdates
**note** : No notification can be send if the user has never done /registerprivate on a private chat with the bot, this is a limitation of the telegram bot API.

View File

@ -1,16 +1,19 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 14:41:03
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 17:49:51
* @Last Modified by: Bartuccio Antoine
* @Last Modified time: 2019-01-02 22:37:58
*/
package shared
import (
"encoding/json"
"fmt"
"io/ioutil"
"sync"
tb "gopkg.in/tucnak/telebot.v2"
)
type users struct {
@ -23,6 +26,7 @@ type usersFile struct {
path string
}
// Users shared user for commands
var Users users
var uf usersFile
@ -72,6 +76,31 @@ func (u users) GetUsernames() []string {
return usernames
}
// GetUserChat retrieve the chat of the user if registered
func (u users) GetUserChat(username string) (*tb.Chat, error) {
serializedChat, exists := u.Get(username, "private_chat")
if !exists {
return nil, fmt.Errorf("No private chat registered for %s", username)
}
chat := &tb.Chat{}
if json.Unmarshal([]byte(serializedChat), chat) != nil {
return nil, fmt.Errorf("Error while parsing chat for %s", username)
}
return chat, nil
}
// SetUserChat register a private chat for an user
func (u users) SetUserChat(username string, chat *tb.Chat) {
serializedChat, err := json.Marshal(chat)
if err != nil {
return
}
u.Set(username, "private_chat", string(serializedChat))
}
func (u usersFile) read() {
u.mutex.Lock()
defer u.mutex.Unlock()