From 7ffb5a4a2aaa99206f5342e9b7b86e1c272c003c Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Tue, 1 Jan 2019 23:37:25 +0100 Subject: [PATCH 1/3] Save message instead of message text for subscriptions --- commands/subscribe.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/commands/subscribe.go b/commands/subscribe.go index 48100b6..a11ab15 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -11,7 +11,6 @@ package commands import ( - "fmt" "strconv" "strings" @@ -95,14 +94,14 @@ func Publish(m *tb.Message) { defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") if !exists { - messageList := []string{m.ReplyTo.Text} + messageList := []tb.Message{*m.ReplyTo} shared.ChatData.Set(m.Chat.ID, "published_messages", messageList) return } shared.ChatData.Set( m.Chat.ID, "published_messages", - append(savedMessages.([]interface{}), m.ReplyTo.Text), + append(savedMessages.([]interface{}), *m.ReplyTo), ) } @@ -139,7 +138,7 @@ func Unpublish(m *tb.Message) { // Command syntax : /retrieve func Retrieve(m *tb.Message) { chatList := []int64{} - messageList := []string{} + messageList := []tb.Message{} if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") @@ -170,7 +169,7 @@ func Retrieve(m *tb.Message) { for _, chatID := range chatList { if savedMessages, exists := shared.ChatData.Get(chatID, "published_messages"); exists { for _, message := range savedMessages.([]interface{}) { - if message, ok := message.(string); ok { + if message, ok := message.(tb.Message); ok { messageList = append(messageList, message) } } @@ -183,8 +182,8 @@ func Retrieve(m *tb.Message) { } shared.Bot.Send(m.Chat, "--- Messages publiés ---") - for index, message := range messageList { - shared.Bot.Send(m.Chat, fmt.Sprintf("%d : %s", index, message)) + for _, message := range messageList { + shared.Bot.Forward(m.Chat, &message) } shared.Bot.Send(m.Chat, "--- Messages publiés ---") } From da0729d2341a207d1bba9c09290b01cca2e9d154 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 2 Jan 2019 00:58:19 +0100 Subject: [PATCH 2/3] Store messages for subscription feature as a real Message --- commands/subscribe.go | 121 ++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 41 deletions(-) diff --git a/commands/subscribe.go b/commands/subscribe.go index a11ab15..7a388df 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -11,6 +11,7 @@ package commands import ( + "encoding/json" "strconv" "strings" @@ -18,11 +19,39 @@ import ( tb "gopkg.in/tucnak/telebot.v2" ) +func getPublishedMessages(chatID int64) []tb.Message { + messages := []tb.Message{} + + serializedMessages, exists := shared.ChatData.Get(chatID, "published_messages") + if !exists { + return messages + } + + if _, ok := serializedMessages.(string); !ok { + return messages + } + + if json.Unmarshal([]byte(serializedMessages.(string)), &messages) != nil { + return []tb.Message{} + } + + return messages +} + +func setPublishedMessages(chatID int64, messages []tb.Message) { + data, err := json.Marshal(messages) + if err != nil { + return + } + + shared.ChatData.Set(chatID, "published_messages", string(data)) +} + // Subscribe user sending the command to the current chat // Command syntax : /subscribe func Subscribe(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") @@ -50,7 +79,7 @@ func Subscribe(m *tb.Message) { // Command syntax : /unsubscribe func Unsubscribe(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") @@ -73,7 +102,7 @@ func Unsubscribe(m *tb.Message) { // Command syntax : /listsubscribers func ListSubscribers(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } subscribers := m.Chat.Title + " subscribers : \n\n" @@ -84,52 +113,65 @@ func ListSubscribers(m *tb.Message) { } // Publish a message from the current chat -// Command syntax (while repying to a message) : /publish +// Command syntax (while replying to a message) : /publish func Publish(m *tb.Message) { if m.ReplyTo == nil { shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier") return } - defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) - savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") - if !exists { - messageList := []tb.Message{*m.ReplyTo} - shared.ChatData.Set(m.Chat.ID, "published_messages", messageList) + 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 } - shared.ChatData.Set( - m.Chat.ID, - "published_messages", - append(savedMessages.([]interface{}), *m.ReplyTo), - ) + + defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) + savedMessages := getPublishedMessages(m.Chat.ID) + setPublishedMessages(m.Chat.ID, append(savedMessages, *m.ReplyTo)) } // Unpublish remove a message from published messages in the current chat -// Command syntax : /unpublish [publication ID] +// Command syntax (while replying to a message) : /unpublish func Unpublish(m *tb.Message) { - parsedCommand := strings.Split(m.Text, " ") - if len(parsedCommand) < 2 { - shared.Bot.Send(m.Chat, "syntaxe : /unpublish [publication ID]") + + // This is not working at the moment + // This can't work because when retrieving, the newly send message + // has a different ID from the one stored so you can't detect that's + // it's in the database except if you find the original message + // which is very unlikely to happen + 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 } - index, err := strconv.Atoi(parsedCommand[1]) - if err != nil { - shared.Bot.Send(m.Chat, "ID de publication invalide") + + if m.ReplyTo == nil { + shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le dépublier") return } - savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") - if !exists || len(savedMessages.([]interface{})) == 0 { - shared.Bot.Send(m.Chat, "Aucun message publié") + + if !m.ReplyTo.IsForwarded() { + shared.Bot.Send(m.Chat, "Ce message ne peut pas avoir été publié") return } - if len(savedMessages.([]interface{})) <= index || index < 0 { - shared.Bot.Send(m.Chat, "Aucun message avec cet ID de publication n'existe") + + publishedMessages := getPublishedMessages(m.Chat.ID) + filteredPublishedMessages := []tb.Message{} + found := false + for _, message := range publishedMessages { + if message.ID == m.ReplyTo.ID { + found = true + continue + } + filteredPublishedMessages = append(filteredPublishedMessages, message) + } + + if !found { + shared.Bot.Send(m.Chat, "Ce message n'a jamais été publié") return } - savedMessages = append(savedMessages.([]interface{})[:index], savedMessages.([]interface{})[index+1:]...) - shared.ChatData.Set(m.Chat.ID, "published_messages", savedMessages) + + setPublishedMessages(m.Chat.ID, filteredPublishedMessages) shared.Bot.Send(m.Chat, "Message supprimé des publication") } @@ -138,7 +180,7 @@ func Unpublish(m *tb.Message) { // Command syntax : /retrieve func Retrieve(m *tb.Message) { chatList := []int64{} - messageList := []tb.Message{} + hasMessage := false if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") @@ -166,26 +208,23 @@ func Retrieve(m *tb.Message) { chatList = append(chatList, m.Chat.ID) } + shared.Bot.Send(m.Chat, "--- Messages publiés ---") + for _, chatID := range chatList { - if savedMessages, exists := shared.ChatData.Get(chatID, "published_messages"); exists { - for _, message := range savedMessages.([]interface{}) { - if message, ok := message.(tb.Message); ok { - messageList = append(messageList, message) - } - } + messages := getPublishedMessages(chatID) + if len(messages) > 0 { + hasMessage = true + } + for _, message := range messages { + shared.Bot.Forward(m.Chat, &message) } } - if len(messageList) == 0 { + if !hasMessage { shared.Bot.Send(m.Chat, "Aucun message publié") - return } shared.Bot.Send(m.Chat, "--- Messages publiés ---") - for _, message := range messageList { - shared.Bot.Forward(m.Chat, &message) - } - shared.Bot.Send(m.Chat, "--- Messages publiés ---") } // Get all users subscribed to the provided channel From 0acdcb6351f8fff9f9649dfa7977737849272506 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 2 Jan 2019 02:21:15 +0100 Subject: [PATCH 3/3] Best effort to provide a unpublish option --- commands/subscribe.go | 30 +++++++++++++++++++++++------- doc/publish.md | 4 +++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/commands/subscribe.go b/commands/subscribe.go index 7a388df..9bd3600 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -1,5 +1,5 @@ /* -* @Author: Amalvy Arthur +* @Authors: Amalvy Arthur, Bartuccio Antoine */ // This module uses a property named "subscribed_chats" in the shared Users structure. @@ -54,6 +54,12 @@ func Subscribe(m *tb.Message) { 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 + } + userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") if !exists { shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title) @@ -82,6 +88,12 @@ func Unsubscribe(m *tb.Message) { 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 + } + userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") if !exists || len(userSubscribedChats) == 0 { shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat") @@ -135,11 +147,6 @@ func Publish(m *tb.Message) { // Command syntax (while replying to a message) : /unpublish func Unpublish(m *tb.Message) { - // This is not working at the moment - // This can't work because when retrieving, the newly send message - // has a different ID from the one stored so you can't detect that's - // it's in the database except if you find the original message - // which is very unlikely to happen 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 @@ -159,7 +166,16 @@ func Unpublish(m *tb.Message) { filteredPublishedMessages := []tb.Message{} found := false for _, message := range publishedMessages { - if message.ID == m.ReplyTo.ID { + // You can't just compare messages id because + // when retrieving, the newly send message + // has a different ID from the one stored so you can't detect that's + // it's in the database except if you find the original message + // which is very unlikely to happen + // As a workaround, we check the original sender, original unix time + // and associated text + if message.Sender.ID == m.ReplyTo.OriginalSender.ID && + message.Unixtime == int64(m.ReplyTo.OriginalUnixtime) && + message.Text == m.ReplyTo.Text { found = true continue } diff --git a/doc/publish.md b/doc/publish.md index 84e4e4d..3862db4 100644 --- a/doc/publish.md +++ b/doc/publish.md @@ -52,7 +52,9 @@ The publish module intend to be a "multi-pin" feature for chats, allowing users **Usage location** : Group chat only -**Command syntax** : /unpublish [publication ID] +**usage conditions** : Reply to the message to unpublish + +**Command syntax** : /unpublish ### Retrieve