From dea2b4cfb44674466d0ec7263976ef436bc6caa8 Mon Sep 17 00:00:00 2001 From: Amalvy Arthur Date: Sun, 30 Dec 2018 19:31:56 +0100 Subject: [PATCH] Basic save feature --- .gitignore | 5 ++++- alfred.go | 3 +++ commands/save.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 commands/save.go diff --git a/.gitignore b/.gitignore index eb5b34a..9a15d12 100644 --- a/.gitignore +++ b/.gitignore @@ -11,10 +11,13 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +#swap files +*~ + # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ settings_custom.json history.json users.json -chat_data.json \ No newline at end of file +chat_data.json diff --git a/alfred.go b/alfred.go index a52b950..6980b59 100644 --- a/alfred.go +++ b/alfred.go @@ -32,6 +32,8 @@ func main() { "/chaos": commands.TwitterSJW, "/apero": commands.AperoTime, "/quote": commands.Quote, + "/save": commands.Save, + "/getsaved": commands.GetSaved, } if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { @@ -61,6 +63,7 @@ func main() { b.Handle(key, value) } + log.Println("token : " + settings.Settings["token"].(string)) log.Println("Starting bot") b.Start() } diff --git a/commands/save.go b/commands/save.go new file mode 100644 index 0000000..914859d --- /dev/null +++ b/commands/save.go @@ -0,0 +1,42 @@ +/* +* @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) + } +}