Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
dd62f237b0 | |||
107d90deb8 | |||
6f111a42e9 | |||
2aa043b5fc |
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
|
||||
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module go.balki.me/tglistbot
|
||||
|
||||
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=
|
67
main.go
67
main.go
@ -4,47 +4,47 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.balki.me/anyhttp"
|
||||
"go.balki.me/tglistbot/glist"
|
||||
)
|
||||
|
||||
// Version will be set from build commandline
|
||||
var Version 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() {
|
||||
|
||||
apiToken = os.Getenv("TGLB_API_TOKEN")
|
||||
|
||||
if apiToken == "" {
|
||||
log.Panicln("TG_API_TOKEN is empty")
|
||||
log.Print(usage)
|
||||
log.Panicln("TGLB_API_TOKEN is empty")
|
||||
}
|
||||
|
||||
port, unixSocketPath := func() (int, string) {
|
||||
portStr := os.Getenv("TGLB_PORT")
|
||||
|
||||
defaultPort := 28923
|
||||
|
||||
if strings.HasPrefix(portStr, "unix/") {
|
||||
return defaultPort, strings.TrimPrefix(portStr, "unix/")
|
||||
addr := func() string {
|
||||
addr := os.Getenv("TGLB_PORT")
|
||||
if addr == "" {
|
||||
return "28923"
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(portStr); err == nil {
|
||||
return port, ""
|
||||
}
|
||||
return defaultPort, ""
|
||||
return addr
|
||||
}()
|
||||
|
||||
dataPath := func() string {
|
||||
@ -61,7 +61,10 @@ func main() {
|
||||
|
||||
glist.DataPath = dataPath
|
||||
|
||||
commit := func() string {
|
||||
version := func() string {
|
||||
if Version != "" {
|
||||
return Version
|
||||
}
|
||||
if bi, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, s := range bi.Settings {
|
||||
if s.Key == "vcs.revision" {
|
||||
@ -72,14 +75,7 @@ func main() {
|
||||
return "unknown"
|
||||
}()
|
||||
|
||||
listeningOn := func() string {
|
||||
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)
|
||||
log.Printf("List bot (%s) starting with datapath: %q, %s\n", version, dataPath, addr)
|
||||
|
||||
var chats sync.Map
|
||||
if err := loadData(dataPath, &chats); err != nil {
|
||||
@ -147,24 +143,7 @@ func main() {
|
||||
|
||||
}
|
||||
})
|
||||
if unixSocketPath != "" {
|
||||
// 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))
|
||||
log.Panicln(anyhttp.ListenAndServe(addr, nil))
|
||||
}
|
||||
|
||||
func handleTextAdded(gl *glist.GList, text string) {
|
||||
|
Reference in New Issue
Block a user