Simplify Retrieve function

This commit is contained in:
Antoine Bartuccio 2018-12-31 19:59:36 +01:00
parent a47c016c62
commit e9b3dabd10
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 46 additions and 26 deletions

View File

@ -22,7 +22,7 @@ import (
// Subscribe user sending the command to the current chat
// Command syntax : /subscribe
func Subscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
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")
return
}
@ -50,7 +50,7 @@ func Subscribe(m *tb.Message) {
// Unsubscribe user sending the command from the current chat
// Command syntax : /unsubscribe
func Unsubscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
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")
return
}
@ -73,7 +73,7 @@ func Unsubscribe(m *tb.Message) {
// ListSubscribers List all subscribers of the current chat
// Command syntax : /listsubscribers
func ListSubscribers(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
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")
return
}
@ -99,8 +99,11 @@ func Publish(m *tb.Message) {
shared.ChatData.Set(m.Chat.ID, "published_messages", messageList)
return
}
shared.ChatData.Set(m.Chat.ID, "published_messages",
append(savedMessages.([]interface{}), m.ReplyTo.Text))
shared.ChatData.Set(
m.Chat.ID,
"published_messages",
append(savedMessages.([]interface{}), m.ReplyTo.Text),
)
}
@ -135,38 +138,55 @@ func Unpublish(m *tb.Message) {
// If performed in Group Chat : retrieved all published messages for this chat
// Command syntax : /retrieve
func Retrieve(m *tb.Message) {
if m.Chat.Type == "group" || m.Chat.Type == "supergroup" {
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
fmt.Println(savedMessages)
if !exists || len(savedMessages.([]interface{})) == 0 {
shared.Bot.Send(m.Chat, "Aucun message publié")
return
}
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for index, message := range savedMessages.([]interface{}) {
shared.Bot.Send(m.Chat, strconv.Itoa(index)+" : "+message.(string))
}
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
chatList := []int64{}
messageList := []string{}
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")
return
}
if m.Chat.Type == "private" {
if m.Chat.Type == tb.ChatPrivate {
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists || len(userSubscribedChats) == 0 {
shared.Bot.Send(m.Chat, "Aucun abonnement")
shared.Bot.Send(m.Chat, "Aucun abonnements")
return
}
for _, chat := range strings.Split(userSubscribedChats, ":") {
chatID, _ := strconv.ParseInt(chat, 10, 64)
savedMessages, _ := shared.ChatData.Get(chatID, "published_messages")
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for index, message := range savedMessages.([]interface{}) {
shared.Bot.Send(m.Chat, strconv.Itoa(index)+" : "+message.(string))
chatID, err := strconv.ParseInt(chat, 10, 64)
if err != nil {
shared.Bot.Send(m.Chat, "Erreur lors de la récupération de vos évènements")
return
}
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
chatList = append(chatList, chatID)
}
}
if m.Chat.Type == tb.ChatGroup || m.Chat.Type == tb.ChatSuperGroup {
chatList = append(chatList, m.Chat.ID)
}
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 {
messageList = append(messageList, message)
}
}
}
}
if len(messageList) == 0 {
shared.Bot.Send(m.Chat, "Aucun message publié")
return
}
shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for index, message := range messageList {
shared.Bot.Send(m.Chat, fmt.Sprintf("%d : %s", index, message))
}
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
}
// Get all users subscribed to the provided channel