update.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package tg
  2. import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  3. type Update struct {
  4. tgbotapi.Update
  5. }
  6. // The type represents general update channel.
  7. type UpdateChan struct {
  8. chn chan Update
  9. }
  10. // Return new update channel.
  11. func NewUpdateChan() *UpdateChan {
  12. ret := &UpdateChan{}
  13. ret.chn = make(chan Update)
  14. return ret
  15. }
  16. func (updates *UpdateChan) Chan() chan Update {
  17. return updates.chn
  18. }
  19. // Send an update to the channel.
  20. // Returns true if the update was sent.
  21. func (updates *UpdateChan) Send(u Update) bool {
  22. defer recover()
  23. if updates == nil || updates.chn == nil {
  24. return false
  25. }
  26. updates.chn <- u
  27. return true
  28. }
  29. // Read an update from the channel.
  30. func (updates *UpdateChan) Read() (Update, bool) {
  31. if updates == nil || updates.chn == nil {
  32. return Update{}, false
  33. }
  34. return <-updates.chn, true
  35. }
  36. // Returns true if the channel is closed.
  37. func (updates *UpdateChan) Closed() bool {
  38. return updates==nil || updates.chn == nil
  39. }
  40. // Close the channel. Used in defers.
  41. func (updates *UpdateChan) Close() {
  42. if updates == nil || updates.chn == nil {
  43. return
  44. }
  45. chn := updates.chn
  46. updates.chn = nil
  47. close(chn)
  48. }
  49. func (u Update) HasDocument() bool {
  50. return u.Message != nil &&
  51. u.Message.Document != nil
  52. }
  53. func (u Update) DocumentID() FileID {
  54. return FileID(u.Update.Message.Document.FileID)
  55. }
  56. func (u *Update) DocumentName() string {
  57. return u.Message.Document.FileName
  58. }
  59. func (u Update) DocumentSize() int {
  60. return u.Message.Document.FileSize
  61. }
  62. func (u Update) DocumentMimeType() string {
  63. return u.Message.Document.MimeType
  64. }
  65. func (u Update) HasPhotos() bool {
  66. return u.Message != nil && u.Message.Photo != nil &&
  67. len(u.Message.Photo) != 0
  68. }
  69. func (u Update) PhotoIDs() []FileID {
  70. ret := make([]FileID, len(u.Message.Photo))
  71. for i, photo := range u.Message.Photo {
  72. ret[i] = FileID(photo.FileID)
  73. }
  74. return ret
  75. }