2023-08-19 10:19:31 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
ImageFileType
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
UnknownFileTypeErr = errors.New("unknown file type")
|
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
2023-09-29 13:36:37 +03:00
|
|
|
*MessageCompo
|
2023-08-19 10:19:31 +03:00
|
|
|
path string
|
|
|
|
typ FileType
|
|
|
|
caption string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFile(path string) *File {
|
2023-09-29 13:36:37 +03:00
|
|
|
ret := &File{}
|
|
|
|
|
|
|
|
ret.MessageCompo = NewMessage("")
|
|
|
|
ret.path = path
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
return ret
|
2023-08-19 10:19:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) withType(typ FileType) *File {
|
|
|
|
f.typ = typ
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Type() FileType {
|
|
|
|
return f.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Image() *File {
|
|
|
|
return f.withType(ImageFileType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Caption(caption string) *File {
|
|
|
|
f.caption = caption
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) NeedsUpload() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) UploadData() (string, io.Reader, error) {
|
|
|
|
rd, err := os.Open(f.path)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bufRd := bufio.NewReader(rd)
|
|
|
|
|
|
|
|
fileName := filepath.Base(f.path)
|
|
|
|
|
|
|
|
return fileName, bufRd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) SendData() string {
|
|
|
|
return ""
|
|
|
|
}
|
2023-09-08 17:37:32 +03:00
|
|
|
func (f *File) SendConfig(
|
2023-10-11 13:41:08 +03:00
|
|
|
sid SessionId, bot *Bot,
|
2023-09-16 14:34:17 +03:00
|
|
|
) (*SendConfig) {
|
2023-09-08 17:37:32 +03:00
|
|
|
var config SendConfig
|
2023-10-11 13:41:08 +03:00
|
|
|
cid := sid.ToApi()
|
2023-08-19 12:47:33 +03:00
|
|
|
|
|
|
|
switch f.Type() {
|
|
|
|
case ImageFileType:
|
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
|
|
|
|
|
|
|
config.Image = &photo
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
return &config
|
2023-08-19 12:47:33 +03:00
|
|
|
}
|