From 79b69be83f0bc7479ee92633ffa3cdbaf0d77630 Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Fri, 17 Apr 2020 00:46:26 +0200 Subject: [PATCH] Continued integrating interactive sendMsg Signed-off-by: Ettore Dreucci --- src/manageMsg.go | 76 +++++++++++++++++++++++++++++----- src/telegramCommands.go | 34 +--------------- src/telegramMenus.go | 90 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 158 insertions(+), 42 deletions(-) diff --git a/src/manageMsg.go b/src/manageMsg.go index 468bbcd..3c97a89 100644 --- a/src/manageMsg.go +++ b/src/manageMsg.go @@ -10,11 +10,11 @@ import ( ) 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"` + SenderID int `sql:"sender_id" json:"sender_id"` + Group map[userGroup]bool `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 { @@ -67,6 +67,39 @@ func getLastMsgPerUser(userID int) (*tb.StoredMessage, error) { 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 { sendMsgWithSpecificMenu(user, msg, nil, new) @@ -146,7 +179,7 @@ func sendMsgWithSpecificMenu(user *tb.User, msg string, menu [][]tb.InlineButton 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} jsonMsg, err := json.Marshal(newGroupMsg) if err != nil { @@ -176,7 +209,7 @@ func setGroupMsg(msg *groupMsg, msgID int64) error { return nil } -func addUGToGroupMsg(msgID int64, group userGroup) error { +func setUGInGroupMsg(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) @@ -188,12 +221,34 @@ func addUGToGroupMsg(msgID int64, group userGroup) error { log.Printf("Error unmarshalling groupMsg: %v", err) 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) 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 { ID, err := strconv.ParseInt(msgID, 10, 64) if err != nil { @@ -230,7 +285,10 @@ func sendMsgToGroup(msgID string) error { return ErrSendMsg } } - for _, group := range jsonMsg.Group { + for group, is := range jsonMsg.Group { + if !is { + continue + } users, err := getUsersInGroup(group) if err != nil { log.Printf("Error retrieving users in sendTo group: %v", err) diff --git a/src/telegramCommands.go b/src/telegramCommands.go index 69e1b3b..fefbf6b 100644 --- a/src/telegramCommands.go +++ b/src/telegramCommands.go @@ -296,42 +296,12 @@ func sendMsgCmd(sender *tb.User, payload string, newMsg bool) { log.Printf("Error in sending message: %v", err) } } else { - msgID, err := addNewGroupMsg(sender, []userGroup{}, payload) + msgID, err := addNewGroupMsg(sender, map[userGroup]bool{}, payload) if err != nil { log.Printf("Error adding new groupMsg in db: %v", err) return } - menu := getUserGroupMenu() - 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 = "" - } + menu := getGroupMsgMenu(msgID, sender) msg := "*Il messaggio che stai per inviare é *\n" + payload + "\n*Seleziona i gruppi a cui vuoi inviarlo*" sendMsgWithSpecificMenu(sender, msg, menu, true) diff --git a/src/telegramMenus.go b/src/telegramMenus.go index f26daed..fa1a1c6 100644 --- a/src/telegramMenus.go +++ b/src/telegramMenus.go @@ -140,6 +140,93 @@ func getUserGroupMenu() [][]tb.InlineButton { 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) { dataContent := strings.Split(c.Data, "+") if len(dataContent) <= 1 { @@ -200,7 +287,7 @@ func ugBtnCallback(c *tb.Callback, group userGroup) { } } } else if sendUg { - err = addUGToGroupMsg(msgID, group) + err = setUGInGroupMsg(msgID, group) if err != nil { bot.Respond(c, &tb.CallbackResponse{ Text: errAlert, @@ -212,6 +299,7 @@ func ugBtnCallback(c *tb.Callback, group userGroup) { ShowAlert: true, }) } + err = editLastMsgInlineKeyboard(c.Sender, getGroupMsgMenu(msgID, c.Sender)) } }