Interactive group selection for groupMsg

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-04-15 12:48:45 +02:00
parent c9f0ee4ad3
commit c1456bfb0d
3 changed files with 213 additions and 139 deletions

View File

@@ -11,7 +11,7 @@ import (
type groupMsg struct { type groupMsg struct {
SenderID int `sql:"sender_id" json:"sender_id"` SenderID int `sql:"sender_id" json:"sender_id"`
Group userGroup `sql:"group" json:"group"` Group []userGroup `sql:"group" json:"group"`
Msg string `sql:"msg" json:"msg"` Msg string `sql:"msg" json:"msg"`
Date time.Time `sql:"date" json:"date"` Date time.Time `sql:"date" json:"date"`
Sent bool `sql:"sent" json:"sent"` Sent bool `sql:"sent" json:"sent"`
@@ -146,7 +146,7 @@ func sendMsgWithSpecificMenu(user *tb.User, msg string, menu [][]tb.InlineButton
return nil return nil
} }
func addNewGroupMsg(sender *tb.User, group userGroup, msg string) (int64, error) { func addNewGroupMsg(sender *tb.User, group []userGroup, msg string) (int64, error) {
newGroupMsg := groupMsg{sender.ID, group, msg, time.Now(), false} newGroupMsg := groupMsg{sender.ID, group, msg, time.Now(), false}
jsonMsg, err := json.Marshal(newGroupMsg) jsonMsg, err := json.Marshal(newGroupMsg)
if err != nil { if err != nil {
@@ -162,13 +162,13 @@ func addNewGroupMsg(sender *tb.User, group userGroup, msg string) (int64, error)
return msgID - 1, nil return msgID - 1, nil
} }
func setGroupMsg(msg *groupMsg, index int64) error { func setGroupMsg(msg *groupMsg, msgID int64) error {
jsonMsg, err := json.Marshal(msg) jsonMsg, err := json.Marshal(msg)
if err != nil { if err != nil {
log.Printf("Error in marshalling groupMsg to json: %v", err) log.Printf("Error in marshalling groupMsg to json: %v", err)
return ErrJSONMarshall return ErrJSONMarshall
} }
err = redisClient.LSet(groupMsgs, index, jsonMsg).Err() err = redisClient.LSet(groupMsgs, msgID, jsonMsg).Err()
if err != nil { if err != nil {
log.Printf("Error modifying a groupMsg: %v", err) log.Printf("Error modifying a groupMsg: %v", err)
return ErrRedisSetList return ErrRedisSetList
@@ -176,6 +176,24 @@ func setGroupMsg(msg *groupMsg, index int64) error {
return nil return nil
} }
func addUGToGroupMsg(msgID int64, group userGroup) error {
msg, err := redisClient.LIndex(groupMsgs, msgID).Result()
if err != nil {
log.Printf("Error retriving group message from hash: %v", err)
return ErrRedisRetrieveHash
}
jsonMsg := &groupMsg{}
err = json.Unmarshal([]byte(msg), jsonMsg)
if err != nil {
log.Printf("Error unmarshalling groupMsg: %v", err)
return ErrJSONUnmarshall
}
jsonMsg.Group = append(jsonMsg.Group, group)
setGroupMsg(jsonMsg, msgID)
return nil
}
func sendMsgToGroup(msgID string) error { func sendMsgToGroup(msgID string) error {
ID, err := strconv.ParseInt(msgID, 10, 64) ID, err := strconv.ParseInt(msgID, 10, 64)
if err != nil { if err != nil {
@@ -193,15 +211,27 @@ func sendMsgToGroup(msgID string) error {
log.Printf("Error unmarshalling groupMsg: %v", err) log.Printf("Error unmarshalling groupMsg: %v", err)
return ErrJSONUnmarshall return ErrJSONUnmarshall
} }
if jsonMsg.Sent {
return ErrSendMsg
}
sender, err := getUserInfo(jsonMsg.SenderID) sender, err := getUserInfo(jsonMsg.SenderID)
if err != nil { if err != nil {
log.Printf("Error retrieving sender info: %v", err) log.Printf("Error retrieving sender info: %v", err)
return ErrGetUser return ErrGetUser
} }
users, err := getUsersInGroup(jsonMsg.Group) if jsonMsg.Sent {
err = sendMsgWithMenu(sender, "Il messaggio é giá stato inviato!", false)
if err != nil {
log.Printf("Error sending msg to sender: %v", err)
return ErrSendMsg
}
} else {
err = sendMsg(sender, "Inizio invio massivo", false)
if err != nil {
log.Printf("Error sending msg to sender: %v", err)
return ErrSendMsg
}
}
for _, group := range jsonMsg.Group {
users, err := getUsersInGroup(group)
if err != nil { if err != nil {
log.Printf("Error retrieving users in sendTo group: %v", err) log.Printf("Error retrieving users in sendTo group: %v", err)
return ErrGroupInvalid return ErrGroupInvalid
@@ -212,7 +242,7 @@ func sendMsgToGroup(msgID string) error {
log.Printf("Error retrieving user info from id: %v", err) log.Printf("Error retrieving user info from id: %v", err)
continue continue
} }
groupName, _ := getGroupName(jsonMsg.Group) groupName, _ := getGroupName(group)
msg = "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + jsonMsg.Msg msg = "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + jsonMsg.Msg
err = sendMsg(user, msg, true) err = sendMsg(user, msg, true)
if err != nil { if err != nil {
@@ -223,6 +253,7 @@ func sendMsgToGroup(msgID string) error {
log.Printf("Error sending msg to user: %v", err) log.Printf("Error sending msg to user: %v", err)
} }
} }
}
jsonMsg.Sent = true jsonMsg.Sent = true
jsonMsg.Date = time.Now() jsonMsg.Date = time.Now()
@@ -231,7 +262,7 @@ func sendMsgToGroup(msgID string) error {
log.Printf("Error updating groupMsg after send: %v", err) log.Printf("Error updating groupMsg after send: %v", err)
} }
err = sendMsgWithMenu(sender, "Messaggio inviato a tutti i componenti della sezione", false) err = sendMsgWithMenu(sender, "Messaggio inviato a tutti i componenti delle sezioni indicate", false)
if err != nil { if err != nil {
log.Printf("Error sending msg to sender: %v", err) log.Printf("Error sending msg to sender: %v", err)
} }

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"log" "log"
"strconv" "strconv"
"strings"
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
) )
@@ -116,14 +115,14 @@ func authUserCmd(sender *tb.User, payload string, newMsg bool) {
log.Printf("Error retriving user groups: %v", err) log.Printf("Error retriving user groups: %v", err)
} }
menu := getAuthUserMenu() menu := getUserGroupMenu()
menu[0][0].Data = strconv.Itoa(user.ID) menu[0][0].Data = strconv.Itoa(user.ID) + "+auth"
menu[0][1].Data = strconv.Itoa(user.ID) menu[0][1].Data = strconv.Itoa(user.ID) + "+auth"
menu[1][0].Data = strconv.Itoa(user.ID) menu[1][0].Data = strconv.Itoa(user.ID) + "+auth"
menu[1][1].Data = strconv.Itoa(user.ID) menu[1][1].Data = strconv.Itoa(user.ID) + "+auth"
menu[2][0].Data = strconv.Itoa(user.ID) menu[2][0].Data = strconv.Itoa(user.ID) + "+auth"
menu[2][1].Data = strconv.Itoa(user.ID) menu[2][1].Data = strconv.Itoa(user.ID) + "+auth"
menu[2][2].Data = strconv.Itoa(user.ID) menu[2][2].Data = strconv.Itoa(user.ID) + "+auth"
for _, group := range userGroups { for _, group := range userGroups {
switch group { switch group {
@@ -176,14 +175,14 @@ func deAuthUserCmd(sender *tb.User, payload string, newMsg bool) {
log.Printf("Error retriving user description: %v", err) log.Printf("Error retriving user description: %v", err)
} }
menu := getAuthUserMenu() menu := getUserGroupMenu()
menu[0][0].Data = strconv.Itoa(user.ID) + "+remove" menu[0][0].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[0][1].Data = strconv.Itoa(user.ID) + "+remove" menu[0][1].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[1][0].Data = strconv.Itoa(user.ID) + "+remove" menu[1][0].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[1][1].Data = strconv.Itoa(user.ID) + "+remove" menu[1][1].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[2][0].Data = strconv.Itoa(user.ID) + "+remove" menu[2][0].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[2][1].Data = strconv.Itoa(user.ID) + "+remove" menu[2][1].Data = strconv.Itoa(user.ID) + "+deAuth"
menu[2][2].Data = strconv.Itoa(user.ID) + "+remove" menu[2][2].Data = strconv.Itoa(user.ID) + "+deAuth"
if is, _ := isUserInGroup(user.ID, ugSoprano); !is { if is, _ := isUserInGroup(user.ID, ugSoprano); !is {
menu[0][0].Text = "" menu[0][0].Text = ""
@@ -291,40 +290,50 @@ func helpCmd(user *tb.User, newMsg bool) {
} }
func sendMsgCmd(sender *tb.User, payload string, newMsg bool) { func sendMsgCmd(sender *tb.User, payload string, newMsg bool) {
arg := strings.SplitN(payload, " ", 2) if payload == "" {
if payload == "" || len(arg) != 2 || arg[1] == "" || arg[1] == " " {
err := sendMsgWithMenu(sender, sendMsgHowToMsg, newMsg) err := sendMsgWithMenu(sender, sendMsgHowToMsg, newMsg)
if err != nil { if err != nil {
log.Printf("Error in sending message: %v", err) log.Printf("Error in sending message: %v", err)
} }
} else { } else {
group, err := getUserGroupFromStr(arg[0]) msgID, err := addNewGroupMsg(sender, []userGroup{}, payload)
if err != nil {
log.Printf("Error in parsing the userGroup: %v", err)
return
}
is, err := isUserInGroup(sender.ID, group)
if err != nil {
log.Printf("Error checking if sender is in sendTo group: %v", err)
return
}
if is {
ugName, _ := getGroupName(group)
//sendMsgToGroup(sender, group, arg[1])
msgID, err := addNewGroupMsg(sender, group, arg[1])
if err != nil { if err != nil {
log.Printf("Error adding new groupMsg in db: %v", err) log.Printf("Error adding new groupMsg in db: %v", err)
return return
} }
msg := "*Sei sicuro di voler inviare il seguente messaggio al gruppo " + ugName + "?*\n" + arg[1] menu := getUserGroupMenu()
sendMsgMenu[0][0].Data = strconv.FormatInt(msgID, 10) menu[3] = append(menu[3], confirmSendBtn)
sendMsgWithSpecificMenu(sender, msg, sendMsgMenu, true) menu[0][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
} else { menu[0][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
err = sendMsgWithMenu(sender, sendMsgErrMsg, true) menu[1][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
if err != nil { menu[1][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
log.Printf("Error sending msg to user: %v", err) menu[2][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
} menu[2][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
} menu[2][2].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[3][1].Data = strconv.FormatInt(msgID, 10)
if is, _ := isUserInGroup(sender.ID, ugSoprano); !is {
menu[0][0].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugContralto); !is {
menu[0][1].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugTenore); !is {
menu[1][0].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugBasso); !is {
menu[1][1].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugCommissario); !is {
menu[2][0].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugReferente); !is {
menu[2][1].Text = ""
}
if is, _ := isUserInGroup(sender.ID, ugPreparatore); !is {
menu[2][2].Text = ""
}
msg := "*Il messaggio che stai per inviare é *\n" + payload + "\n*Seleziona i gruppi a cui vuoi inviarlo*"
sendMsgWithSpecificMenu(sender, msg, menu, true)
} }
} }

View File

@@ -69,32 +69,32 @@ var (
Unique: "confirm_send_btn", Unique: "confirm_send_btn",
Text: "\xE2\x9C\x85 Conferma", Text: "\xE2\x9C\x85 Conferma",
} }
authUGSopranoBtn = tb.InlineButton{ ugSopranoBtn = tb.InlineButton{
Unique: "auth_ugSoprano_btn", Unique: "ugSoprano_btn",
Text: "\xF0\x9F\x91\xA7 Soprani", Text: "\xF0\x9F\x91\xA7 Soprani",
} }
authUGContraltoBtn = tb.InlineButton{ ugContraltoBtn = tb.InlineButton{
Unique: "auth_ugContralto_btn", Unique: "ugContralto_btn",
Text: "\xF0\x9F\x91\xA9 Contralti", Text: "\xF0\x9F\x91\xA9 Contralti",
} }
authUGTenoreBtn = tb.InlineButton{ ugTenoreBtn = tb.InlineButton{
Unique: "auth_ugTenore_btn", Unique: "ugTenore_btn",
Text: "\xF0\x9F\x91\xA6 Tenori", Text: "\xF0\x9F\x91\xA6 Tenori",
} }
authUGBassoBtn = tb.InlineButton{ ugBassoBtn = tb.InlineButton{
Unique: "auth_ugBasso_btn", Unique: "ugBasso_btn",
Text: "\xF0\x9F\x91\xA8 Bassi", Text: "\xF0\x9F\x91\xA8 Bassi",
} }
authUGCommissarioBtn = tb.InlineButton{ ugCommissarioBtn = tb.InlineButton{
Unique: "auth_ugCommissario_btn", Unique: "ugCommissario_btn",
Text: "\xF0\x9F\x93\x9D Commissari", Text: "\xF0\x9F\x93\x9D Commissari",
} }
authUGReferenteBtn = tb.InlineButton{ ugReferenteBtn = tb.InlineButton{
Unique: "auth_ugReferente_btn", Unique: "ugReferente_btn",
Text: "\xF0\x9F\x93\x8B Referenti", Text: "\xF0\x9F\x93\x8B Referenti",
} }
authUGPreparatoreBtn = tb.InlineButton{ ugPreparatoreBtn = tb.InlineButton{
Unique: "auth_ugPreparatori_btn", Unique: "ugPreparatori_btn",
Text: "\xF0\x9F\x8E\xB9 Preparatori", Text: "\xF0\x9F\x8E\xB9 Preparatori",
} }
) )
@@ -118,50 +118,71 @@ func setBotMenus() error {
startMenu = append(startMenu, []tb.InlineButton{startBtn}) startMenu = append(startMenu, []tb.InlineButton{startBtn})
backMenu = append(backMenu, []tb.InlineButton{backBtn}) backMenu = append(backMenu, []tb.InlineButton{backBtn})
botInfoMenu = append(botInfoMenu, []tb.InlineButton{helpBtn, stopBtn}, []tb.InlineButton{backBtn}) botInfoMenu = append(botInfoMenu, []tb.InlineButton{helpBtn, stopBtn}, []tb.InlineButton{backBtn})
authUserMenu = append(authUserMenu, /* authUserMenu = append(authUserMenu,
[]tb.InlineButton{authUGSopranoBtn, authUGContraltoBtn}, []tb.InlineButton{ugSopranoBtn, ugContraltoBtn},
[]tb.InlineButton{authUGTenoreBtn, authUGBassoBtn}, []tb.InlineButton{ugTenoreBtn, ugBassoBtn},
[]tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn}, []tb.InlineButton{ugCommissarioBtn, ugReferenteBtn, ugPreparatoreBtn},
[]tb.InlineButton{backBtn}, []tb.InlineButton{backBtn},
) ) */
sendMsgMenu = append(sendMsgMenu, []tb.InlineButton{confirmSendBtn, backBtn}) sendMsgMenu = append(sendMsgMenu, []tb.InlineButton{confirmSendBtn, backBtn})
return nil return nil
} }
func getAuthUserMenu() [][]tb.InlineButton { func getUserGroupMenu() [][]tb.InlineButton {
var authUserMenu [][]tb.InlineButton var ugMenu [][]tb.InlineButton
authUserMenu = append(authUserMenu, ugMenu = append(ugMenu,
[]tb.InlineButton{authUGSopranoBtn, authUGContraltoBtn}, []tb.InlineButton{ugSopranoBtn, ugContraltoBtn},
[]tb.InlineButton{authUGTenoreBtn, authUGBassoBtn}, []tb.InlineButton{ugTenoreBtn, ugBassoBtn},
[]tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn}, []tb.InlineButton{ugCommissarioBtn, ugReferenteBtn, ugPreparatoreBtn},
[]tb.InlineButton{backBtn}, []tb.InlineButton{backBtn},
) )
return authUserMenu return ugMenu
} }
func groupCallback(c *tb.Callback, group userGroup) { func ugBtnCallback(c *tb.Callback, group userGroup) {
dataContent := strings.Split(c.Data, "+") dataContent := strings.Split(c.Data, "+")
userID, err := strconv.Atoi(dataContent[0]) if len(dataContent) <= 1 {
log.Printf("Error: too few arguments")
return
}
var userID int
var msgID int64
var errAlert, successAlert string
auth, deAuth, sendUg := false, false, false
groupName, err := getGroupName(group)
if dataContent[1] == "deAuth" {
deAuth = true
errAlert = "Impossibile deautorizzare l'utente per il gruppo " + groupName
successAlert = "Utente " + dataContent[0] + " rimosso dal gruppo " + groupName
userID, err = strconv.Atoi(dataContent[0])
if err != nil { if err != nil {
log.Printf("Error converting string to int: %v", err) log.Printf("Error converting string to int: %v", err)
return return
} }
var errAlert, authAlert string } else if dataContent[1] == "auth" {
var add bool auth = true
groupName, err := getGroupName(group)
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 errAlert = "Impossibile aggiungere l'utente al gruppo " + groupName
authAlert = "Utente " + dataContent[0] + " aggiunto al gruppo " + groupName successAlert = "Utente " + dataContent[0] + " aggiunto al gruppo " + groupName
userID, err = strconv.Atoi(dataContent[0])
if err != nil {
log.Printf("Error converting string to int: %v", err)
return
} }
err = addUserGroupCmd(userID, group, add) } else if dataContent[1] == "sendUg" {
sendUg = true
errAlert = "Impossibile aggiungere il gruppo " + groupName + " alla lista dei destinatari"
successAlert = "Gruppo " + groupName + " aggiunto alla lista dei destinatari"
msgID, err = strconv.ParseInt(dataContent[0], 10, 64)
if err != nil {
log.Printf("Error converting msgID to int64: %v", err)
return
}
}
if auth || deAuth {
err = addUserGroupCmd(userID, group, auth)
if err != nil { if err != nil {
bot.Respond(c, &tb.CallbackResponse{ bot.Respond(c, &tb.CallbackResponse{
Text: errAlert, Text: errAlert,
@@ -169,15 +190,29 @@ func groupCallback(c *tb.Callback, group userGroup) {
}) })
} else { } else {
bot.Respond(c, &tb.CallbackResponse{ bot.Respond(c, &tb.CallbackResponse{
Text: authAlert, Text: successAlert,
ShowAlert: true, ShowAlert: true,
}) })
if add { if auth {
authUserCmd(c.Sender, dataContent[0], false) authUserCmd(c.Sender, dataContent[0], false)
} else { } else if deAuth {
deAuthUserCmd(c.Sender, dataContent[0], false) deAuthUserCmd(c.Sender, dataContent[0], false)
} }
} }
} else if sendUg {
err = addUGToGroupMsg(msgID, group)
if err != nil {
bot.Respond(c, &tb.CallbackResponse{
Text: errAlert,
ShowAlert: true,
})
} else {
bot.Respond(c, &tb.CallbackResponse{
Text: successAlert,
ShowAlert: true,
})
}
}
} }
func setBotCallbacks() error { func setBotCallbacks() error {
@@ -216,7 +251,6 @@ func setBotCallbacks() error {
sendMsgWithMenu(c.Sender, menuMsg, false) sendMsgWithMenu(c.Sender, menuMsg, false)
}) })
//TODO
bot.Handle(&confirmSendBtn, func(c *tb.Callback) { bot.Handle(&confirmSendBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{ bot.Respond(c, &tb.CallbackResponse{
Text: sentStartedMsg, Text: sentStartedMsg,
@@ -240,32 +274,32 @@ func setBotCallbacks() error {
sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false) sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false)
}) })
bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) { bot.Handle(&ugSopranoBtn, func(c *tb.Callback) {
groupCallback(c, ugSoprano) ugBtnCallback(c, ugSoprano)
}) })
bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) { bot.Handle(&ugContraltoBtn, func(c *tb.Callback) {
groupCallback(c, ugContralto) ugBtnCallback(c, ugContralto)
}) })
bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) { bot.Handle(&ugTenoreBtn, func(c *tb.Callback) {
groupCallback(c, ugTenore) ugBtnCallback(c, ugTenore)
}) })
bot.Handle(&authUGBassoBtn, func(c *tb.Callback) { bot.Handle(&ugBassoBtn, func(c *tb.Callback) {
groupCallback(c, ugBasso) ugBtnCallback(c, ugBasso)
}) })
bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) { bot.Handle(&ugCommissarioBtn, func(c *tb.Callback) {
groupCallback(c, ugCommissario) ugBtnCallback(c, ugCommissario)
}) })
bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) { bot.Handle(&ugReferenteBtn, func(c *tb.Callback) {
groupCallback(c, ugReferente) ugBtnCallback(c, ugReferente)
}) })
bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) { bot.Handle(&ugPreparatoreBtn, func(c *tb.Callback) {
groupCallback(c, ugPreparatore) ugBtnCallback(c, ugPreparatore)
}) })
return nil return nil