parent
cb64d56c3f
commit
3dac9c01aa
10
alfred.go
10
alfred.go
@ -2,13 +2,14 @@
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-23 15:24:22
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-23 19:14:10
|
||||
* @Last Modified time: 2018-07-24 02:25:26
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"./settings"
|
||||
"./shared"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
"log"
|
||||
"time"
|
||||
@ -19,6 +20,9 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Initialize history")
|
||||
shared.InitHistory(int(settings.Settings["history size"].(float64)))
|
||||
|
||||
log.Println("Bot initialisation")
|
||||
b, err := tb.NewBot(tb.Settings{
|
||||
Token: settings.Settings["token"].(string),
|
||||
@ -33,6 +37,10 @@ func main() {
|
||||
b.Send(m.Chat, "Bonjour "+m.Sender.Username)
|
||||
})
|
||||
|
||||
b.Handle(tb.OnText, func(m *tb.Message) {
|
||||
shared.History.AddMessage(m.Chat.ID, m.Text)
|
||||
})
|
||||
|
||||
log.Println("Starting bot")
|
||||
b.Start()
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
{
|
||||
"token": "INSERT TOKEN HERE"
|
||||
"token": "INSERT TOKEN HERE",
|
||||
"history size": 10
|
||||
}
|
85
shared/history.go
Normal file
85
shared/history.go
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 01:27:11
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 02:09:53
|
||||
*/
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type history struct {
|
||||
mutex sync.Mutex
|
||||
size int
|
||||
data map[int64][]string
|
||||
}
|
||||
|
||||
var History history
|
||||
|
||||
func InitHistory(size int) {
|
||||
History = history{}
|
||||
History.mutex.Lock()
|
||||
defer History.mutex.Unlock()
|
||||
History.size = size
|
||||
History.data = make(map[int64][]string)
|
||||
}
|
||||
|
||||
func (h history) Size() int {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
return h.size
|
||||
}
|
||||
|
||||
func (h history) Message(chatID int64, n int) string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
array, exists := h.data[chatID]
|
||||
if exists && n >= 0 && n < len(array) {
|
||||
return array[n]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h history) AddMessage(chatID int64, m string) {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
if _, exists := h.data[chatID]; !exists {
|
||||
h.data[chatID] = make([]string, h.size, h.size)
|
||||
}
|
||||
h.append(chatID, m)
|
||||
}
|
||||
|
||||
func (h history) LastMessage(chatID int64) string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
if _, exists := h.data[chatID]; exists {
|
||||
return h.data[chatID][h.size-1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h history) ChatHistory(chatID int64) []string {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
array, exists := h.data[chatID]
|
||||
if exists {
|
||||
c := make([]string, h.size, h.size)
|
||||
copy(c, array)
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
array, _ := h.data[chatID]
|
||||
copy(c, array[1:])
|
||||
for i, val := range c {
|
||||
array[i] = val
|
||||
}
|
||||
array[h.size-1] = m
|
||||
}
|
Loading…
Reference in New Issue
Block a user