Store messages for subscription feature as a real Message
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2019-01-02 00:58:19 +01:00
parent 7ffb5a4a2a
commit da0729d234
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 80 additions and 41 deletions

View File

@ -11,6 +11,7 @@
package commands package commands
import ( import (
"encoding/json"
"strconv" "strconv"
"strings" "strings"
@ -18,11 +19,39 @@ import (
tb "gopkg.in/tucnak/telebot.v2" 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 // Subscribe user sending the command to the current chat
// Command syntax : /subscribe // Command syntax : /subscribe
func Subscribe(m *tb.Message) { func Subscribe(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 return
} }
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
@ -50,7 +79,7 @@ func Subscribe(m *tb.Message) {
// Command syntax : /unsubscribe // Command syntax : /unsubscribe
func Unsubscribe(m *tb.Message) { func Unsubscribe(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 return
} }
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
@ -73,7 +102,7 @@ func Unsubscribe(m *tb.Message) {
// Command syntax : /listsubscribers // Command syntax : /listsubscribers
func ListSubscribers(m *tb.Message) { func ListSubscribers(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 return
} }
subscribers := m.Chat.Title + " subscribers : \n\n" subscribers := m.Chat.Title + " subscribers : \n\n"
@ -84,52 +113,65 @@ func ListSubscribers(m *tb.Message) {
} }
// Publish a message from the current chat // 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) { func Publish(m *tb.Message) {
if m.ReplyTo == nil { if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier") shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier")
return return
} }
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup {
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
if !exists {
messageList := []tb.Message{*m.ReplyTo}
shared.ChatData.Set(m.Chat.ID, "published_messages", messageList)
return return
} }
shared.ChatData.Set(
m.Chat.ID, defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
"published_messages", savedMessages := getPublishedMessages(m.Chat.ID)
append(savedMessages.([]interface{}), *m.ReplyTo), setPublishedMessages(m.Chat.ID, append(savedMessages, *m.ReplyTo))
)
} }
// Unpublish remove a message from published messages in the current chat // 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) { func Unpublish(m *tb.Message) {
parsedCommand := strings.Split(m.Text, " ")
if len(parsedCommand) < 2 { // This is not working at the moment
shared.Bot.Send(m.Chat, "syntaxe : /unpublish [publication ID]") // 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 return
} }
index, err := strconv.Atoi(parsedCommand[1])
if err != nil { if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "ID de publication invalide") shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le dépublier")
return return
} }
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
if !exists || len(savedMessages.([]interface{})) == 0 { if !m.ReplyTo.IsForwarded() {
shared.Bot.Send(m.Chat, "Aucun message publié") shared.Bot.Send(m.Chat, "Ce message ne peut pas avoir été publié")
return 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 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") shared.Bot.Send(m.Chat, "Message supprimé des publication")
} }
@ -138,7 +180,7 @@ func Unpublish(m *tb.Message) {
// Command syntax : /retrieve // Command syntax : /retrieve
func Retrieve(m *tb.Message) { func Retrieve(m *tb.Message) {
chatList := []int64{} chatList := []int64{}
messageList := []tb.Message{} hasMessage := false
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate { 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") 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) chatList = append(chatList, m.Chat.ID)
} }
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for _, chatID := range chatList { for _, chatID := range chatList {
if savedMessages, exists := shared.ChatData.Get(chatID, "published_messages"); exists { messages := getPublishedMessages(chatID)
for _, message := range savedMessages.([]interface{}) { if len(messages) > 0 {
if message, ok := message.(tb.Message); ok { hasMessage = true
messageList = append(messageList, message) }
} for _, message := range messages {
} shared.Bot.Forward(m.Chat, &message)
} }
} }
if len(messageList) == 0 { if !hasMessage {
shared.Bot.Send(m.Chat, "Aucun message publié") shared.Bot.Send(m.Chat, "Aucun message publié")
return
} }
shared.Bot.Send(m.Chat, "--- Messages publiés ---") 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 // Get all users subscribed to the provided channel