Integrato menu di conferma per invio messaggio

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-04-14 19:26:49 +02:00
parent 0e15459556
commit c9f0ee4ad3
5 changed files with 147 additions and 22 deletions

View File

@@ -4,10 +4,19 @@ import (
"encoding/json"
"log"
"strconv"
"time"
tb "gopkg.in/tucnak/telebot.v2"
)
type groupMsg struct {
SenderID int `sql:"sender_id" json:"sender_id"`
Group userGroup `sql:"group" json:"group"`
Msg string `sql:"msg" json:"msg"`
Date time.Time `sql:"date" json:"date"`
Sent bool `sql:"sent" json:"sent"`
}
func modifyPrevMsg(userID int, storedMsg *tb.StoredMessage, newMsg string, newOptions *tb.SendOptions) error {
msg, err := bot.Edit(storedMsg, newMsg, newOptions)
if err != nil {
@@ -26,7 +35,8 @@ func modifyPrevMsg(userID int, storedMsg *tb.StoredMessage, newMsg string, newOp
func setLastMsgPerUser(userID int, msg *tb.Message) error {
storedMsg := tb.StoredMessage{
MessageID: strconv.Itoa(msg.ID),
ChatID: msg.Chat.ID}
ChatID: msg.Chat.ID,
}
jsonMsg, err := json.Marshal(storedMsg)
if err != nil {
@@ -135,3 +145,96 @@ func sendMsgWithSpecificMenu(user *tb.User, msg string, menu [][]tb.InlineButton
return nil
}
func addNewGroupMsg(sender *tb.User, group userGroup, msg string) (int64, error) {
newGroupMsg := groupMsg{sender.ID, group, msg, time.Now(), false}
jsonMsg, err := json.Marshal(newGroupMsg)
if err != nil {
log.Printf("Error in marshalling groupMsg to json: %v", err)
return -1, ErrJSONMarshall
}
//err = redisClient.HSet(lastMsgPerUser, strconv.Itoa(userID), jsonMsg).Err()
msgID, err := redisClient.RPush(groupMsgs, jsonMsg).Result()
if err != nil {
log.Printf("Error adding new group message in hash: %v", err)
return -1, ErrRedisAddList
}
return msgID - 1, nil
}
func setGroupMsg(msg *groupMsg, index int64) error {
jsonMsg, err := json.Marshal(msg)
if err != nil {
log.Printf("Error in marshalling groupMsg to json: %v", err)
return ErrJSONMarshall
}
err = redisClient.LSet(groupMsgs, index, jsonMsg).Err()
if err != nil {
log.Printf("Error modifying a groupMsg: %v", err)
return ErrRedisSetList
}
return nil
}
func sendMsgToGroup(msgID string) error {
ID, err := strconv.ParseInt(msgID, 10, 64)
if err != nil {
log.Printf("Error converting msgID to int64: %v", err)
return ErrAtoiConv
}
msg, err := redisClient.LIndex(groupMsgs, ID).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
}
if jsonMsg.Sent {
return ErrSendMsg
}
sender, err := getUserInfo(jsonMsg.SenderID)
if err != nil {
log.Printf("Error retrieving sender info: %v", err)
return ErrGetUser
}
users, err := getUsersInGroup(jsonMsg.Group)
if err != nil {
log.Printf("Error retrieving users in sendTo group: %v", err)
return ErrGroupInvalid
}
for _, userID := range users {
user, err := getUserInfo(userID)
if err != nil {
log.Printf("Error retrieving user info from id: %v", err)
continue
}
groupName, _ := getGroupName(jsonMsg.Group)
msg = "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + jsonMsg.Msg
err = sendMsg(user, msg, true)
if err != nil {
log.Printf("Error sending msg to user: %v", err)
}
err = sendMsgWithMenu(user, msgReceivedMsg, true)
if err != nil {
log.Printf("Error sending msg to user: %v", err)
}
}
jsonMsg.Sent = true
jsonMsg.Date = time.Now()
err = setGroupMsg(jsonMsg, ID)
if err != nil {
log.Printf("Error updating groupMsg after send: %v", err)
}
err = sendMsgWithMenu(sender, "Messaggio inviato a tutti i componenti della sezione", false)
if err != nil {
log.Printf("Error sending msg to sender: %v", err)
}
return nil
}

View File

@@ -17,6 +17,7 @@ const (
authUsers = "authUsers"
adminUsers = "adminUsers"
lastMsgPerUser = "lastMsgPerUser"
groupMsgs = "groupMsgs"
mediaPath = "mediaPath"
)
@@ -43,6 +44,10 @@ var (
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")
//ErrRedisAddList is thrown when it's not possible to add a new value in a list
ErrRedisAddList = errors.New("redis: couldn't add value in list")
//ErrRedisSetList is thrown when it's not possible to update a value in a list
ErrRedisSetList = errors.New("redis: couldn't set value in list")
//ErrTokenParsing is thrown when it's not possible to parse the bot token
ErrTokenParsing = errors.New("botToken: cannot parse token")
//ErrTokenInvalid is thrown when the string parsed isn't a valid telegram bot token

View File

@@ -36,6 +36,7 @@ const (
sendMsgHowToMsg string = "Per inviare un messaggio ad una sezione invia il comando \n`/sendMsg SEZIONE TESTO_MESSAGGIO` sostituendo `SEZIONE` con il nome della sezione a cui inviare il messaggio e `TESTO_MESSAGGIO` con il testo del messaggio che vuoi recapitare"
sendMsgErrMsg string = "Puoi inviare messaggi soltanto alle sezioni cui appartieni"
msgReceivedMsg string = "Hai ricevuto questo messaggio perché fai parte di una sezione del Coro UniPi. Se non vuoi piú riceverne fallo presente!"
sentStartedMsg string = "Invio del messaggio iniziato."
)
var bot *tb.Bot

View File

@@ -310,25 +310,16 @@ func sendMsgCmd(sender *tb.User, payload string, newMsg bool) {
return
}
if is {
users, err := getUsersInGroup(group)
ugName, _ := getGroupName(group)
//sendMsgToGroup(sender, group, arg[1])
msgID, err := addNewGroupMsg(sender, group, arg[1])
if err != nil {
log.Printf("Error retrieving users in sendTo group: %v", err)
log.Printf("Error adding new groupMsg in db: %v", err)
return
}
for _, userID := range users {
user, err := getUserInfo(userID)
if err != nil {
log.Printf("Error retrieving user info from id: %v", err)
continue
}
groupName, _ := getGroupName(group)
msg := "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + arg[1]
err = sendMsg(user, msg, true)
if err != nil {
log.Printf("Error sending msg to user: %v", err)
}
err = sendMsgWithMenu(user, msgReceivedMsg, true)
}
msg := "*Sei sicuro di voler inviare il seguente messaggio al gruppo " + ugName + "?*\n" + arg[1]
sendMsgMenu[0][0].Data = strconv.FormatInt(msgID, 10)
sendMsgWithSpecificMenu(sender, msg, sendMsgMenu, true)
} else {
err = sendMsgWithMenu(sender, sendMsgErrMsg, true)
if err != nil {

View File

@@ -17,6 +17,7 @@ var (
backMenu [][]tb.InlineButton
botInfoMenu [][]tb.InlineButton
authUserMenu [][]tb.InlineButton
sendMsgMenu [][]tb.InlineButton
)
var (
@@ -30,7 +31,7 @@ var (
}
backBtn = tb.InlineButton{
Unique: "back_btn",
Text: "\xF0\x9F\x94\x99 Torna al menù principale",
Text: "\xF0\x9F\x94\x99 Torna indietro",
}
infoBtn = tb.InlineButton{
Unique: "info_btn",
@@ -46,11 +47,11 @@ var (
}
authBtn = tb.InlineButton{
Unique: "auth_btn",
Text: "\xE2\x9C\x85 Autorizza utente",
Text: "\xE2\x9E\x95 Autorizza utente",
}
deAuthBtn = tb.InlineButton{
Unique: "de_auth_btn",
Text: "\xE2\x9D\x8C Deautorizza utente",
Text: "\xE2\x9E\x96 Deautorizza utente",
}
adminBtn = tb.InlineButton{
Unique: "admin_btn",
@@ -64,6 +65,10 @@ var (
Unique: "send_msg_btn",
Text: "\xF0\x9F\x93\xA3 Invia messaggio alla sezione",
}
confirmSendBtn = tb.InlineButton{
Unique: "confirm_send_btn",
Text: "\xE2\x9C\x85 Conferma",
}
authUGSopranoBtn = tb.InlineButton{
Unique: "auth_ugSoprano_btn",
Text: "\xF0\x9F\x91\xA7 Soprani",
@@ -119,6 +124,7 @@ func setBotMenus() error {
[]tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn},
[]tb.InlineButton{backBtn},
)
sendMsgMenu = append(sendMsgMenu, []tb.InlineButton{confirmSendBtn, backBtn})
return nil
}
@@ -194,51 +200,70 @@ func setBotCallbacks() error {
msg, _ := getUserDescription(c.Sender)
sendMsgWithSpecificMenu(c.Sender, msg, backMenu, false)
})
bot.Handle(&infoBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithSpecificMenu(c.Sender, contactMsg, botInfoMenu, false)
})
bot.Handle(&helpBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
helpCmd(c.Sender, false)
})
bot.Handle(&backBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithMenu(c.Sender, menuMsg, false)
})
//TODO
bot.Handle(&confirmSendBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{
Text: sentStartedMsg,
ShowAlert: true,
})
sendMsgToGroup(c.Data)
})
bot.Handle(&authBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithMenu(c.Sender, authHowToMsg, false)
})
bot.Handle(&deAuthBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithMenu(c.Sender, deAuthHowToMsg, false)
})
bot.Handle(&sendMsgBtn, func(c *tb.Callback) {
bot.Respond(c, &tb.CallbackResponse{})
sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false)
})
bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) {
groupCallback(c, ugSoprano)
})
bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) {
groupCallback(c, ugContralto)
})
bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) {
groupCallback(c, ugTenore)
})
bot.Handle(&authUGBassoBtn, func(c *tb.Callback) {
groupCallback(c, ugBasso)
})
bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) {
groupCallback(c, ugCommissario)
})
bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) {
groupCallback(c, ugReferente)
})
bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) {
groupCallback(c, ugPreparatore)
})