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)