forked from git.klmp200.net/ALFRED
Persistant history across reboots
This commit is contained in:
parent
7336770332
commit
85b52e23e5
1
.gitignore
vendored
1
.gitignore
vendored
@ -15,3 +15,4 @@
|
|||||||
.glide/
|
.glide/
|
||||||
|
|
||||||
settings_custom.json
|
settings_custom.json
|
||||||
|
history.json
|
@ -2,7 +2,7 @@
|
|||||||
* @Author: Bartuccio Antoine
|
* @Author: Bartuccio Antoine
|
||||||
* @Date: 2018-07-23 15:24:22
|
* @Date: 2018-07-23 15:24:22
|
||||||
* @Last Modified by: klmp200
|
* @Last Modified by: klmp200
|
||||||
* @Last Modified time: 2018-07-24 12:35:08
|
* @Last Modified time: 2018-07-24 12:44:38
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
@ -30,7 +30,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Initialize history")
|
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")
|
log.Println("Bot initialisation")
|
||||||
b, err := tb.NewBot(tb.Settings{
|
b, err := tb.NewBot(tb.Settings{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"token": "INSERT TOKEN HERE",
|
"token": "INSERT TOKEN HERE",
|
||||||
"history size": 10
|
"history size": 10,
|
||||||
|
"history file": "history.json"
|
||||||
}
|
}
|
@ -2,12 +2,14 @@
|
|||||||
* @Author: Bartuccio Antoine
|
* @Author: Bartuccio Antoine
|
||||||
* @Date: 2018-07-24 01:27:11
|
* @Date: 2018-07-24 01:27:11
|
||||||
* @Last Modified by: klmp200
|
* @Last Modified by: klmp200
|
||||||
* @Last Modified time: 2018-07-24 11:51:52
|
* @Last Modified time: 2018-07-24 12:54:21
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package shared
|
package shared
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,15 +19,24 @@ type history struct {
|
|||||||
data map[int64][]string
|
data map[int64][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type historyFile struct {
|
||||||
|
mutex sync.Mutex
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
var History history
|
var History history
|
||||||
|
var hf historyFile
|
||||||
|
|
||||||
// Init a chat history of a given size
|
// 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 = history{}
|
||||||
History.mutex.Lock()
|
History.mutex.Lock()
|
||||||
defer History.mutex.Unlock()
|
defer History.mutex.Unlock()
|
||||||
History.size = size
|
History.size = size
|
||||||
History.data = make(map[int64][]string)
|
History.data = make(map[int64][]string)
|
||||||
|
hf.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the number of messages saved in the history
|
// 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[i] = val
|
||||||
}
|
}
|
||||||
array[h.size-1] = m
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user