tglistbot/main.go

206 lines
4.3 KiB
Go
Raw Normal View History

2022-12-23 23:43:19 -05:00
package main
import (
2022-12-26 13:41:45 -05:00
"bytes"
2022-12-23 23:43:19 -05:00
"encoding/json"
"fmt"
"io"
"log"
"net/http"
2022-12-26 13:41:45 -05:00
"sync"
"time"
"gitea.balki.me/telegram-msgchkbox/glist"
2022-12-23 23:43:19 -05:00
)
2022-12-23 23:50:09 -05:00
var apiToken = "421791796:AAE4wPbcqfLP1GNeGR3RTBiyX16fCj3HPAM"
2022-12-23 23:43:19 -05:00
func main() {
2022-12-26 13:41:45 -05:00
var chats sync.Map
2022-12-23 23:43:19 -05:00
fmt.Println("vim-go")
http.HandleFunc("/zeexkfcsdjpmncirkyelwzotjmmefcqtcogrfwnafidionxiacwnslwuhbwfuppjgwzbmazd", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
update := struct {
Message *struct {
ID int `json:"message_id"`
Chat struct {
ID int `json:"id"`
}
Text string
}
CallbackQuery *struct {
ID string `json:"id"`
Message struct {
ID int `json:"message_id"`
Chat struct {
ID int `json:"id"`
}
}
2022-12-25 01:01:35 -05:00
Data string
2022-12-23 23:43:19 -05:00
} `json:"callback_query"`
}{}
err = json.Unmarshal(body, &update)
if err != nil {
log.Println(err)
return
}
if update.Message != nil {
2022-12-26 13:41:45 -05:00
chatID := update.Message.Chat.ID
g, _ := chats.LoadOrStore(chatID, glist.NewGList(chatID))
gl := g.(*glist.GList)
go handleTextAdded(gl, update.Message.Text)
2022-12-23 23:43:19 -05:00
} else if update.CallbackQuery != nil {
2022-12-26 13:41:45 -05:00
defer func() { go answerCallbackQuery(update.CallbackQuery.ID) }()
chatID := update.CallbackQuery.Message.Chat.ID
g, ok := chats.Load(chatID)
if !ok {
log.Println("Chat not found: %s", chatID)
return
}
gl := g.(*glist.GList)
go handleButtonClick(gl, update.CallbackQuery.Message.ID, update.CallbackQuery.Data)
2022-12-23 23:43:19 -05:00
}
})
port := 28923
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
2022-12-26 13:41:45 -05:00
func handleTextAdded(gl *glist.GList, text string) {
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
gl.Add(text)
count := gl.AllMsgCounter + 1
gl.AllMsgCounter = count
time.AfterFunc(30*time.Second, func() {
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter {
sendList(gl, "sendMessage")
}
})
}
func handleButtonClick(gl *glist.GList, messageID int, text string) {
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
if gl.MessageID != nil {
if messageID != *gl.MessageID {
go deleteMessage(gl.ChatID, *gl.MessageID)
}
}
gl.MessageID = &messageID
if text == "clear" {
gl.ClearChecked()
} else {
gl.Toggle(text)
}
sendList(gl, "editMessageText")
}
func sendList(gl *glist.GList, method string) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method)
sendMsgReq, err := gl.GenSendListReq()
2022-12-25 01:01:35 -05:00
if err != nil {
log.Println(err)
return
}
2022-12-26 13:41:45 -05:00
resp, err := http.Post(url, "application/json", bytes.NewReader(sendMsgReq))
2022-12-25 01:01:35 -05:00
if err != nil {
log.Println(err)
return
}
2022-12-25 13:40:40 -05:00
logBody(resp.Body)
2022-12-25 01:01:35 -05:00
}
2022-12-26 13:41:45 -05:00
func answerCallbackQuery(callbackQueryID string) {
answerUrl := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", apiToken, callbackQueryID)
resp, err := http.Get(answerUrl)
2022-12-23 23:43:19 -05:00
if err != nil {
log.Println(err)
return
}
2022-12-25 13:40:40 -05:00
logBody(resp.Body)
}
2022-12-26 13:41:45 -05:00
func deleteMessage(chatID int, messageID int) {
2022-12-23 23:50:09 -05:00
deleteUrl := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", apiToken, chatID, messageID)
2022-12-25 13:40:40 -05:00
resp, err := http.Get(deleteUrl)
2022-12-23 23:43:19 -05:00
if err != nil {
log.Println(err)
return
}
2022-12-25 13:40:40 -05:00
logBody(resp.Body)
}
func logBody(respBody io.ReadCloser) {
defer func() {
err := respBody.Close()
if err != nil {
log.Println(err)
}
}()
body, err := io.ReadAll(respBody)
2022-12-23 23:43:19 -05:00
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
}
2022-12-26 13:41:45 -05:00
/* Example data
2022-12-23 23:43:19 -05:00
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"
*/