diff --git a/bot.go b/bot.go index 72240e2..06bc8db 100644 --- a/bot.go +++ b/bot.go @@ -9,7 +9,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -type Update = tgbotapi.Update + type Chat = tgbotapi.Chat type User = tgbotapi.User @@ -189,13 +189,16 @@ func (bot *Bot) Run() error { me, _ := bot.Api.GetMe() bot.Me = &me - for u := range updates { + for up := range updates { + u := &Update{ + Update: &up, + } chn, ok := handles[u.FromChat().Type] if !ok { continue } - chn <- &u + chn <- u } return nil @@ -208,7 +211,7 @@ func (bot *Bot) handlePrivate(updates chan *Update) { for u := range updates { sid = SessionId(u.FromChat().ID) ctx, ctxOk := bot.contexts[sid] - if u.Message != nil && !ctxOk { + if u.Message != nil && !ctxOk { session, sessionOk := bot.sessions[sid] if !sessionOk { diff --git a/context.go b/context.go index 7a4343e..52b6656 100644 --- a/context.go +++ b/context.go @@ -2,8 +2,9 @@ package tg import ( "fmt" - - //tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "io" + "net/http" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" //"path" ) @@ -124,6 +125,9 @@ func (c *Context) SendfHTML(format string, v ...any) (*Message, error) { return c.Send(NewMessage(fmt.Sprintf(format, v...)).HTML()) } +func (c *Context) SendfR(format string, v ...any) (*Message, error) { + return c.Send(NewMessage(Escape2(fmt.Sprintf(format, v...))).MD2()) +} // Get the input for current widget. // Should be used inside handlers (aka "Serve"). @@ -320,3 +324,38 @@ func (c *Context) ReadString(pref string, args ...any) string { return text } +func (c *Context) GetFile(fileId FileId) (io.ReadCloser, error) { + file, err := c.Bot.Api.GetFile(tgbotapi.FileConfig{FileID:string(fileId)}) + if err != nil { + return nil, err + } + r, err := http.Get(fmt.Sprintf( + "https://api.telegram.org/file/bot%s/%s", + c.Bot.Api.Token, + file.FilePath, + )) + if err != nil { + return nil, err + } + if r.StatusCode != 200 { + return nil, StatusCodeErr + } + + return r.Body, nil +} + +func (c *Context) ReadFile(fileId FileId) ([]byte, error) { + file, err := c.GetFile(fileId) + if err != nil { + return nil, err + } + defer file.Close() + + bts, err := io.ReadAll(file) + if err != nil { + return nil, err + } + + return bts, nil +} + diff --git a/errors.go b/errors.go index 621d9fa..44a498a 100644 --- a/errors.go +++ b/errors.go @@ -18,6 +18,7 @@ var ( ActionNotDefinedErr = errors.New("action was not defined") MapCollisionErr = errors.New("map collision occured") ContextNotExistErr = errors.New("the context does not exist") + StatusCodeErr = errors.New("not success response status code") ) func (wut WrongUpdateType) Error() string { diff --git a/file.go b/file.go index 70a97b0..020d88a 100644 --- a/file.go +++ b/file.go @@ -10,6 +10,7 @@ import ( "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) +type FileConfig = tgbotapi.FileConfig type PhotoConfig = tgbotapi.PhotoConfig type FileType int diff --git a/message.go b/message.go index 5d94eaf..e0c482b 100644 --- a/message.go +++ b/message.go @@ -65,7 +65,6 @@ func (msg *MessageCompo) MD2() *MessageCompo { return msg.withParseMode(tgbotapi.ModeMarkdownV2) } - // Set the HTML parsing mode. func (msg *MessageCompo) HTML() *MessageCompo { return msg.withParseMode(tgbotapi.ModeHTML) diff --git a/update.go b/update.go index 1fc2c29..488ab0e 100644 --- a/update.go +++ b/update.go @@ -1,5 +1,14 @@ package tg +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + +type FileId string + +type Update struct { + *tgbotapi.Update + c *Context +} + // The type represents general update channel. type UpdateChan struct { chn chan *Update @@ -51,3 +60,23 @@ func (updates *UpdateChan) Close() { close(chn) } +func (u *Update) HasDocument() bool { + return u.Message != nil && u.Message.Document != nil +} + +func (u *Update) DocumentId() FileId { + return FileId(u.Update.Message.Document.FileID) +} + +func (u *Update) HasPhotos() bool { + return u.Message != nil && u.Message.Photo != nil && + len(u.Message.Photo) != 0 +} + +func (u *Update) PhotoIds() []FileId { + ret := make([]FileId, len(u.Message.Photo)) + for i, photo := range u.Message.Photo { + ret[i] = FileId(photo.FileID) + } + return ret +}