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