Further development

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-04-05 01:25:43 +02:00
parent fe1cd28327
commit c0bf3b2b36
10 changed files with 190 additions and 162 deletions

View File

@@ -67,27 +67,27 @@ func getBotToken() (string, error) {
return token, nil
}
func addBotInfo(botToken string, botUser string) error {
func addBotInfo(botUser string) error {
if redisClient == nil {
return ErrNilPointer
}
err := redisClient.HSet(botInfo, botToken, botUser).Err()
err := redisClient.Set(botInfo, botUser, 0).Err()
if err != nil {
log.Printf("Error in adding bot info: %v", err)
return ErrRedisAddHash
return ErrRedisAddString
}
return nil
}
func removeBotInfo(botToken string) error {
func removeBotInfo() error {
if redisClient == nil {
return ErrNilPointer
}
err := redisClient.HDel(botInfo, botToken).Err()
err := redisClient.Del(botInfo).Err()
if err != nil {
log.Printf("Error in removing bot info: %v", err)
return ErrRedisDelHash
return ErrRedisDelString
}
return nil
}

View File

@@ -143,7 +143,7 @@ func authorizeUser(userID int, authorize bool) error {
log.Printf("Error adding token to set: %v", err)
return ErrRedisAddSet
}
err = sendMsg(user, newAuthMsg, true)
err = sendMsgWithMenu(user, newAuthMsg, true)
if err != nil {
log.Printf("Error sending message to new authorized user: %v", err)
return ErrSendMsg

View File

@@ -37,6 +37,10 @@ var (
ErrRedisAddHash = errors.New("redis: couldn't add key in hash")
//ErrRedisDelHash is thrown when it's not possible to remove a key from a hash
ErrRedisDelHash = errors.New("redis: couldn't remove key from hash")
//ErrRedisAddString is thrown when it's not possible to add a string
ErrRedisAddString = errors.New("redis: couldn't add string")
//ErrRedisDelString is thrown when it's not possible to remove a string
ErrRedisDelString = errors.New("redis: couldn't remove string")
//ErrRedisRetrieveHash is thrown when it's not possible to retrieve a key from a hash
ErrRedisRetrieveHash = errors.New("redis: couldn't retrieve key from hash")
//ErrTokenParsing is thrown when it's not possible to parse the bot token

View File

@@ -21,7 +21,7 @@ const (
unstoppableMsg string = "Non ci siamo... Io l'ho nominata AMMINISTRATORE, cosa crede?! Questo ruolo esige impegno! Non può certo bloccarmi!"
wrongCmdMsg string = "Non capisco, si spieghi meglio! Per cortesia, basta basta! La prego! Non so di cosa sta parlando!"
authHowToMsg string = "Per autorizzare un utente invia un messaggio con scritto \n`/authUser ID_UTENTE`\n sostituendo `ID_UTENTE` con l'ID che ti é stato comunicato dall'utente da autorizzare"
deAuthHowToMsg string = "Per deautorizzare un utente invia un messaggio con scritto \n`/authUser USERNAME`\n sostituendo `USERNAME` con il nome utente da deautorizzare"
deAuthHowToMsg string = "Per deautorizzare un utente invia un messaggio con scritto \n`/deAuthUser ID_UTENTE`\n sostituendo `ID_UTENTE` con il nome utente da deautorizzare"
newAuthMsg string = "Benvenuto! Da ora in poi lei fa ufficialmente parte del magnifico *Coro dell'Università di Pisa*! Deve sentirsi onorato."
delAuthMsg string = "Capisco, quindi se ne sta andando... Beh un po' mi dispiace, devo ammetterlo. Se ripassa da queste parti sarà sempre il benvenuto! Arrivederci."
newAdminMsg string = "Beh allora, vediamo... Ah si, la nomino amministratore! Da grandi poteri derivano grandi responsabilità. Mi raccomando, non me ne faccia pentire!"
@@ -94,7 +94,7 @@ func botInit() error {
return ErrBotInit
}
err = addBotInfo(token, bot.Me.Username)
err = addBotInfo(bot.Me.Username)
if err != nil {
log.Printf("Error: bot %s info couldn't be added: %v", bot.Me.Username, err)
return ErrBotInit

View File

@@ -18,8 +18,10 @@ var genericCommands = map[string]bool{
"/prossimoEvento": true,
}
var authCommands = map[string]bool{
"/prossimaProva": true,
"/prossimaProvaSezione": true,
"/prossimaProvaInsieme": true,
"/ultimaMail": true,
}
var adminCommands = map[string]bool{
"/authUser": true,
@@ -87,27 +89,62 @@ func stopCmd(u *tb.User) {
}
}
func authUserCmd(u *tb.User, payload string) {
func authUserCmd(sender *tb.User, payload string) {
if payload == "" {
err := sendMsg(u, authHowToMsg, true)
err := sendMsgWithMenu(sender, authHowToMsg, true)
if err != nil {
log.Printf("Error in sending message: %v", err)
}
} else {
//TODO check if payload is valid ID
desc, err := getUserDescription(u)
userID, err := strconv.Atoi(payload)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
user, err := getUserInfo(userID)
if err != nil {
log.Printf("Error getting user info: %v", err)
return
}
desc, err := getUserDescription(user)
if err != nil {
log.Printf("Error retriving user description: %v", err)
}
userGroups, err := getUserGroups(user.ID)
if err != nil {
log.Printf("Error retriving user groups: %v", err)
}
menu := authUserMenu
menu[0][0].Data = payload
menu[0][1].Data = payload
menu[1][0].Data = payload
menu[1][1].Data = payload
menu[2][0].Data = payload
menu[2][1].Data = payload
menu[2][2].Data = payload
err = sendMsgWithSpecificMenu(u, "Stai per autorizzare il seguente utente:\n"+
menu[0][0].Data = strconv.Itoa(user.ID)
menu[0][1].Data = strconv.Itoa(user.ID)
menu[1][0].Data = strconv.Itoa(user.ID)
menu[1][1].Data = strconv.Itoa(user.ID)
menu[2][0].Data = strconv.Itoa(user.ID)
menu[2][1].Data = strconv.Itoa(user.ID)
menu[2][2].Data = strconv.Itoa(user.ID)
for _, group := range userGroups {
switch group {
case ugSoprano:
menu[0][0].Text = ""
case ugContralto:
menu[0][1].Text = ""
case ugTenore:
menu[1][0].Text = ""
case ugBasso:
menu[1][1].Text = ""
case ugCommissario:
menu[2][0].Text = ""
case ugReferente:
menu[2][1].Text = ""
case ugPreparatore:
menu[2][2].Text = ""
}
}
err = sendMsgWithSpecificMenu(sender, "Stai per autorizzare il seguente utente:\n"+
desc+
"\nSe le informazioni sono corrette fai 'tap' sui gruppi di appartenenza dell'utente da autorizzare, altrimenti *torna al menù principale ed annulla l'autorizzazione*",
menu, true)
@@ -117,9 +154,9 @@ func authUserCmd(u *tb.User, payload string) {
}
}
func deAuthUserCmd(u *tb.User, payload string) {
func deAuthUserCmd(sender *tb.User, payload string) {
if payload == "" {
err := sendMsg(u, deAuthHowToMsg, true)
err := sendMsgWithMenu(sender, deAuthHowToMsg, true)
if err != nil {
log.Printf("Error in sending message: %v", err)
}
@@ -128,29 +165,86 @@ func deAuthUserCmd(u *tb.User, payload string) {
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
authorizeUser(userID, false)
//TODO
user, err := getUserInfo(userID)
if err != nil {
log.Printf("Error getting user info: %v", err)
return
}
desc, err := getUserDescription(user)
if err != nil {
log.Printf("Error retriving user description: %v", err)
}
menu := authUserMenu
menu[0][0].Data = strconv.Itoa(user.ID) + "+remove"
menu[0][1].Data = strconv.Itoa(user.ID) + "+remove"
menu[1][0].Data = strconv.Itoa(user.ID) + "+remove"
menu[1][1].Data = strconv.Itoa(user.ID) + "+remove"
menu[2][0].Data = strconv.Itoa(user.ID) + "+remove"
menu[2][1].Data = strconv.Itoa(user.ID) + "+remove"
menu[2][2].Data = strconv.Itoa(user.ID) + "+remove"
if is, _ := isUserInGroup(user.ID, ugSoprano); !is {
menu[0][0].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugContralto); !is {
menu[0][1].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugTenore); !is {
menu[1][0].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugBasso); !is {
menu[1][1].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugCommissario); !is {
menu[2][0].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugReferente); !is {
menu[2][1].Text = ""
}
if is, _ := isUserInGroup(user.ID, ugPreparatore); !is {
menu[2][2].Text = ""
}
err = sendMsgWithSpecificMenu(sender, "Stai per deautorizzare il seguente utente:\n"+
desc+
"\nSe le informazioni sono corrette fai 'tap' sui gruppi da cui deautorizzare l'utente, altrimenti *torna al menù principale ed annulla l'autorizzazione*",
menu, true)
if err != nil {
log.Printf("Error in sending message: %v", err)
}
}
}
func addUserGroupCmd(userID int, group userGroup) error {
func addUserGroupCmd(userID int, group userGroup, add bool) error {
userGroups, err := getUserGroups(userID)
if err != nil {
log.Printf("Error retriving user groups: %v", err)
return ErrAddAuthUser
}
is, err := isUserInGroup(userID, group)
if err != nil {
log.Printf("Error checking if user is in group: %v", err)
}
if is {
return ErrAddUser
}
userGroups = append(userGroups, group)
err = setUserGroups(userID, userGroups...)
if err != nil {
log.Printf("Error adding user in group: %v", err)
return ErrAddAuthUser
}
if is && !add {
//REMOVE USER FROM GROUP
//TODO
} else if !is && add {
userGroups = append(userGroups, group)
err = setUserGroups(userID, userGroups...)
if err != nil {
log.Printf("Error adding user in group: %v", err)
return ErrAddAuthUser
}
err = authorizeUser(userID, true)
if err != nil {
log.Printf("Error authorizing user: %v", err)
return ErrAddAuthUser
}
}
return nil
}

View File

@@ -35,7 +35,7 @@ func setBotHandlers() error {
authUserCmd(m.Sender, m.Payload)
})
bot.Handle("/deAuthUser", func(m *tb.Message) {
deAuthUserCmd(m.Sender, m.Payload)
})
bot.Handle(tb.OnText, func(m *tb.Message) {

View File

@@ -3,6 +3,7 @@ package main
import (
"log"
"strconv"
"strings"
tb "gopkg.in/tucnak/telebot.v2"
)
@@ -118,6 +119,38 @@ func setBotMenus() error {
return nil
}
func groupCallback(c *tb.Callback, groupName string) {
dataContent := strings.Split(c.Data, "+")
userID, err := strconv.Atoi(dataContent[0])
if err != nil {
log.Printf("Error converting string to int: %v", err)
return
}
var errAlert, authAlert string
var add bool
if len(dataContent) > 1 && dataContent[1] == "remove" {
add = false
errAlert = "Impossibile deautorizzare l'utente per il gruppo " + groupName
authAlert = "Utente " + dataContent[0] + " rimosso dal gruppo " + groupName
} else {
add = true
errAlert = "Impossibile aggiungere l'utente al gruppo " + groupName
authAlert = "Utente " + dataContent[0] + " aggiunto al gruppo " + groupName
}
err = addUserGroupCmd(userID, ugContralto, add)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: errAlert,
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: authAlert,
ShowAlert: true,
})
}
}
func setBotCallbacks() error {
if bot == nil {
return ErrNilPointer
@@ -154,6 +187,7 @@ func setBotCallbacks() error {
})
bot.Handle(&deAuthBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithMenu(c.Sender, deAuthHowToMsg, false)
})
bot.Handle(&sendMsgBtn, func(c *tb.Callback) {
@@ -161,132 +195,25 @@ func setBotCallbacks() error {
})
bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugSoprano)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Soprani",
ShowAlert: true,
})
}
groupCallback(c, "Soprani")
})
bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugContralto)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Contralti",
ShowAlert: true,
})
}
groupCallback(c, "Contralti")
})
bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugTenore)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Tenori",
ShowAlert: true,
})
}
groupCallback(c, "Tenori")
})
bot.Handle(&authUGBassoBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugBasso)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Bassi",
ShowAlert: true,
})
}
groupCallback(c, "Bassi")
})
bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugCommissario)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Commissari",
ShowAlert: true,
})
}
groupCallback(c, "Commissari")
})
bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugReferente)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Referenti",
ShowAlert: true,
})
}
groupCallback(c, "Referenti")
})
bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) {
userID, err := strconv.Atoi(c.Data)
if err != nil {
log.Printf("Error converting string to int: %v", err)
}
err = addUserGroupCmd(userID, ugPreparatore)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: "Impossibile autorizzare l'utente",
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: "Autorizzato utente " + c.Data + "e aggiunto al gruppo Preparatori",
ShowAlert: true,
})
}
groupCallback(c, "Preparatori")
})
return nil