From f7f330837287b1e11b9bc2ace54980e74e056b94 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 24 Jul 2018 11:59:36 +0200 Subject: [PATCH 1/3] Begin commands package --- alfred.go | 18 ++++-------------- commands/sponge.go | 27 +++++++++++++++++++++++++++ shared/bot.go | 14 ++++++++++++++ shared/history.go | 9 ++++++++- 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 commands/sponge.go create mode 100644 shared/bot.go diff --git a/alfred.go b/alfred.go index 1933c92..bfd7df5 100644 --- a/alfred.go +++ b/alfred.go @@ -2,17 +2,17 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 03:05:51 +* @Last Modified time: 2018-07-24 11:58:31 */ package main import ( + "./commands" "./settings" "./shared" tb "gopkg.in/tucnak/telebot.v2" "log" - "strings" "time" ) @@ -33,6 +33,7 @@ func main() { log.Fatal(err) return } + shared.Bot = b b.Handle("/hello", func(m *tb.Message) { b.Send(m.Chat, "Bonjour "+m.Sender.Username) @@ -42,18 +43,7 @@ func main() { shared.History.AddMessage(m.Chat.ID, m.Text) }) - b.Handle("/sponge", func(m *tb.Message) { - message := "" - for i, char := range shared.History.LastMessage(m.Chat.ID) { - if i%2 == 0 { - message += strings.ToLower(string(char)) - } else { - message += strings.ToUpper(string(char)) - } - } - b.Send(m.Chat, message) - - }) + b.Handle("/sponge", commands.Sponge) b.Handle("/git", func(m *tb.Message) { b.Send(m.Chat, "Mon code est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)") diff --git a/commands/sponge.go b/commands/sponge.go new file mode 100644 index 0000000..009ba5c --- /dev/null +++ b/commands/sponge.go @@ -0,0 +1,27 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 11:52:11 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 11:58:42 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" + "strings" +) + +func Sponge(m *tb.Message) { + message := "" + for i, char := range shared.History.LastMessage(m.Chat.ID) { + if i%2 == 0 { + message += strings.ToLower(string(char)) + } else { + message += strings.ToUpper(string(char)) + } + } + shared.Bot.Send(m.Chat, message) + +} diff --git a/shared/bot.go b/shared/bot.go new file mode 100644 index 0000000..9880987 --- /dev/null +++ b/shared/bot.go @@ -0,0 +1,14 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 11:56:47 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 11:58:34 + */ + +package shared + +import ( + tb "gopkg.in/tucnak/telebot.v2" +) + +var Bot *tb.Bot diff --git a/shared/history.go b/shared/history.go index 0dc315e..acf329b 100644 --- a/shared/history.go +++ b/shared/history.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 01:27:11 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 02:09:53 +* @Last Modified time: 2018-07-24 11:51:52 */ package shared @@ -19,6 +19,7 @@ type history struct { var History history +// Init a chat history of a given size func InitHistory(size int) { History = history{} History.mutex.Lock() @@ -27,12 +28,14 @@ func InitHistory(size int) { History.data = make(map[int64][]string) } +// Get the number of messages saved in the history func (h history) Size() int { h.mutex.Lock() defer h.mutex.Unlock() return h.size } +// Get a selected message in a chat history func (h history) Message(chatID int64, n int) string { h.mutex.Lock() defer h.mutex.Unlock() @@ -43,6 +46,7 @@ func (h history) Message(chatID int64, n int) string { return "" } +// Append a message to a given chat func (h history) AddMessage(chatID int64, m string) { h.mutex.Lock() defer h.mutex.Unlock() @@ -52,6 +56,7 @@ func (h history) AddMessage(chatID int64, m string) { h.append(chatID, m) } +// Get the last message of a given chat func (h history) LastMessage(chatID int64) string { h.mutex.Lock() defer h.mutex.Unlock() @@ -61,6 +66,7 @@ func (h history) LastMessage(chatID int64) string { return "" } +// Get a copy of a given chat history func (h history) ChatHistory(chatID int64) []string { h.mutex.Lock() defer h.mutex.Unlock() @@ -73,6 +79,7 @@ func (h history) ChatHistory(chatID int64) []string { return nil } +// Add a message at the end of a chat and move everithyng up // Assert that the slice exists and mutex already locked func (h history) append(chatID int64, m string) { c := make([]string, h.size-1, h.size-1) From 7336770332eacdde4ef7385bfc93d9db638a4a4e Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 24 Jul 2018 12:21:50 +0200 Subject: [PATCH 2/3] Externalized all commands in corresponding package --- alfred.go | 30 ++++++++++++------------------ commands/framapad.go | 17 +++++++++++++++++ commands/git.go | 17 +++++++++++++++++ commands/hello.go | 17 +++++++++++++++++ commands/on_text.go | 17 +++++++++++++++++ 5 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 commands/framapad.go create mode 100644 commands/git.go create mode 100644 commands/hello.go create mode 100644 commands/on_text.go diff --git a/alfred.go b/alfred.go index bfd7df5..75bcf9c 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 11:58:31 +* @Last Modified time: 2018-07-24 12:35:08 */ package main @@ -17,6 +17,14 @@ import ( ) func main() { + registered_commands := map[string]func(*tb.Message){ + tb.OnText: commands.OnText, + "/hello": commands.Hello, + "/sponge": commands.Sponge, + "/git": commands.Git, + "/framapad": commands.Framapad, + } + if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { log.Fatal(err) } @@ -35,23 +43,9 @@ func main() { } shared.Bot = b - b.Handle("/hello", func(m *tb.Message) { - b.Send(m.Chat, "Bonjour "+m.Sender.Username) - }) - - b.Handle(tb.OnText, func(m *tb.Message) { - shared.History.AddMessage(m.Chat.ID, m.Text) - }) - - b.Handle("/sponge", commands.Sponge) - - b.Handle("/git", func(m *tb.Message) { - b.Send(m.Chat, "Mon code est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)") - }) - - b.Handle("/framapad", func(m *tb.Message) { - b.Send(m.Chat, "Venez participer à mon développement en posant vos idées ici : https://mensuel.framapad.org/p/ALFRED2LERETOUR.") - }) + for key, value := range registered_commands { + b.Handle(key, value) + } log.Println("Starting bot") b.Start() diff --git a/commands/framapad.go b/commands/framapad.go new file mode 100644 index 0000000..31fc4c2 --- /dev/null +++ b/commands/framapad.go @@ -0,0 +1,17 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 12:11:26 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 12:12:58 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" +) + +func Framapad(m *tb.Message) { + shared.Bot.Send(m.Chat, "Venez participer à mon développement en posant vos idées ici : https://mensuel.framapad.org/p/ALFRED2LERETOUR.") +} diff --git a/commands/git.go b/commands/git.go new file mode 100644 index 0000000..29a9c4a --- /dev/null +++ b/commands/git.go @@ -0,0 +1,17 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 12:07:34 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 12:08:49 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" +) + +func Git(m *tb.Message) { + shared.Bot.Send(m.Chat, "Mon code source est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)") +} diff --git a/commands/hello.go b/commands/hello.go new file mode 100644 index 0000000..bbb0f70 --- /dev/null +++ b/commands/hello.go @@ -0,0 +1,17 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 12:05:45 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 12:06:39 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" +) + +func Hello(m *tb.Message) { + shared.Bot.Send(m.Chat, "Bonjour "+m.Sender.Username) +} diff --git a/commands/on_text.go b/commands/on_text.go new file mode 100644 index 0000000..c5db4a8 --- /dev/null +++ b/commands/on_text.go @@ -0,0 +1,17 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 12:09:37 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 12:10:26 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" +) + +func OnText(m *tb.Message) { + shared.History.AddMessage(m.Chat.ID, m.Text) +} From 85b52e23e5dee2f93a1ee131c9280ca6e2e0b8c9 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 24 Jul 2018 12:57:31 +0200 Subject: [PATCH 3/3] Persistant history across reboots --- .gitignore | 3 ++- alfred.go | 5 +++-- settings.json | 3 ++- shared/history.go | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b23330f..b868d05 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ -settings_custom.json \ No newline at end of file +settings_custom.json +history.json \ No newline at end of file diff --git a/alfred.go b/alfred.go index 75bcf9c..cbe1889 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:35:08 +* @Last Modified time: 2018-07-24 12:44:38 */ package main @@ -30,7 +30,8 @@ func main() { } log.Println("Initialize history") - shared.InitHistory(int(settings.Settings["history size"].(float64))) + shared.InitHistory(int(settings.Settings["history size"].(float64)), + settings.Settings["history file"].(string)) log.Println("Bot initialisation") b, err := tb.NewBot(tb.Settings{ diff --git a/settings.json b/settings.json index f6f81c4..587f67d 100644 --- a/settings.json +++ b/settings.json @@ -1,4 +1,5 @@ { "token": "INSERT TOKEN HERE", - "history size": 10 + "history size": 10, + "history file": "history.json" } \ No newline at end of file diff --git a/shared/history.go b/shared/history.go index acf329b..406392b 100644 --- a/shared/history.go +++ b/shared/history.go @@ -2,12 +2,14 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 01:27:11 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 11:51:52 +* @Last Modified time: 2018-07-24 12:54:21 */ package shared import ( + "encoding/json" + "io/ioutil" "sync" ) @@ -17,15 +19,24 @@ type history struct { data map[int64][]string } +type historyFile struct { + mutex sync.Mutex + path string +} + var History history +var hf historyFile // Init a chat history of a given size -func InitHistory(size int) { +func InitHistory(size int, history_file_path string) { + hf = historyFile{} + hf.path = history_file_path History = history{} History.mutex.Lock() defer History.mutex.Unlock() History.size = size History.data = make(map[int64][]string) + hf.read() } // Get the number of messages saved in the history @@ -89,4 +100,25 @@ func (h history) append(chatID int64, m string) { array[i] = val } array[h.size-1] = m + go hf.write() +} + +func (h historyFile) read() { + h.mutex.Lock() + defer h.mutex.Unlock() + data, err := ioutil.ReadFile(h.path) + if err != nil { + // File doesn't exist, skip import + return + } + json.Unmarshal(data, &History.data) +} + +func (h historyFile) write() { + h.mutex.Lock() + defer h.mutex.Unlock() + History.mutex.Lock() + defer History.mutex.Unlock() + data, _ := json.Marshal(History.data) + ioutil.WriteFile(h.path, data, 0770) }