This commit is contained in:
YouROK
2025-03-08 00:17:05 +03:00
parent 174e18149a
commit a2700552b2

View File

@@ -1,15 +1,42 @@
package tgbot package tgbot
import ( import (
"encoding/json"
"errors" "errors"
"github.com/dustin/go-humanize"
tele "gopkg.in/telebot.v4" tele "gopkg.in/telebot.v4"
"gopkg.in/telebot.v4/middleware"
"net/http" "net/http"
"os"
"path/filepath"
"server/log" "server/log"
"server/settings"
"server/torr" "server/torr"
"server/web"
"strconv"
"strings" "strings"
"time" "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) { func Start(token string) {
pref := tele.Settings{ pref := tele.Settings{
Token: token, Token: token,
@@ -26,8 +53,11 @@ func Start(token string) {
return 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) b.Handle("Help", help)
b.Handle("/help", help) b.Handle("/help", help)
@@ -50,13 +80,47 @@ func Start(token string) {
args := c.Args() args := c.Args()
if len(args) > 0 { if len(args) > 0 {
if args[0] == "\ffiles" { if args[0] == "\ffiles" {
msg, err := c.Bot().Send(c.Sender(), "Подключение к торренту...")
t := torr.GetTorrent(args[1]) t := torr.GetTorrent(args[1])
if t == nil { if t == nil {
c.Send("Torrent not connected:", args[1]) c.Edit(msg, "Torrent not connected: "+args[1])
} else { 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 := "<b>" + filepath.Base(file.Path) + "</b> <i>" + humanize.Bytes(uint64(file.Length)) + "</i> " +
"<a href=\"http://" + host + ":" + settings.Port + "/stream/" + filepath.Base(file.Path) + "?link=" + t.Hash().HexString() + "&index=" + strconv.Itoa(file.Id) + "&play\">Download</a>\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" { if args[0] == "\fdelete" {
return nil return nil
@@ -82,7 +146,7 @@ func list(c tele.Context) error {
btnFiles := tele.InlineButton{Text: "Файлы", Unique: "files", Data: t.Hash().String()} btnFiles := tele.InlineButton{Text: "Файлы", Unique: "files", Data: t.Hash().String()}
btnDelete := tele.InlineButton{Text: "Удалить", Unique: "delete", Data: t.Hash().String()} btnDelete := tele.InlineButton{Text: "Удалить", Unique: "delete", Data: t.Hash().String()}
torrKbd := &tele.ReplyMarkup{InlineKeyboard: [][]tele.InlineButton{{btnFiles, btnDelete}}} 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 return nil
} }