Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
dd62f237b0 | |||
107d90deb8 | |||
6f111a42e9 | |||
2aa043b5fc | |||
347823b0bd |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
tglistbot*
|
12
Makefile
Normal file
12
Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
VERSION = $(shell git describe --tags --abbrev=0)
|
||||||
|
|
||||||
|
build_release:
|
||||||
|
CGO_ENABLED=0 go build -buildmode=pie -ldflags "-X main.Version=$(VERSION)" -o tglistbot-$(VERSION)
|
||||||
|
|
||||||
|
release_check:
|
||||||
|
go list -m go.balki.me/tglistbot@$(VERSION)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -i tglistbot* || true
|
||||||
|
|
@ -109,16 +109,24 @@ func (g *GList) ClearChecked() {
|
|||||||
persistReqC <- g
|
persistReqC <- g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SendMethod string
|
||||||
|
|
||||||
|
var (
|
||||||
|
NEWLIST SendMethod = "sendMessage"
|
||||||
|
EDITLIST SendMethod = "editMessageText"
|
||||||
|
)
|
||||||
|
|
||||||
type button struct {
|
type button struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
CallbackData string `json:"callback_data"`
|
CallbackData string `json:"callback_data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type newListReq struct {
|
type newListReq struct {
|
||||||
ChatID int `json:"chat_id"`
|
ChatID int `json:"chat_id"`
|
||||||
MessageID *int `json:"message_id,omitempty"`
|
MessageID *int `json:"message_id,omitempty"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
ReplyMarkup struct {
|
DisableNotification *bool `json:"disable_notification,omitempty"`
|
||||||
|
ReplyMarkup struct {
|
||||||
InlineKeyboard [][]button `json:"inline_keyboard"`
|
InlineKeyboard [][]button `json:"inline_keyboard"`
|
||||||
} `json:"reply_markup"`
|
} `json:"reply_markup"`
|
||||||
}
|
}
|
||||||
@ -147,8 +155,12 @@ func makeButtons(items []Entry) [][]button {
|
|||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GList) GenSendListReq() ([]byte, error) {
|
func (g *GList) GenSendListReq(method SendMethod) ([]byte, error) {
|
||||||
req := newListReq{ChatID: g.ChatID, MessageID: g.MessageID, Text: "List:"}
|
req := newListReq{ChatID: g.ChatID, MessageID: g.MessageID, Text: "List:"}
|
||||||
|
if method == NEWLIST {
|
||||||
|
disableNotification := true
|
||||||
|
req.DisableNotification = &disableNotification
|
||||||
|
}
|
||||||
itemButtons := makeButtons(g.Items)
|
itemButtons := makeButtons(g.Items)
|
||||||
controlButtons := []button{{"clear checked", "clear"}}
|
controlButtons := []button{{"clear checked", "clear"}}
|
||||||
req.ReplyMarkup.InlineKeyboard = append(itemButtons, controlButtons)
|
req.ReplyMarkup.InlineKeyboard = append(itemButtons, controlButtons)
|
||||||
|
@ -7,12 +7,12 @@ import (
|
|||||||
|
|
||||||
func TestGList(t *testing.T) {
|
func TestGList(t *testing.T) {
|
||||||
g := NewGList(4342, "foo")
|
g := NewGList(4342, "foo")
|
||||||
data, err := g.GenSendListReq()
|
data, err := g.GenSendListReq(NEWLIST)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}`
|
expected := `{"chat_id":4342,"text":"List:","disable_notification":true,"reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}`
|
||||||
if expected != string(data) {
|
if expected != string(data) {
|
||||||
t.Fatalf("expected: %s\n got:%s\n", expected, string(data))
|
t.Fatalf("expected: %s\n got:%s\n", expected, string(data))
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
|||||||
module go.balki.me/tglistbot
|
module go.balki.me/tglistbot
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
|
require go.balki.me/anyhttp v0.1.0
|
||||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
go.balki.me/anyhttp v0.1.0 h1:ULzLWS1pRWMEduHHJxXCbvxoTmxNaWSNANV9gQ0Pigw=
|
||||||
|
go.balki.me/anyhttp v0.1.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
|
75
main.go
75
main.go
@ -4,47 +4,47 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.balki.me/anyhttp"
|
||||||
"go.balki.me/tglistbot/glist"
|
"go.balki.me/tglistbot/glist"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Version will be set from build commandline
|
||||||
|
var Version string
|
||||||
var apiToken string
|
var apiToken string
|
||||||
|
|
||||||
|
var usage string = `Telegram List bot
|
||||||
|
Environment variables:
|
||||||
|
TGLB_API_TOKEN (required): See https://core.telegram.org/bots#how-do-i-create-a-bot
|
||||||
|
TGLB_PORT (default 28923): Set numerical port or unix//run/path.sock for unix socket
|
||||||
|
TGLB_DATA_PATH (default .): Directory path where list data is stored
|
||||||
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
apiToken = os.Getenv("TGLB_API_TOKEN")
|
apiToken = os.Getenv("TGLB_API_TOKEN")
|
||||||
|
|
||||||
if apiToken == "" {
|
if apiToken == "" {
|
||||||
log.Panicln("TG_API_TOKEN is empty")
|
log.Print(usage)
|
||||||
|
log.Panicln("TGLB_API_TOKEN is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
port, unixSocketPath := func() (int, string) {
|
addr := func() string {
|
||||||
portStr := os.Getenv("TGLB_PORT")
|
addr := os.Getenv("TGLB_PORT")
|
||||||
|
if addr == "" {
|
||||||
defaultPort := 28923
|
return "28923"
|
||||||
|
|
||||||
if strings.HasPrefix(portStr, "unix/") {
|
|
||||||
return defaultPort, strings.TrimPrefix(portStr, "unix/")
|
|
||||||
}
|
}
|
||||||
|
return addr
|
||||||
if port, err := strconv.Atoi(portStr); err == nil {
|
|
||||||
return port, ""
|
|
||||||
}
|
|
||||||
return defaultPort, ""
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
dataPath := func() string {
|
dataPath := func() string {
|
||||||
@ -61,7 +61,10 @@ func main() {
|
|||||||
|
|
||||||
glist.DataPath = dataPath
|
glist.DataPath = dataPath
|
||||||
|
|
||||||
commit := func() string {
|
version := func() string {
|
||||||
|
if Version != "" {
|
||||||
|
return Version
|
||||||
|
}
|
||||||
if bi, ok := debug.ReadBuildInfo(); ok {
|
if bi, ok := debug.ReadBuildInfo(); ok {
|
||||||
for _, s := range bi.Settings {
|
for _, s := range bi.Settings {
|
||||||
if s.Key == "vcs.revision" {
|
if s.Key == "vcs.revision" {
|
||||||
@ -72,14 +75,7 @@ func main() {
|
|||||||
return "unknown"
|
return "unknown"
|
||||||
}()
|
}()
|
||||||
|
|
||||||
listeningOn := func() string {
|
log.Printf("List bot (%s) starting with datapath: %q, %s\n", version, dataPath, addr)
|
||||||
if unixSocketPath != "" {
|
|
||||||
return fmt.Sprintf("socket: %q", unixSocketPath)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("port: %v", port)
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Printf("List bot (%s) starting with datapath: %q, %s\n", commit, dataPath, listeningOn)
|
|
||||||
|
|
||||||
var chats sync.Map
|
var chats sync.Map
|
||||||
if err := loadData(dataPath, &chats); err != nil {
|
if err := loadData(dataPath, &chats); err != nil {
|
||||||
@ -147,24 +143,7 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if unixSocketPath != "" {
|
log.Panicln(anyhttp.ListenAndServe(addr, nil))
|
||||||
// Remove old one
|
|
||||||
if err := os.Remove(unixSocketPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
||||||
log.Panicf("Failed to remove unix socket : %q err: %v\n", unixSocketPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := net.Listen("unix", unixSocketPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Unable to listen to unix socket : %q err: %v\n", unixSocketPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = os.Chmod(unixSocketPath, 0666); err != nil {
|
|
||||||
log.Panicf("Failed to set permission of unix socket %q err: %v\n", unixSocketPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Panicln(http.Serve(l, nil))
|
|
||||||
}
|
|
||||||
log.Panicln(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleTextAdded(gl *glist.GList, text string) {
|
func handleTextAdded(gl *glist.GList, text string) {
|
||||||
@ -177,7 +156,7 @@ func handleTextAdded(gl *glist.GList, text string) {
|
|||||||
gl.Mutex.Lock()
|
gl.Mutex.Lock()
|
||||||
defer gl.Mutex.Unlock()
|
defer gl.Mutex.Unlock()
|
||||||
if count == gl.AllMsgCounter {
|
if count == gl.AllMsgCounter {
|
||||||
resp := sendList(gl, "sendMessage")
|
resp := sendList(gl, glist.NEWLIST)
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -219,13 +198,13 @@ func handleButtonClick(gl *glist.GList, messageID int, text string) {
|
|||||||
deleteMessage(gl.ChatID, messageID)
|
deleteMessage(gl.ChatID, messageID)
|
||||||
gl.MessageID = nil
|
gl.MessageID = nil
|
||||||
} else {
|
} else {
|
||||||
sendList(gl, "editMessageText")
|
sendList(gl, glist.EDITLIST)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendList(gl *glist.GList, method string) []byte {
|
func sendList(gl *glist.GList, method glist.SendMethod) []byte {
|
||||||
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method)
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", apiToken, method)
|
||||||
sendMsgReq, err := gl.GenSendListReq()
|
sendMsgReq, err := gl.GenSendListReq(method)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user