From ef2866e89b851add242e19e392dfedd203c60388 Mon Sep 17 00:00:00 2001 From: Amalvy Arthur Date: Mon, 31 Dec 2018 00:48:46 +0100 Subject: [PATCH] Added basic subscribe and unsubscribe --- alfred.go | 34 ++++++++------ commands/save.go | 42 ----------------- commands/subscribe.go | 106 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 57 deletions(-) delete mode 100644 commands/save.go create mode 100644 commands/subscribe.go diff --git a/alfred.go b/alfred.go index 6980b59..383d165 100644 --- a/alfred.go +++ b/alfred.go @@ -19,21 +19,25 @@ 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, - "/save": commands.Save, - "/getsaved": commands.GetSaved, + 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, } if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { diff --git a/commands/save.go b/commands/save.go deleted file mode 100644 index 914859d..0000000 --- a/commands/save.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -* @Author: Amalvy Arthur - */ - -package commands - -import ( - "log" - - "../shared" - tb "gopkg.in/tucnak/telebot.v2" -) - -func Save(m *tb.Message) { - if m.ReplyTo == nil { - shared.Bot.Send(m.Chat, "Please reply to a message to save it") - return - } - - defer shared.Bot.Send(m.Chat, "Message sauvegardé : "+m.ReplyTo.Text) - savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages") - if !exists { - log.Println("no messages yet") - messageList := []string{m.ReplyTo.Text} - shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList) - return - } - log.Println(append(savedMessages.([]string), m.ReplyTo.Text)) - shared.ChatData.Set(m.Chat.ID, "saved_messages", - append(savedMessages.([]string), m.ReplyTo.Text)) -} - -func GetSaved(m *tb.Message) { - if _, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages"); !exists { - shared.Bot.Send(m.Chat, "Aucun message sauvegardé") - } - shared.Bot.Send(m.Chat, "Some messages exists") - savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages") - for _, message := range savedMessages.([]string) { - shared.Bot.Send(m.Chat, "message : "+message) - } -} diff --git a/commands/subscribe.go b/commands/subscribe.go new file mode 100644 index 0000000..fd08195 --- /dev/null +++ b/commands/subscribe.go @@ -0,0 +1,106 @@ +/* +* @Author: Amalvy Arthur + */ + +// This module use a property named "subscribed_chats" in the shared Users structure. +// It consists of a list of chatID as a string, chatID are separated by ":" (unix PATH style) + +package commands + +import ( + "strconv" + "strings" + + "../shared" + tb "gopkg.in/tucnak/telebot.v2" +) + +// 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" { + shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + return + } + userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") + if !exists { + shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title) + shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10)) + return + } + splittedChats := strings.Split(userSubscribedChats, ":") + for _, chatID := range splittedChats { + if chatID == strconv.FormatInt(m.Chat.ID, 10) { + shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title) + return + } + } + shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title) + if len(userSubscribedChats) != 0 { + shared.Users.Set(m.Sender.Username, "subscribed_chats", userSubscribedChats+":"+strconv.FormatInt(m.Chat.ID, 10)) + } else { + shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10)) + } +} + +// 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" { + shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + 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") + return + } + splittedChats := strings.Split(userSubscribedChats, ":") + for i, chatID := range splittedChats { + if chatID == strconv.FormatInt(m.Chat.ID, 10) { + shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title) + splittedChats = append(splittedChats[:i], splittedChats[i+1:]...) + break + } + } + shared.Users.Set(m.Sender.Username, "subscribed_chats", strings.Join(splittedChats, ":")) +} + +func ListSubscribers(m *tb.Message) { + // if m.Chat.Type != "group" && m.Chat.Type != "supergroup" { + // shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + // return + // } +} + +func Publish(m *tb.Message) { + if m.ReplyTo == nil { + shared.Bot.Send(m.Chat, "Please reply to a message to save it") + return + } + + defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) + savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages") + if !exists { + messageList := []string{m.ReplyTo.Text} + shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList) + return + } + shared.ChatData.Set(m.Chat.ID, "saved_messages", + append(savedMessages.([]string), m.ReplyTo.Text)) +} + +func Unpublish(m *tb.Message) { + +} + +func Retrieve(m *tb.Message) { + if _, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages"); !exists { + shared.Bot.Send(m.Chat, "Aucun message sauvegardé") + return + } + savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages") + for _, message := range savedMessages.([]string) { + shared.Bot.Send(m.Chat, "message : "+message) + } +}