tglistbot/main.go

317 lines
6.9 KiB
Go
Raw Permalink 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
"strings"
2022-12-26 13:41:45 -05:00
"sync"
"time"
"go.balki.me/anyhttp"
2023-03-29 17:22:11 -04:00
"go.balki.me/tglistbot/glist"
2022-12-23 23:43:19 -05:00
)
2023-04-05 11:17:07 -04:00
// Version will be set from build commandline
var Version string
2022-12-26 18:09:06 -05:00
var apiToken string
2022-12-23 23:43:19 -05:00
2024-01-16 11:09:17 -05:00
var usage = `Telegram List bot
2023-04-05 11:28:16 -04:00
Environment variables:
2024-12-19 16:12:51 -05:00
TGLB_API_TOKEN (required) : See https://core.telegram.org/bots#how-do-i-create-a-bot
TGLB_ADDR (default :28923) : See https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax
TGLB_DATA_PATH (default .) : Directory path where list data is stored
2023-04-05 11:28:16 -04:00
`
2022-12-23 23:43:19 -05:00
func main() {
2022-12-26 18:09:06 -05:00
log.SetFlags(log.Flags() | log.Lshortfile)
2023-03-29 17:22:11 -04:00
apiToken = os.Getenv("TGLB_API_TOKEN")
2022-12-26 18:09:06 -05:00
if apiToken == "" {
2023-04-05 11:28:16 -04:00
log.Print(usage)
log.Panicln("TGLB_API_TOKEN is empty")
2022-12-26 18:09:06 -05:00
}
addr := func() string {
2024-01-15 17:26:14 -05:00
addr := os.Getenv("TGLB_ADDR")
if addr == "" {
2024-12-19 16:12:51 -05:00
return ":28923"
2022-12-26 18:09:06 -05:00
}
return addr
2022-12-26 18:09:06 -05:00
}()
dataPath := func() string {
2023-03-29 17:22:11 -04:00
dataPath := os.Getenv("TGLB_DATA_PATH")
2022-12-26 18:09:06 -05:00
if dataPath == "" {
2023-03-08 10:20:37 -05:00
return "."
2022-12-26 18:09:06 -05:00
}
2023-03-08 10:20:37 -05:00
if err := os.MkdirAll(dataPath, 0755); err != nil {
2023-03-29 00:16:20 -04:00
log.Panicf("Failed to create datapath, path: %q, err: %s\n", dataPath, err)
2022-12-26 18:09:06 -05:00
}
return dataPath
}()
glist.DataPath = dataPath
2023-04-05 11:17:07 -04:00
version := func() string {
if Version != "" {
return Version
}
2023-03-08 10:20:37 -05:00
if bi, ok := debug.ReadBuildInfo(); ok {
for _, s := range bi.Settings {
if s.Key == "vcs.revision" {
return s.Value[:8]
}
2022-12-27 22:24:27 -05:00
}
}
2023-03-08 10:20:37 -05:00
return "unknown"
}()
2022-12-27 22:24:27 -05:00
log.Printf("List bot (%s) starting with datapath: %q, %s\n", version, dataPath, addr)
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 {
2023-03-21 14:24:53 -04:00
log.Panicf("failed to load data, err: %s\n", err)
2022-12-26 18:09:06 -05:00
}
botPath := fmt.Sprintf("/bot%s", apiToken)
http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) {
2023-03-08 10:20:37 -05:00
defer func() {
if _, err := w.Write([]byte("ok")); err != nil {
log.Println(err)
}
}()
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
if len(update.Message.Text) > 60 {
replyURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%d&reply_to_message_id=%d&text=ItemTooLong-Max60", apiToken, chatID, update.Message.ID)
resp, err := http.Get(replyURL)
if err != nil {
log.Println(err)
return
}
logBody(resp.Body)
return
}
2022-12-26 13:41:45 -05:00
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 {
2023-03-29 00:16:20 -04:00
log.Printf("Chat not found: %v\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
}
})
2024-12-19 16:12:51 -05:00
err := anyhttp.ListenAndServe(addr, nil)
2024-01-06 14:07:44 -05:00
if err != nil {
log.Panicln(err)
}
2022-12-23 23:43:19 -05:00
}
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(3*time.Second, func() {
2022-12-26 13:41:45 -05:00
gl.Mutex.Lock()
defer gl.Mutex.Unlock()
if count == gl.AllMsgCounter {
2023-04-05 07:32:11 -04:00
resp := sendList(gl, glist.NEWLIST)
2022-12-28 13:59:06 -05:00
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 {
2023-04-05 07:32:11 -04:00
sendList(gl, glist.EDITLIST)
}
2022-12-26 13:41:45 -05:00
}
2023-04-05 07:32:11 -04:00
func sendList(gl *glist.GList, method glist.SendMethod) []byte {
2022-12-26 13:41:45 -05:00
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method)
2023-04-05 07:32:11 -04:00
sendMsgReq, err := gl.GenSendListReq(method)
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
}
log.Println(string(sendMsgReq))
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
}
2023-03-21 13:38:20 -04:00
body := logBody(resp.Body)
ok := struct {
Ok bool `json:"ok"`
}{}
if err := json.Unmarshal(body, &ok); err != nil {
log.Println(err)
return
}
//Old messages can't be deleted, so edit text instead
if !ok.Ok {
updateURL := fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText?chat_id=%d&message_id=%d&text=%s", apiToken, chatID, messageID, "<list updated>")
resp, err := http.Get(updateURL)
if err != nil {
log.Println(err)
return
}
logBody(resp.Body)
}
2022-12-25 13:40:40 -05:00
}
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 {
2023-03-29 00:16:20 -04:00
return fmt.Errorf("failed read file, name: %q, err:%w", name, err)
2022-12-26 18:09:06 -05:00
}
err = json.Unmarshal(data, &gl)
if err != nil {
2023-03-29 00:16:20 -04:00
return fmt.Errorf("failed to parse data, data:%q, err:%w", data, err)
2022-12-26 18:09:06 -05:00
}
chats.Store(gl.ChatID, &gl)
}
return nil
}