Merge branch 'features' of ALFRED/ALFRED into master
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2018-07-24 13:25:25 +00:00 committed by Gitea
commit efa9b54ffa
5 changed files with 126 additions and 8 deletions

3
.gitignore vendored
View File

@ -15,4 +15,5 @@
.glide/
settings_custom.json
history.json
history.json
users.json

View File

@ -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:44:38
* @Last Modified time: 2018-07-24 15:21:53
*/
package main
@ -18,11 +18,13 @@ 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,
tb.OnText: commands.OnText,
"/hello": commands.Hello,
"/sponge": commands.Sponge,
"/git": commands.Git,
"/framapad": commands.Framapad,
"/setgender": commands.SetGender,
"/gender": commands.Gender,
}
if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil {
@ -32,6 +34,8 @@ func main() {
log.Println("Initialize history")
shared.InitHistory(int(settings.Settings["history size"].(float64)),
settings.Settings["history file"].(string))
log.Println("Initialize users infos")
shared.InitUsers(settings.Settings["users file"].(string))
log.Println("Bot initialisation")
b, err := tb.NewBot(tb.Settings{

30
commands/gender.go Normal file
View File

@ -0,0 +1,30 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 14:55:33
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 15:10:37
*/
package commands
import (
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
"strings"
)
func SetGender(m *tb.Message) {
split := strings.Split(m.Text, " ")
data := strings.Join(split[1:], " ")
shared.Users.Set(m.Sender.ID, "gender", data)
shared.Bot.Send(m.Chat, "Votre genre est enregistré, je vous considère maintenant comme « "+data+" ».")
}
func Gender(m *tb.Message) {
data, exists := shared.Users.Get(m.Sender.ID, "gender")
if !exists {
shared.Bot.Send(m.Chat, "Vous n'avez pas enregistré votre genre, je ne voudrais pas l'assumer.")
} else {
shared.Bot.Send(m.Chat, data)
}
}

View File

@ -1,5 +1,6 @@
{
"token": "INSERT TOKEN HERE",
"history size": 10,
"history file": "history.json"
"history file": "history.json",
"users file": "users.json"
}

82
shared/users.go Normal file
View File

@ -0,0 +1,82 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 14:41:03
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 15:23:06
*/
package shared
import (
"encoding/json"
"io/ioutil"
"sync"
)
type users struct {
mutex sync.Mutex
data map[int]map[string]string
}
type usersFile struct {
mutex sync.Mutex
path string
}
var Users users
var uf usersFile
func InitUsers(users_file_path string) {
uf = usersFile{}
uf.path = users_file_path
Users = users{}
Users.mutex.Lock()
defer Users.mutex.Unlock()
Users.data = make(map[int]map[string]string)
uf.read()
}
// Get an info about a given user
func (u users) Get(id int, key string) (string, bool) {
u.mutex.Lock()
defer u.mutex.Unlock()
user, exists := u.data[id]
if !exists {
return "", false
}
if _, exists = user[key]; !exists {
return "", false
}
return user[key], true
}
// Add an info about a given user
func (u users) Set(id int, key, data string) {
u.mutex.Lock()
defer u.mutex.Unlock()
if _, exists := u.data[id]; !exists {
u.data[id] = make(map[string]string)
}
u.data[id][key] = data
go uf.write()
}
func (u usersFile) read() {
u.mutex.Lock()
defer u.mutex.Unlock()
data, err := ioutil.ReadFile(u.path)
if err != nil {
// File doesn't exist, skip import
return
}
json.Unmarshal(data, &Users.data)
}
func (u usersFile) write() {
u.mutex.Lock()
defer u.mutex.Unlock()
Users.mutex.Lock()
defer Users.mutex.Unlock()
data, _ := json.Marshal(Users.data)
ioutil.WriteFile(u.path, data, 0770)
}