2023-09-25 19:52:57 +03:00
|
|
|
package tg
|
|
|
|
|
2023-12-12 12:31:38 +03:00
|
|
|
import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
|
|
|
|
|
|
|
|
type Update struct {
|
2024-03-28 10:41:09 +03:00
|
|
|
tgbotapi.Update
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2023-09-25 19:52:57 +03:00
|
|
|
// The type represents general update channel.
|
|
|
|
type UpdateChan struct {
|
2024-03-28 10:41:09 +03:00
|
|
|
chn chan Update
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return new update channel.
|
|
|
|
func NewUpdateChan() *UpdateChan {
|
|
|
|
ret := &UpdateChan{}
|
2024-03-29 14:30:48 +03:00
|
|
|
ret.chn = make(chan Update)
|
2023-09-25 19:52:57 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
func (updates *UpdateChan) Chan() chan Update {
|
2023-09-25 19:52:57 +03:00
|
|
|
return updates.chn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an update to the channel.
|
|
|
|
// Returns true if the update was sent.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (updates *UpdateChan) Send(u Update) bool {
|
2023-09-28 08:15:57 +03:00
|
|
|
defer recover()
|
2023-09-25 19:52:57 +03:00
|
|
|
if updates == nil || updates.chn == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
updates.chn <- u
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read an update from the channel.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (updates *UpdateChan) Read() (Update, bool) {
|
2023-09-25 19:52:57 +03:00
|
|
|
if updates == nil || updates.chn == nil {
|
2024-03-29 14:30:48 +03:00
|
|
|
return Update{}, false
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
return <-updates.chn, true
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the channel is closed.
|
|
|
|
func (updates *UpdateChan) Closed() bool {
|
|
|
|
return updates==nil || updates.chn == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel. Used in defers.
|
|
|
|
func (updates *UpdateChan) Close() {
|
|
|
|
if updates == nil || updates.chn == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 08:15:57 +03:00
|
|
|
chn := updates.chn
|
2023-09-25 19:52:57 +03:00
|
|
|
updates.chn = nil
|
2023-09-28 08:15:57 +03:00
|
|
|
close(chn)
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (u Update) HasDocument() bool {
|
2024-03-29 14:30:48 +03:00
|
|
|
return u.Message != nil &&
|
2023-12-25 17:35:00 +03:00
|
|
|
u.Message.Document != nil
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
func (u Update) DocumentID() FileID {
|
|
|
|
return FileID(u.Update.Message.Document.FileID)
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
func (u *Update) DocumentName() string {
|
|
|
|
return u.Message.Document.FileName
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (u Update) DocumentSize() int {
|
2024-02-22 14:31:23 +03:00
|
|
|
return u.Message.Document.FileSize
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (u Update) DocumentMimeType() string {
|
2024-02-22 14:31:23 +03:00
|
|
|
return u.Message.Document.MimeType
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (u Update) HasPhotos() bool {
|
2023-12-12 12:31:38 +03:00
|
|
|
return u.Message != nil && u.Message.Photo != nil &&
|
|
|
|
len(u.Message.Photo) != 0
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
func (u Update) PhotoIDs() []FileID {
|
|
|
|
ret := make([]FileID, len(u.Message.Photo))
|
2023-12-12 12:31:38 +03:00
|
|
|
for i, photo := range u.Message.Photo {
|
2024-07-21 16:02:47 +03:00
|
|
|
ret[i] = FileID(photo.FileID)
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2024-03-28 10:41:09 +03:00
|
|
|
|