Continued integrating interactive sendMsg

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-04-17 00:46:26 +02:00
parent c1456bfb0d
commit 79b69be83f
3 changed files with 158 additions and 42 deletions

View File

@@ -10,11 +10,11 @@ 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 map[userGroup]bool `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"`
} }
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 {
@@ -67,6 +67,39 @@ func getLastMsgPerUser(userID int) (*tb.StoredMessage, error) {
return jsonMsg, nil return jsonMsg, nil
} }
func editLastMsgInlineKeyboard(user *tb.User, menu [][]tb.InlineButton) error {
storedMsg, err := getLastMsgPerUser(user.ID)
if err != nil {
log.Printf("Error retriving last message per user: %v", err)
sentMsg, err := bot.Send(user, "", &tb.SendOptions{
ReplyMarkup: &tb.ReplyMarkup{InlineKeyboard: menu},
DisableWebPagePreview: true,
ParseMode: tb.ModeMarkdown,
})
if err != nil {
log.Printf("Error sending message to user: %v", err)
return ErrSendMsg
}
err = setLastMsgPerUser(user.ID, sentMsg)
if err != nil {
log.Printf("Error setting last msg per user: %v", err)
return ErrSetLastMsg
}
}
msg, err := bot.EditReplyMarkup(storedMsg, &tb.ReplyMarkup{InlineKeyboard: menu})
if err != nil {
log.Printf("Error modifying previous message inlineKeyboard: %v", err)
return ErrSendMsg
}
err = setLastMsgPerUser(user.ID, msg)
if err != nil {
log.Printf("Error setting last msg per user: %v", err)
return ErrSetLastMsg
}
return nil
}
func sendMsg(user *tb.User, msg string, new bool) error { func sendMsg(user *tb.User, msg string, new bool) error {
sendMsgWithSpecificMenu(user, msg, nil, new) sendMsgWithSpecificMenu(user, msg, nil, new)
@@ -146,7 +179,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 map[userGroup]bool, 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 {
@@ -176,7 +209,7 @@ func setGroupMsg(msg *groupMsg, msgID int64) error {
return nil return nil
} }
func addUGToGroupMsg(msgID int64, group userGroup) error { func setUGInGroupMsg(msgID int64, group userGroup) error {
msg, err := redisClient.LIndex(groupMsgs, msgID).Result() msg, err := redisClient.LIndex(groupMsgs, msgID).Result()
if err != nil { if err != nil {
log.Printf("Error retriving group message from hash: %v", err) log.Printf("Error retriving group message from hash: %v", err)
@@ -188,12 +221,34 @@ func addUGToGroupMsg(msgID int64, group userGroup) error {
log.Printf("Error unmarshalling groupMsg: %v", err) log.Printf("Error unmarshalling groupMsg: %v", err)
return ErrJSONUnmarshall return ErrJSONUnmarshall
} }
jsonMsg.Group = append(jsonMsg.Group, group)
_, present := jsonMsg.Group[group]
if !present {
jsonMsg.Group[group] = true
} else {
jsonMsg.Group[group] = !jsonMsg.Group[group]
}
setGroupMsg(jsonMsg, msgID) setGroupMsg(jsonMsg, msgID)
return nil return nil
} }
func getUGInGroupMsg(msgID int64) (map[userGroup]bool, error) {
msg, err := redisClient.LIndex(groupMsgs, msgID).Result()
if err != nil {
log.Printf("Error retriving group message from hash: %v", err)
return nil, ErrRedisRetrieveHash
}
jsonMsg := &groupMsg{}
err = json.Unmarshal([]byte(msg), jsonMsg)
if err != nil {
log.Printf("Error unmarshalling groupMsg: %v", err)
return nil, ErrJSONUnmarshall
}
return jsonMsg.Group, 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 {
@@ -230,7 +285,10 @@ func sendMsgToGroup(msgID string) error {
return ErrSendMsg return ErrSendMsg
} }
} }
for _, group := range jsonMsg.Group { for group, is := range jsonMsg.Group {
if !is {
continue
}
users, err := getUsersInGroup(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)

View File

@@ -296,42 +296,12 @@ func sendMsgCmd(sender *tb.User, payload string, newMsg bool) {
log.Printf("Error in sending message: %v", err) log.Printf("Error in sending message: %v", err)
} }
} else { } else {
msgID, err := addNewGroupMsg(sender, []userGroup{}, payload) msgID, err := addNewGroupMsg(sender, map[userGroup]bool{}, payload)
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
} }
menu := getUserGroupMenu() menu := getGroupMsgMenu(msgID, sender)
menu[3] = append(menu[3], confirmSendBtn)
menu[0][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[0][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[1][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[1][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
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*" msg := "*Il messaggio che stai per inviare é *\n" + payload + "\n*Seleziona i gruppi a cui vuoi inviarlo*"
sendMsgWithSpecificMenu(sender, msg, menu, true) sendMsgWithSpecificMenu(sender, msg, menu, true)

View File

@@ -140,6 +140,93 @@ func getUserGroupMenu() [][]tb.InlineButton {
return ugMenu return ugMenu
} }
func getGroupMsgMenu(msgID int64, sender *tb.User) [][]tb.InlineButton {
menu := getUserGroupMenu()
menu[3] = append([]tb.InlineButton{confirmSendBtn}, menu[3]...)
menu[0][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[0][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[1][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
menu[1][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg"
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][0].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 = ""
}
ugInGroupMsg, err := getUGInGroupMsg(msgID)
if err != nil {
log.Printf("Error retrieving group to send groupMsg: %v", err)
}
for group, is := range ugInGroupMsg {
switch group {
case ugSoprano:
if is {
menu[0][0].Text = "*" + menu[0][0].Text + "*"
} else {
strings.Replace(menu[0][0].Text, "*", "", -1)
}
case ugContralto:
if is {
menu[0][1].Text = "*" + menu[0][1].Text + "*"
} else {
strings.Replace(menu[0][1].Text, "*", "", -1)
}
case ugTenore:
if is {
menu[1][0].Text = "*" + menu[1][0].Text + "*"
} else {
strings.Replace(menu[1][0].Text, "*", "", -1)
}
case ugBasso:
if is {
menu[1][1].Text = "*" + menu[1][1].Text + "*"
} else {
strings.Replace(menu[1][1].Text, "*", "", -1)
}
case ugCommissario:
if is {
menu[2][0].Text = "*" + menu[2][0].Text + "*"
} else {
strings.Replace(menu[2][0].Text, "*", "", -1)
}
case ugReferente:
if is {
menu[2][1].Text = "*" + menu[2][1].Text + "*"
} else {
strings.Replace(menu[2][1].Text, "*", "", -1)
}
case ugPreparatore:
if is {
menu[2][2].Text = "*" + menu[2][2].Text + "*"
} else {
strings.Replace(menu[2][2].Text, "*", "", -1)
}
}
}
return menu
}
func ugBtnCallback(c *tb.Callback, group userGroup) { func ugBtnCallback(c *tb.Callback, group userGroup) {
dataContent := strings.Split(c.Data, "+") dataContent := strings.Split(c.Data, "+")
if len(dataContent) <= 1 { if len(dataContent) <= 1 {
@@ -200,7 +287,7 @@ func ugBtnCallback(c *tb.Callback, group userGroup) {
} }
} }
} else if sendUg { } else if sendUg {
err = addUGToGroupMsg(msgID, group) err = setUGInGroupMsg(msgID, group)
if err != nil { if err != nil {
bot.Respond(c, &tb.CallbackResponse{ bot.Respond(c, &tb.CallbackResponse{
Text: errAlert, Text: errAlert,
@@ -212,6 +299,7 @@ func ugBtnCallback(c *tb.Callback, group userGroup) {
ShowAlert: true, ShowAlert: true,
}) })
} }
err = editLastMsgInlineKeyboard(c.Sender, getGroupMsgMenu(msgID, c.Sender))
} }
} }