This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtelegram.go
257 lines (218 loc) · 6.15 KB
/
telegram.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package telegram
import (
"context"
"errors"
"io"
"strings"
"github.com/rclone/rclone/backend"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
"github.com/rclone/rclone/fs/fshttp"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/lib/readers"
"github.com/rclone/rclone/lib/timedata"
"github.com/rclone/rclone/vfs"
"gopkg.in/telebot.v3"
)
// Register with Fs
func init() {
fs.Register(&fs.RegInfo{
Name: "telegram",
Description: "Telegram",
NewFs: NewFs,
Config: func(ctx context.Context, name string, m configmap.Mapper, config string) error {
return configstruct.Set(m, &Options)
},
Options: []fs.Option{{
Name: "token",
Help: "Telegram bot token.",
Required: true,
}, {
Name: "chat_id",
Help: "Telegram chat ID.",
Required: true,
}},
})
}
// Options defines the configuration for this backend
type Options struct {
Token string `config:"token"`
ChatID string `config:"chat_id"`
}
// Fs represents a remote Telegram chat
type Fs struct {
name string
root string
features *vfs.Features
bot *telebot.Bot
chatID int64
}
// NewFs constructs a new Fs
func NewFs(name string, m configmap.Mapper) (fs.Fs, error) {
var options Options
if err := configstruct.Decode(m, &options); err != nil {
return nil, err
}
bot, err := telebot.NewBot(telebot.Settings{Token: options.Token})
if err != nil {
return nil, err
}
me, err := bot.Me()
if err != nil {
return nil, err
}
chatID, err := me.ChatID()
if err != nil {
return nil, err
}
f := &Fs{
name: name,
root: "/",
features: vfs.NewFeatures(),
bot: bot,
chatID: chatID,
}
return f, nil
}
// Name of the remote (as passed into NewFs)
func (f *Fs) Name() string {
return f.name
}
// Root of the remote (as passed into NewFs)
func (f *Fs) Root() string {
return f.root
}
// String returns a description of the FS
func (f *Fs) String() string {
return "Telegram chat " + f.name
}
// Features returns the optional features of this Fs
func (f *Fs) Features() *vfs.Features {
return f.features
}
// Hashes returns the supported hash sets
func (f *Fs) Hashes() hash.Set {
return hash.Set(hash.None)
}
// Put uploads contents to the remote path
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
if src.Size() > int64(2<<30) {
return nil, errors.New("telegram backend only supports files up to 2GB in size")
}
fileName := src.Remote()
if strings.HasPrefix(fileName, "/") {
fileName = fileName[1:]
}
file := telebot.Document{
File: telebot.FromReader(readers.NewLimited(in, src.Size())),
Caption: fileName,
}
message, err := f.bot.Send(f.chatID, &file)
if err != nil {
return nil, err
}
return &Object{
fs: f,
path: "/" + message.Document.FileID,
name: fileName,
size: src.Size(),
modTime: message.Date,
isDir: false,
}, nil
}
// List returns a channel to the objects and subdirectories
// in dir with directory entries popped from the channel
func (f *Fs) List(ctx context.Context, dir string) (fs.DirChan, fs.EntryChan, error) {
return nil, nil, errors.New("telegram backend does not support directory listing")
}
// NewObject finds the Object at remote
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
message, err := f.bot.GetFile(remote[1:])
if err != nil {
return nil, err
}
return &Object{
fs: f,
path: remote,
name: message.FilePath,
size: int64(message.FileSize),
modTime: timedata.FromUnix(message.Date),
isDir: false,
}, nil
}
// PutStream uploads contents to the remote path using a stream
func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
return f.Put(ctx, in, src, options...)
}
// Mkdir creates the directory if it doesn't exist
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
return errors.New("telegram backend does not support directory creation")
}
// Rmdir removes the directory
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
return errors.New("telegram backend does not support directory removal")
}
// Precision of the ModTimes in this Fs
func (f *Fs) Precision() time.Duration {
return fs.ModTimeNotSupported
}
// Object represents a remote Telegram file
type Object struct {
fs *Fs
path string
name string
size int64
modTime time.Time
isDir bool
}
// Fs returns the parent Fs
func (o *Object) Fs() fs.Info {
return o.fs
}
// String returns a description of the Object
func (o *Object) String() string {
return o.path
}
// Remote returns the remote path
func (o *Object) Remote() string {
return o.path
}
// Hash returns the selected checksum of the file
// If no checksum is available it returns ""
func (o *Object) Hash(r hash.Type) (string, error) {
return "", hash.ErrUnsupported
}
// Size returns the size of the file
func (o *Object) Size() int64 {
return o.size
}
// ModTime returns the modification time of the file
func (o *Object) ModTime(ctx context.Context) time.Time {
return o.modTime
}
// SetModTime sets the modification time of the file
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
return errors.New("telegram backend does not support modification times")
}
// Storable returns whether this object can be stored
func (o *Object) Storable() bool {
return true
}
// Open opens the file for read
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
fileBytes, err := o.fs.bot.Download(&telebot.File{FileID: o.path[1:]})
if err != nil {
return nil, err
}
return fileBytes, nil
}
// Update updates the object from in with modTime
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
return errors.New("telegram backend does not support object updates")
}
// Remove deletes the remote object
func (o *Object) Remove(ctx context.Context) error {
_, err := o.fs.bot.Delete(&telebot.Message{Document: &telebot.Document{FileID: o.path[1:]}})
return err
}