2023-08-19 10:19:31 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2023-12-25 17:35:00 +03:00
|
|
|
//"os"
|
|
|
|
//"path/filepath"
|
2023-08-19 12:47:33 +03:00
|
|
|
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-08-19 10:19:31 +03:00
|
|
|
)
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
type FileID string
|
2023-12-12 12:31:38 +03:00
|
|
|
type FileConfig = tgbotapi.FileConfig
|
2023-09-08 17:37:32 +03:00
|
|
|
type PhotoConfig = tgbotapi.PhotoConfig
|
2023-08-19 10:19:31 +03:00
|
|
|
type FileType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoFileType FileType = iota
|
2023-12-25 17:35:00 +03:00
|
|
|
PhotoFileType
|
|
|
|
DocumentFileType
|
2023-08-19 10:19:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
UnknownFileTypeErr = errors.New("unknown file type")
|
|
|
|
)
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// The type implements the structure to easily send
|
|
|
|
// files to the client.
|
2023-08-19 10:19:31 +03:00
|
|
|
type File struct {
|
2024-03-28 10:41:09 +03:00
|
|
|
MessageCompo
|
2023-12-25 17:35:00 +03:00
|
|
|
name string
|
|
|
|
reader io.Reader
|
|
|
|
upload bool
|
2023-08-19 10:19:31 +03:00
|
|
|
typ FileType
|
2023-12-25 17:35:00 +03:00
|
|
|
data, caption string
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Create the new file with the specified reader.
|
|
|
|
// By default it NeedsUpload is set to true.
|
|
|
|
func NewFile(reader io.Reader) *File {
|
2023-09-29 13:36:37 +03:00
|
|
|
ret := &File{}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
ret.MessageCompo = *Messagef("")
|
2023-12-25 17:35:00 +03:00
|
|
|
ret.reader = reader
|
|
|
|
ret.upload = true
|
2023-09-29 13:36:37 +03:00
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
return ret
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
func (f *File) Name(name string) *File {
|
|
|
|
f.name = name
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2023-08-19 10:19:31 +03:00
|
|
|
func (f *File) withType(typ FileType) *File {
|
|
|
|
f.typ = typ
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Get the file type.
|
2023-08-19 10:19:31 +03:00
|
|
|
func (f *File) Type() FileType {
|
|
|
|
return f.typ
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Set the file type to PhotoFileType.
|
|
|
|
func (f *File) Photo() *File {
|
|
|
|
return f.withType(PhotoFileType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Document() *File {
|
|
|
|
return f.withType(DocumentFileType)
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Set the file caption.
|
2023-08-19 10:19:31 +03:00
|
|
|
func (f *File) Caption(caption string) *File {
|
|
|
|
f.caption = caption
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Specifiy whether the file needs to be uploaded to Telegram.
|
|
|
|
func (f *File) Upload(upload bool) *File {
|
|
|
|
f.upload = upload
|
|
|
|
return f
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Set the data to return via SendData()
|
|
|
|
func (f *File) Data(data string) *File {
|
|
|
|
f.data = data
|
|
|
|
return f
|
|
|
|
}
|
2023-08-19 10:19:31 +03:00
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
func (f *File) NeedsUpload() bool {
|
|
|
|
return f.upload
|
|
|
|
}
|
2023-08-19 10:19:31 +03:00
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
func (f *File) UploadData() (string, io.Reader, error) {
|
|
|
|
// Bufferizing the reader
|
|
|
|
// to make it faster.
|
|
|
|
bufRd := bufio.NewReader(f.reader)
|
|
|
|
fileName := f.name
|
2023-08-19 10:19:31 +03:00
|
|
|
|
|
|
|
return fileName, bufRd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) SendData() string {
|
2023-12-25 17:35:00 +03:00
|
|
|
return f.data
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
2023-12-25 17:35:00 +03:00
|
|
|
|
2023-09-08 17:37:32 +03:00
|
|
|
func (f *File) SendConfig(
|
2024-07-21 16:02:47 +03:00
|
|
|
sid SessionID, bot *Bot,
|
2024-03-28 10:41:09 +03:00
|
|
|
) (SendConfig) {
|
2023-09-08 17:37:32 +03:00
|
|
|
var config SendConfig
|
2024-07-21 16:02:47 +03:00
|
|
|
cid := sid.ToAPI()
|
2023-08-19 12:47:33 +03:00
|
|
|
|
|
|
|
switch f.Type() {
|
2023-12-25 17:35:00 +03:00
|
|
|
case PhotoFileType:
|
2023-10-11 13:41:08 +03:00
|
|
|
photo := tgbotapi.NewPhoto(cid, f)
|
2023-08-19 12:47:33 +03:00
|
|
|
photo.Caption = f.caption
|
2023-09-08 17:37:32 +03:00
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
config.Chattable = photo
|
2023-12-25 17:35:00 +03:00
|
|
|
case DocumentFileType:
|
2024-07-21 16:02:47 +03:00
|
|
|
doc := tgbotapi.NewDocument(sid.ToAPI(), f)
|
2023-12-25 17:35:00 +03:00
|
|
|
doc.Caption = f.caption
|
2024-03-29 14:30:48 +03:00
|
|
|
config.Chattable = doc
|
2023-08-19 12:47:33 +03:00
|
|
|
default:
|
2023-09-16 14:34:17 +03:00
|
|
|
panic(UnknownFileTypeErr)
|
2023-08-19 12:47:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
return config
|
2023-08-19 12:47:33 +03:00
|
|
|
}
|