feat: started implementing easier way to access the Telegram files.
This commit is contained in:
parent
4232ac8d2a
commit
085d4cf77c
6 changed files with 79 additions and 7 deletions
9
bot.go
9
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
|
||||
|
|
43
context.go
43
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
1
file.go
1
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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
29
update.go
29
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue