/
userbot_client.go
130 lines (106 loc) · 3.15 KB
/
userbot_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package telegram
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/cenkalti/backoff/v4"
"github.com/far4599/telegram-bot-youtube-download/internal/config"
"github.com/far4599/telegram-bot-youtube-download/internal/models"
"github.com/far4599/telegram-bot-youtube-download/internal/pkg/log"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/telegram/uploader"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
)
type UserBotClient struct {
conf *config.Config
client *telegram.Client
}
func NewUserBotClient(ctx context.Context, conf *config.Config) *UserBotClient {
c := &UserBotClient{
conf: conf,
}
go func() {
if err := c.run(ctx); err != nil {
log.Logger.Fatal(err)
}
}()
return c
}
func (c *UserBotClient) run(ctx context.Context) error {
sessionDir := c.conf.Telegram.App.SessionDir
if err := os.MkdirAll(sessionDir, 0700); err != nil {
return errors.Wrapf(err, "failed to create sessions dir '%s'", sessionDir)
}
opts := telegram.Options{
Logger: log.Logger.Desugar(),
ReconnectionBackoff: func() backoff.BackOff {
return backoff.NewExponentialBackOff()
},
NoUpdates: true,
SessionStorage: &session.FileStorage{
Path: filepath.Join(sessionDir, "session.json"),
},
}
c.client = telegram.NewClient(c.conf.Telegram.App.ID, c.conf.Telegram.App.Hash, opts)
return c.client.Run(ctx, func(ctx context.Context) error {
status, err := c.client.Auth().Status(ctx)
if err != nil {
return errors.Wrap(err, "failed to get auth status")
}
if !status.Authorized {
if _, err = c.client.Auth().Bot(ctx, c.conf.Telegram.Bot.Token); err != nil {
return errors.Wrap(err, "failed to login as userbot")
}
}
log.Logger.Info("userbot connected")
return telegram.RunUntilCanceled(ctx, c.client)
})
}
func (c *UserBotClient) UploadFile(ctx context.Context, to tg.InputPeerClass, videoOption *models.VideoOption, path string) error {
api := tg.NewClient(c.client)
u := uploader.NewUploader(api)
s := message.NewSender(api).WithUploader(u)
uploaderProgress := NewUploaderProgress()
defer uploaderProgress.Close()
target := s.To(to)
if target == nil {
return nil
}
defer func() {
recover()
_ = target.TypingAction().Cancel(ctx)
}()
go func() {
for progress := range uploaderProgress.ProgressChan() {
log.Logger.Debugw("upload progress changed", "progress", progress)
if videoOption.Audio {
_ = target.TypingAction().UploadDocument(ctx, int(progress))
} else {
_ = target.TypingAction().UploadVideo(ctx, int(progress))
}
}
}()
f, err := u.WithProgress(uploaderProgress).FromPath(ctx, path)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to upload '%s'", path))
}
var md message.MediaOption
if videoOption.Audio {
md = message.Audio(f).
Title(videoOption.VideoInfo.Title).
Performer(videoOption.VideoInfo.URL).
DurationSeconds(videoOption.VideoInfo.Duration)
} else {
md = message.Video(f, styling.Plain(videoOption.Label+" - "+videoOption.VideoInfo.Title))
}
_, err = target.Media(ctx, md)
if err != nil {
return err
}
return nil
}