some cleanup

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-26 22:10:06 -05:00
parent 400411cfbc
commit 99994c6722

60
main.go
View File

@ -52,7 +52,7 @@ func main() {
glist.DataPath = dataPath glist.DataPath = dataPath
glist.DoPersist() glist.DoPersist()
log.Printf("List bot starting with datapath:%s, port:%d\n", dataPath, port) log.Printf("Grocery List bot starting with datapath:%s, port:%d\n", dataPath, port)
var chats sync.Map var chats sync.Map
if err := loadData(dataPath, &chats); err != nil { if err := loadData(dataPath, &chats); err != nil {
@ -67,6 +67,7 @@ func main() {
return return
} }
log.Println(string(body)) log.Println(string(body))
update := struct { update := struct {
Message *struct { Message *struct {
ID int `json:"message_id"` ID int `json:"message_id"`
@ -86,13 +87,13 @@ func main() {
Data string Data string
} `json:"callback_query"` } `json:"callback_query"`
}{} }{}
err = json.Unmarshal(body, &update)
if err != nil { if err := json.Unmarshal(body, &update); err != nil {
log.Println(err) log.Println(err)
return return
} }
if update.Message != nil { if update.Message != nil && update.Message.Text != "" {
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
g, _ := chats.LoadOrStore(chatID, glist.NewGList(chatID)) g, _ := chats.LoadOrStore(chatID, glist.NewGList(chatID))
@ -118,15 +119,12 @@ func main() {
} }
func handleTextAdded(gl *glist.GList, text string) { func handleTextAdded(gl *glist.GList, text string) {
if text == "" {
return
}
gl.Mutex.Lock() gl.Mutex.Lock()
defer gl.Mutex.Unlock() defer gl.Mutex.Unlock()
gl.Add(text) gl.Add(text)
count := gl.AllMsgCounter + 1 count := gl.AllMsgCounter + 1
gl.AllMsgCounter = count gl.AllMsgCounter = count
time.AfterFunc(30*time.Second, func() { time.AfterFunc(10*time.Second, func() {
gl.Mutex.Lock() gl.Mutex.Lock()
defer gl.Mutex.Unlock() defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter { if count == gl.AllMsgCounter {
@ -234,49 +232,3 @@ func loadData(dataPath string, chats *sync.Map) error {
} }
return nil return nil
} }
/* Example data
update_id: 547400623
message:
message_id: 869
from:
id: 385713421
is_bot: false
first_name: Balakrishnan
language_code: en
chat:
id: -832015899
title: Playbot
type: group
all_members_are_administrators: true
date: 1671850862
text: Hi
update_id: 547400624
callback_query:
id: "1656626530601531051"
from:
id: 385713421
is_bot: false
first_name: Balakrishnan
language_code: en
message:
message_id: 868
from:
id: 421791796
is_bot: true
first_name: grocery_guy
username: grocery_guy_bot
chat:
id: -832015899
title: Playbot
type: group
all_members_are_administrators: true
date: 1671074930
text: foo
reply_markup:
inline_keyboard:
- - text: blah
callback_data: "42"
chat_instance: "5165047605234327446"
data: "42"
*/