file.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package tg
  2. import (
  3. "bufio"
  4. "errors"
  5. "io"
  6. //"os"
  7. //"path/filepath"
  8. "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  9. )
  10. type FileConfig = tgbotapi.FileConfig
  11. type PhotoConfig = tgbotapi.PhotoConfig
  12. type FileType int
  13. const (
  14. NoFileType FileType = iota
  15. PhotoFileType
  16. DocumentFileType
  17. )
  18. var (
  19. UnknownFileTypeErr = errors.New("unknown file type")
  20. )
  21. // The type implements the structure to easily send
  22. // files to the client.
  23. type File struct {
  24. MessageCompo
  25. name string
  26. reader io.Reader
  27. upload bool
  28. typ FileType
  29. data, caption string
  30. }
  31. // Create the new file with the specified reader.
  32. // By default it NeedsUpload is set to true.
  33. func NewFile(reader io.Reader) *File {
  34. ret := &File{}
  35. ret.MessageCompo = NewMessage("")
  36. ret.reader = reader
  37. ret.upload = true
  38. return ret
  39. }
  40. func (f *File) Name(name string) *File {
  41. f.name = name
  42. return f
  43. }
  44. func (f *File) withType(typ FileType) *File {
  45. f.typ = typ
  46. return f
  47. }
  48. // Get the file type.
  49. func (f *File) Type() FileType {
  50. return f.typ
  51. }
  52. // Set the file type to PhotoFileType.
  53. func (f *File) Photo() *File {
  54. return f.withType(PhotoFileType)
  55. }
  56. func (f *File) Document() *File {
  57. return f.withType(DocumentFileType)
  58. }
  59. // Set the file caption.
  60. func (f *File) Caption(caption string) *File {
  61. f.caption = caption
  62. return f
  63. }
  64. // Specifiy whether the file needs to be uploaded to Telegram.
  65. func (f *File) Upload(upload bool) *File {
  66. f.upload = upload
  67. return f
  68. }
  69. // Set the data to return via SendData()
  70. func (f *File) Data(data string) *File {
  71. f.data = data
  72. return f
  73. }
  74. func (f *File) NeedsUpload() bool {
  75. return f.upload
  76. }
  77. func (f *File) UploadData() (string, io.Reader, error) {
  78. // Bufferizing the reader
  79. // to make it faster.
  80. bufRd := bufio.NewReader(f.reader)
  81. fileName := f.name
  82. return fileName, bufRd, nil
  83. }
  84. func (f *File) SendData() string {
  85. return f.data
  86. }
  87. func (f *File) SendConfig(
  88. sid SessionId, bot *Bot,
  89. ) (SendConfig) {
  90. var config SendConfig
  91. cid := sid.ToApi()
  92. switch f.Type() {
  93. case PhotoFileType:
  94. photo := tgbotapi.NewPhoto(cid, f)
  95. photo.Caption = f.caption
  96. config.Photo = &photo
  97. case DocumentFileType:
  98. doc := tgbotapi.NewDocument(sid.ToApi(), f)
  99. doc.Caption = f.caption
  100. config.Document = &doc
  101. default:
  102. panic(UnknownFileTypeErr)
  103. }
  104. return config
  105. }