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.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
if err := loadData(dataPath, &chats); err != nil {
@ -67,6 +67,7 @@ func main() {
return
}
log.Println(string(body))
update := struct {
Message *struct {
ID int `json:"message_id"`
@ -86,13 +87,13 @@ func main() {
Data string
} `json:"callback_query"`
}{}
err = json.Unmarshal(body, &update)
if err != nil {
if err := json.Unmarshal(body, &update); err != nil {
log.Println(err)
return
}
if update.Message != nil {
if update.Message != nil && update.Message.Text != "" {
chatID := update.Message.Chat.ID
g, _ := chats.LoadOrStore(chatID, glist.NewGList(chatID))
@ -118,15 +119,12 @@ func main() {
}
func handleTextAdded(gl *glist.GList, text string) {
if text == "" {
return
}
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
gl.Add(text)
count := gl.AllMsgCounter + 1
gl.AllMsgCounter = count
time.AfterFunc(30*time.Second, func() {
time.AfterFunc(10*time.Second, func() {
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter {
@ -234,49 +232,3 @@ func loadData(dataPath string, chats *sync.Map) error {
}
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"
*/