tglistbot/main.go

269 lines
5.6 KiB
Go
Raw Normal View History

2022-12-26 18:09:06 -05:00
// This is the main package
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 18:09:06 -05:00
"os"
"path"
2022-12-27 22:24:27 -05:00
"runtime/debug"
2022-12-26 18:09:06 -05:00
"strconv"
"strings"
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-26 18:09:06 -05:00
var apiToken string
2022-12-23 23:43:19 -05:00
func main() {
2022-12-26 18:09:06 -05:00
apiToken = os.Getenv("CHKBOT_API_TOKEN")
if apiToken == "" {
log.Panicln("CHKBOT_API_TOKEN is empty")
}
port := func() int {
portStr := os.Getenv("CHKBOT_PORT")
port, err := strconv.Atoi(portStr)
if err == nil {
return port
}
return 28923
}()
dataPath := func() string {
dataPath := os.Getenv("CHKBOT_DATA_PATH")
if dataPath == "" {
dataPath = "."
}
err := os.MkdirAll(dataPath, 0755)
if err != nil {
log.Panicf("Failed to create datapath, path: %s, err: %s", dataPath, err)
}
return dataPath
}()
glist.DataPath = dataPath
2022-12-27 22:24:27 -05:00
commit := "unknown"
if bi, ok := debug.ReadBuildInfo(); ok {
for _, s := range bi.Settings {
if s.Key == "vcs.revision" {
commit = s.Value[:8]
}
}
}
log.Printf("Grocery List bot (%s) starting with datapath:%s, port:%d\n", commit, dataPath, port)
2022-12-26 18:09:06 -05:00
2022-12-26 13:41:45 -05:00
var chats sync.Map
2022-12-26 18:09:06 -05:00
if err := loadData(dataPath, &chats); err != nil {
log.Panicf("failed to load data, err: %s", err)
}
botPath := fmt.Sprintf("/bot%s", apiToken)
http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) {
2022-12-28 01:36:04 -05:00
_, err := w.Write([]byte("ok"))
if err != nil {
log.Println(err)
return
}
2022-12-23 23:43:19 -05:00
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
2022-12-26 22:10:06 -05:00
2022-12-23 23:43:19 -05:00
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"`
}{}
2022-12-26 22:10:06 -05:00
if err := json.Unmarshal(body, &update); err != nil {
2022-12-23 23:43:19 -05:00
log.Println(err)
return
}
2022-12-28 00:15:03 -05:00
// Ignore if Text is empty or is a command
if update.Message != nil && update.Message.Text != "" && update.Message.Text[0] != '/' {
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-26 18:09:06 -05:00
2022-12-23 23:43:19 -05:00
} else if update.CallbackQuery != nil {
2022-12-26 18:09:06 -05:00
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 {
2022-12-28 01:36:04 -05:00
log.Printf("Chat not found: %d\n", chatID)
2022-12-26 13:41:45 -05:00
return
}
gl := g.(*glist.GList)
go handleButtonClick(gl, update.CallbackQuery.Message.ID, update.CallbackQuery.Data)
2022-12-23 23:43:19 -05:00
}
})
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
2022-12-26 22:10:06 -05:00
time.AfterFunc(10*time.Second, func() {
2022-12-26 13:41:45 -05:00
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter {
2022-12-28 13:59:06 -05:00
resp := sendList(gl, "sendMessage")
if resp == nil {
return
}
response := struct {
Ok bool `json:"ok"`
Result struct {
MessageID int `json:"message_id"`
} `json:"result"`
}{}
if err := json.Unmarshal(resp, &response); err != nil {
log.Println(err)
return
}
if !response.Ok {
log.Println("not ok")
return
}
2022-12-27 22:47:26 -05:00
if gl.MessageID != nil {
deleteMessage(gl.ChatID, *gl.MessageID)
}
2022-12-28 13:59:06 -05:00
gl.MessageID = &response.Result.MessageID
2022-12-26 13:41:45 -05:00
}
})
}
func handleButtonClick(gl *glist.GList, messageID int, text string) {
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
gl.MessageID = &messageID
if text == "clear" {
gl.ClearChecked()
2022-12-26 13:41:45 -05:00
} else {
gl.Toggle(text)
}
if len(gl.Items) == 0 {
deleteMessage(gl.ChatID, messageID)
2022-12-27 22:47:26 -05:00
gl.MessageID = nil
} else {
sendList(gl, "editMessageText")
}
2022-12-26 13:41:45 -05:00
}
2022-12-28 13:59:06 -05:00
func sendList(gl *glist.GList, method string) []byte {
2022-12-26 13:41:45 -05:00
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)
2022-12-28 13:59:06 -05:00
return nil
2022-12-25 01:01:35 -05:00
}
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)
2022-12-28 13:59:06 -05:00
return nil
2022-12-25 01:01:35 -05:00
}
2022-12-28 13:59:06 -05:00
return logBody(resp.Body)
2022-12-25 01:01:35 -05:00
}
2022-12-26 13:41:45 -05:00
func answerCallbackQuery(callbackQueryID string) {
2022-12-26 18:09:06 -05:00
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-26 18:09:06 -05:00
deleteURL := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", apiToken, chatID, messageID)
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)
}
2022-12-28 13:59:06 -05:00
func logBody(respBody io.ReadCloser) []byte {
2022-12-25 13:40:40 -05:00
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)
2022-12-28 13:59:06 -05:00
return nil
2022-12-23 23:43:19 -05:00
}
log.Println(string(body))
2022-12-28 13:59:06 -05:00
return body
2022-12-23 23:43:19 -05:00
}
2022-12-26 18:09:06 -05:00
func loadData(dataPath string, chats *sync.Map) error {
items, err := os.ReadDir(dataPath)
if err != nil {
return err
}
for _, de := range items {
if de.IsDir() {
continue
}
name := de.Name()
if !strings.HasPrefix(name, "chkchat") {
continue
}
var gl glist.GList
data, err := os.ReadFile(path.Join(dataPath, name))
if err != nil {
return fmt.Errorf("failed read file, name: %s, err:%w", name, err)
}
err = json.Unmarshal(data, &gl)
if err != nil {
return fmt.Errorf("failed to parse data, data:%s, err:%w", data, err)
}
chats.Store(gl.ChatID, &gl)
}
return nil
}