diff --git a/server/tgbot/bot.go b/server/tgbot/bot.go
index 53e9252..ae1249b 100644
--- a/server/tgbot/bot.go
+++ b/server/tgbot/bot.go
@@ -1,15 +1,42 @@
package tgbot
import (
+ "encoding/json"
"errors"
+ "github.com/dustin/go-humanize"
tele "gopkg.in/telebot.v4"
+ "gopkg.in/telebot.v4/middleware"
"net/http"
+ "os"
+ "path/filepath"
"server/log"
+ "server/settings"
"server/torr"
+ "server/web"
+ "strconv"
"strings"
"time"
)
+type Config struct {
+ WhiteIds []int64
+}
+
+var cfg *Config
+
+func init() {
+ cfg = &Config{}
+ fn := filepath.Join(settings.Path, "tg.cfg")
+ buf, err := os.ReadFile(fn)
+ if err != nil {
+ return
+ }
+ err = json.Unmarshal(buf, &cfg)
+ if err != nil {
+ log.TLogln("Error read tg config:", err)
+ }
+}
+
func Start(token string) {
pref := tele.Settings{
Token: token,
@@ -26,8 +53,11 @@ func Start(token string) {
return
}
- //Commands
+ if len(cfg.WhiteIds) > 0 {
+ b.Use(middleware.Whitelist(cfg.WhiteIds...))
+ }
+ //Commands
b.Handle("help", help)
b.Handle("Help", help)
b.Handle("/help", help)
@@ -50,13 +80,47 @@ func Start(token string) {
args := c.Args()
if len(args) > 0 {
if args[0] == "\ffiles" {
+ msg, err := c.Bot().Send(c.Sender(), "Подключение к торренту...")
t := torr.GetTorrent(args[1])
if t == nil {
- c.Send("Torrent not connected:", args[1])
- } else {
-
+ c.Edit(msg, "Torrent not connected: "+args[1])
+ return nil
}
- return nil
+ if err == nil {
+ go func() {
+ for !t.WaitInfo() {
+ time.Sleep(time.Second)
+ t = torr.GetTorrent(args[1])
+ }
+ c.Bot().Delete(msg)
+ host := settings.PubIPv4
+ if host == "" {
+ ips := web.GetLocalIps()
+ if len(ips) == 0 {
+ host = "127.0.0.1"
+ } else {
+ host = ips[0]
+ }
+ }
+
+ t = torr.GetTorrent(args[1])
+ st := t.Status()
+ txt := "Файлы:\n"
+ for _, file := range st.FileStats {
+ ff := "" + filepath.Base(file.Path) + " " + humanize.Bytes(uint64(file.Length)) + " " +
+ "Download\n\n"
+ if len(txt+ff) > 4096 {
+ c.Send(txt)
+ txt = ""
+ }
+ txt += ff
+ }
+ if len(txt) > 0 {
+ c.Send(txt)
+ }
+ }()
+ }
+ return err
}
if args[0] == "\fdelete" {
return nil
@@ -82,7 +146,7 @@ func list(c tele.Context) error {
btnFiles := tele.InlineButton{Text: "Файлы", Unique: "files", Data: t.Hash().String()}
btnDelete := tele.InlineButton{Text: "Удалить", Unique: "delete", Data: t.Hash().String()}
torrKbd := &tele.ReplyMarkup{InlineKeyboard: [][]tele.InlineButton{{btnFiles, btnDelete}}}
- c.Send(t.Title, torrKbd)
+ c.Send(t.Title+" "+humanize.Bytes(uint64(t.Size)), torrKbd)
}
return nil
}