Disable notificaiton for new list

This commit is contained in:
Balakrishnan Balasubramanian 2023-04-05 07:32:11 -04:00
parent 0b1d455bc5
commit 347823b0bd
3 changed files with 23 additions and 11 deletions

View File

@ -109,6 +109,13 @@ func (g *GList) ClearChecked() {
persistReqC <- g persistReqC <- g
} }
type SendMethod string
var (
NEWLIST SendMethod = "sendMessage"
EDITLIST SendMethod = "editMessageText"
)
type button struct { type button struct {
Text string `json:"text"` Text string `json:"text"`
CallbackData string `json:"callback_data"` CallbackData string `json:"callback_data"`
@ -118,6 +125,7 @@ type newListReq struct {
ChatID int `json:"chat_id"` ChatID int `json:"chat_id"`
MessageID *int `json:"message_id,omitempty"` MessageID *int `json:"message_id,omitempty"`
Text string `json:"text"` Text string `json:"text"`
DisableNotification *bool `json:"disable_notification,omitempty"`
ReplyMarkup struct { ReplyMarkup struct {
InlineKeyboard [][]button `json:"inline_keyboard"` InlineKeyboard [][]button `json:"inline_keyboard"`
} `json:"reply_markup"` } `json:"reply_markup"`
@ -147,8 +155,12 @@ func makeButtons(items []Entry) [][]button {
return buttons return buttons
} }
func (g *GList) GenSendListReq() ([]byte, error) { func (g *GList) GenSendListReq(method SendMethod) ([]byte, error) {
req := newListReq{ChatID: g.ChatID, MessageID: g.MessageID, Text: "List:"} req := newListReq{ChatID: g.ChatID, MessageID: g.MessageID, Text: "List:"}
if method == NEWLIST {
disableNotification := true
req.DisableNotification = &disableNotification
}
itemButtons := makeButtons(g.Items) itemButtons := makeButtons(g.Items)
controlButtons := []button{{"clear checked", "clear"}} controlButtons := []button{{"clear checked", "clear"}}
req.ReplyMarkup.InlineKeyboard = append(itemButtons, controlButtons) req.ReplyMarkup.InlineKeyboard = append(itemButtons, controlButtons)

View File

@ -7,12 +7,12 @@ import (
func TestGList(t *testing.T) { func TestGList(t *testing.T) {
g := NewGList(4342, "foo") g := NewGList(4342, "foo")
data, err := g.GenSendListReq() data, err := g.GenSendListReq(NEWLIST)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}` expected := `{"chat_id":4342,"text":"List:","disable_notification":true,"reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}`
if expected != string(data) { if expected != string(data) {
t.Fatalf("expected: %s\n got:%s\n", expected, string(data)) t.Fatalf("expected: %s\n got:%s\n", expected, string(data))
} }

View File

@ -177,7 +177,7 @@ func handleTextAdded(gl *glist.GList, text string) {
gl.Mutex.Lock() gl.Mutex.Lock()
defer gl.Mutex.Unlock() defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter { if count == gl.AllMsgCounter {
resp := sendList(gl, "sendMessage") resp := sendList(gl, glist.NEWLIST)
if resp == nil { if resp == nil {
return return
} }
@ -219,13 +219,13 @@ func handleButtonClick(gl *glist.GList, messageID int, text string) {
deleteMessage(gl.ChatID, messageID) deleteMessage(gl.ChatID, messageID)
gl.MessageID = nil gl.MessageID = nil
} else { } else {
sendList(gl, "editMessageText") sendList(gl, glist.EDITLIST)
} }
} }
func sendList(gl *glist.GList, method string) []byte { func sendList(gl *glist.GList, method glist.SendMethod) []byte {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method) url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method)
sendMsgReq, err := gl.GenSendListReq() sendMsgReq, err := gl.GenSendListReq(method)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil return nil