-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
131 lines (105 loc) · 2.23 KB
/
file.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
package tg
import (
"bufio"
"errors"
"io"
//"os"
//"path/filepath"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type FileConfig = tgbotapi.FileConfig
type PhotoConfig = tgbotapi.PhotoConfig
type FileType int
const (
NoFileType FileType = iota
PhotoFileType
DocumentFileType
)
var (
UnknownFileTypeErr = errors.New("unknown file type")
)
// The type implements the structure to easily send
// files to the client.
type File struct {
*MessageCompo
name string
reader io.Reader
upload bool
typ FileType
data, caption string
}
// Create the new file with the specified reader.
// By default it NeedsUpload is set to true.
func NewFile(reader io.Reader) *File {
ret := &File{}
ret.MessageCompo = NewMessage("")
ret.reader = reader
ret.upload = true
return ret
}
func (f *File) Name(name string) *File {
f.name = name
return f
}
func (f *File) withType(typ FileType) *File {
f.typ = typ
return f
}
// Get the file type.
func (f *File) Type() FileType {
return f.typ
}
// Set the file type to PhotoFileType.
func (f *File) Photo() *File {
return f.withType(PhotoFileType)
}
func (f *File) Document() *File {
return f.withType(DocumentFileType)
}
// Set the file caption.
func (f *File) Caption(caption string) *File {
f.caption = caption
return f
}
// Specifiy whether the file needs to be uploaded to Telegram.
func (f *File) Upload(upload bool) *File {
f.upload = upload
return f
}
// Set the data to return via SendData()
func (f *File) Data(data string) *File {
f.data = data
return f
}
func (f *File) NeedsUpload() bool {
return f.upload
}
func (f *File) UploadData() (string, io.Reader, error) {
// Bufferizing the reader
// to make it faster.
bufRd := bufio.NewReader(f.reader)
fileName := f.name
return fileName, bufRd, nil
}
func (f *File) SendData() string {
return f.data
}
func (f *File) SendConfig(
sid SessionId, bot *Bot,
) (*SendConfig) {
var config SendConfig
cid := sid.ToApi()
switch f.Type() {
case PhotoFileType:
photo := tgbotapi.NewPhoto(cid, f)
photo.Caption = f.caption
config.Photo = &photo
case DocumentFileType:
doc := tgbotapi.NewDocument(sid.ToApi(), f)
doc.Caption = f.caption
config.Document = &doc
default:
panic(UnknownFileTypeErr)
}
return &config
}