Integrato menu di conferma per invio messaggio
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
105
src/manageMsg.go
105
src/manageMsg.go
@@ -4,10 +4,19 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
tb "gopkg.in/tucnak/telebot.v2"
|
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 {
|
func modifyPrevMsg(userID int, storedMsg *tb.StoredMessage, newMsg string, newOptions *tb.SendOptions) error {
|
||||||
msg, err := bot.Edit(storedMsg, newMsg, newOptions)
|
msg, err := bot.Edit(storedMsg, newMsg, newOptions)
|
||||||
if err != nil {
|
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 {
|
func setLastMsgPerUser(userID int, msg *tb.Message) error {
|
||||||
storedMsg := tb.StoredMessage{
|
storedMsg := tb.StoredMessage{
|
||||||
MessageID: strconv.Itoa(msg.ID),
|
MessageID: strconv.Itoa(msg.ID),
|
||||||
ChatID: msg.Chat.ID}
|
ChatID: msg.Chat.ID,
|
||||||
|
}
|
||||||
|
|
||||||
jsonMsg, err := json.Marshal(storedMsg)
|
jsonMsg, err := json.Marshal(storedMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -135,3 +145,96 @@ 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) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ const (
|
|||||||
authUsers = "authUsers"
|
authUsers = "authUsers"
|
||||||
adminUsers = "adminUsers"
|
adminUsers = "adminUsers"
|
||||||
lastMsgPerUser = "lastMsgPerUser"
|
lastMsgPerUser = "lastMsgPerUser"
|
||||||
|
groupMsgs = "groupMsgs"
|
||||||
mediaPath = "mediaPath"
|
mediaPath = "mediaPath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,6 +44,10 @@ var (
|
|||||||
ErrRedisDelString = errors.New("redis: couldn't remove 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 is thrown when it's not possible to retrieve a key from a hash
|
||||||
ErrRedisRetrieveHash = errors.New("redis: couldn't retrieve key from 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 is thrown when it's not possible to parse the bot token
|
||||||
ErrTokenParsing = errors.New("botToken: cannot parse token")
|
ErrTokenParsing = errors.New("botToken: cannot parse token")
|
||||||
//ErrTokenInvalid is thrown when the string parsed isn't a valid telegram bot token
|
//ErrTokenInvalid is thrown when the string parsed isn't a valid telegram bot token
|
||||||
|
@@ -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"
|
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"
|
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!"
|
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
|
var bot *tb.Bot
|
||||||
|
@@ -310,25 +310,16 @@ func sendMsgCmd(sender *tb.User, payload string, newMsg bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if is {
|
if is {
|
||||||
users, err := getUsersInGroup(group)
|
ugName, _ := getGroupName(group)
|
||||||
|
//sendMsgToGroup(sender, group, arg[1])
|
||||||
|
msgID, err := addNewGroupMsg(sender, group, arg[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error retrieving users in sendTo group: %v", err)
|
log.Printf("Error adding new groupMsg in db: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, userID := range users {
|
msg := "*Sei sicuro di voler inviare il seguente messaggio al gruppo " + ugName + "?*\n" + arg[1]
|
||||||
user, err := getUserInfo(userID)
|
sendMsgMenu[0][0].Data = strconv.FormatInt(msgID, 10)
|
||||||
if err != nil {
|
sendMsgWithSpecificMenu(sender, msg, sendMsgMenu, true)
|
||||||
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)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = sendMsgWithMenu(sender, sendMsgErrMsg, true)
|
err = sendMsgWithMenu(sender, sendMsgErrMsg, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -17,6 +17,7 @@ var (
|
|||||||
backMenu [][]tb.InlineButton
|
backMenu [][]tb.InlineButton
|
||||||
botInfoMenu [][]tb.InlineButton
|
botInfoMenu [][]tb.InlineButton
|
||||||
authUserMenu [][]tb.InlineButton
|
authUserMenu [][]tb.InlineButton
|
||||||
|
sendMsgMenu [][]tb.InlineButton
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -30,7 +31,7 @@ var (
|
|||||||
}
|
}
|
||||||
backBtn = tb.InlineButton{
|
backBtn = tb.InlineButton{
|
||||||
Unique: "back_btn",
|
Unique: "back_btn",
|
||||||
Text: "\xF0\x9F\x94\x99 Torna al menù principale",
|
Text: "\xF0\x9F\x94\x99 Torna indietro",
|
||||||
}
|
}
|
||||||
infoBtn = tb.InlineButton{
|
infoBtn = tb.InlineButton{
|
||||||
Unique: "info_btn",
|
Unique: "info_btn",
|
||||||
@@ -46,11 +47,11 @@ var (
|
|||||||
}
|
}
|
||||||
authBtn = tb.InlineButton{
|
authBtn = tb.InlineButton{
|
||||||
Unique: "auth_btn",
|
Unique: "auth_btn",
|
||||||
Text: "\xE2\x9C\x85 Autorizza utente",
|
Text: "\xE2\x9E\x95 Autorizza utente",
|
||||||
}
|
}
|
||||||
deAuthBtn = tb.InlineButton{
|
deAuthBtn = tb.InlineButton{
|
||||||
Unique: "de_auth_btn",
|
Unique: "de_auth_btn",
|
||||||
Text: "\xE2\x9D\x8C Deautorizza utente",
|
Text: "\xE2\x9E\x96 Deautorizza utente",
|
||||||
}
|
}
|
||||||
adminBtn = tb.InlineButton{
|
adminBtn = tb.InlineButton{
|
||||||
Unique: "admin_btn",
|
Unique: "admin_btn",
|
||||||
@@ -64,6 +65,10 @@ var (
|
|||||||
Unique: "send_msg_btn",
|
Unique: "send_msg_btn",
|
||||||
Text: "\xF0\x9F\x93\xA3 Invia messaggio alla sezione",
|
Text: "\xF0\x9F\x93\xA3 Invia messaggio alla sezione",
|
||||||
}
|
}
|
||||||
|
confirmSendBtn = tb.InlineButton{
|
||||||
|
Unique: "confirm_send_btn",
|
||||||
|
Text: "\xE2\x9C\x85 Conferma",
|
||||||
|
}
|
||||||
authUGSopranoBtn = tb.InlineButton{
|
authUGSopranoBtn = tb.InlineButton{
|
||||||
Unique: "auth_ugSoprano_btn",
|
Unique: "auth_ugSoprano_btn",
|
||||||
Text: "\xF0\x9F\x91\xA7 Soprani",
|
Text: "\xF0\x9F\x91\xA7 Soprani",
|
||||||
@@ -119,6 +124,7 @@ func setBotMenus() error {
|
|||||||
[]tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn},
|
[]tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn},
|
||||||
[]tb.InlineButton{backBtn},
|
[]tb.InlineButton{backBtn},
|
||||||
)
|
)
|
||||||
|
sendMsgMenu = append(sendMsgMenu, []tb.InlineButton{confirmSendBtn, backBtn})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -194,51 +200,70 @@ func setBotCallbacks() error {
|
|||||||
msg, _ := getUserDescription(c.Sender)
|
msg, _ := getUserDescription(c.Sender)
|
||||||
sendMsgWithSpecificMenu(c.Sender, msg, backMenu, false)
|
sendMsgWithSpecificMenu(c.Sender, msg, backMenu, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&infoBtn, func(c *tb.Callback) {
|
bot.Handle(&infoBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
sendMsgWithSpecificMenu(c.Sender, contactMsg, botInfoMenu, false)
|
sendMsgWithSpecificMenu(c.Sender, contactMsg, botInfoMenu, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&helpBtn, func(c *tb.Callback) {
|
bot.Handle(&helpBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
helpCmd(c.Sender, false)
|
helpCmd(c.Sender, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&backBtn, func(c *tb.Callback) {
|
bot.Handle(&backBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
sendMsgWithMenu(c.Sender, menuMsg, false)
|
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.Handle(&authBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
sendMsgWithMenu(c.Sender, authHowToMsg, false)
|
sendMsgWithMenu(c.Sender, authHowToMsg, false)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&deAuthBtn, func(c *tb.Callback) {
|
bot.Handle(&deAuthBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
sendMsgWithMenu(c.Sender, deAuthHowToMsg, false)
|
sendMsgWithMenu(c.Sender, deAuthHowToMsg, false)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&sendMsgBtn, func(c *tb.Callback) {
|
bot.Handle(&sendMsgBtn, func(c *tb.Callback) {
|
||||||
bot.Respond(c, &tb.CallbackResponse{})
|
bot.Respond(c, &tb.CallbackResponse{})
|
||||||
sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false)
|
sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugSoprano)
|
groupCallback(c, ugSoprano)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugContralto)
|
groupCallback(c, ugContralto)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugTenore)
|
groupCallback(c, ugTenore)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGBassoBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGBassoBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugBasso)
|
groupCallback(c, ugBasso)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugCommissario)
|
groupCallback(c, ugCommissario)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugReferente)
|
groupCallback(c, ugReferente)
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) {
|
bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) {
|
||||||
groupCallback(c, ugPreparatore)
|
groupCallback(c, ugPreparatore)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user