feat: started implementing easier way to access the Telegram files.

This commit is contained in:
Andrey Parhomenko 2023-12-12 12:31:38 +03:00
parent 4232ac8d2a
commit 085d4cf77c
6 changed files with 79 additions and 7 deletions

11
bot.go
View file

@ -9,7 +9,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type Update = tgbotapi.Update
type Chat = tgbotapi.Chat type Chat = tgbotapi.Chat
type User = tgbotapi.User type User = tgbotapi.User
@ -189,13 +189,16 @@ func (bot *Bot) Run() error {
me, _ := bot.Api.GetMe() me, _ := bot.Api.GetMe()
bot.Me = &me bot.Me = &me
for u := range updates { for up := range updates {
u := &Update{
Update: &up,
}
chn, ok := handles[u.FromChat().Type] chn, ok := handles[u.FromChat().Type]
if !ok { if !ok {
continue continue
} }
chn <- &u chn <- u
} }
return nil return nil
@ -208,7 +211,7 @@ func (bot *Bot) handlePrivate(updates chan *Update) {
for u := range updates { for u := range updates {
sid = SessionId(u.FromChat().ID) sid = SessionId(u.FromChat().ID)
ctx, ctxOk := bot.contexts[sid] ctx, ctxOk := bot.contexts[sid]
if u.Message != nil && !ctxOk { if u.Message != nil && !ctxOk {
session, sessionOk := bot.sessions[sid] session, sessionOk := bot.sessions[sid]
if !sessionOk { if !sessionOk {

View file

@ -2,8 +2,9 @@ package tg
import ( import (
"fmt" "fmt"
"io"
//tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "net/http"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
//"path" //"path"
) )
@ -124,6 +125,9 @@ func (c *Context) SendfHTML(format string, v ...any) (*Message, error) {
return c.Send(NewMessage(fmt.Sprintf(format, v...)).HTML()) 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. // Get the input for current widget.
// Should be used inside handlers (aka "Serve"). // Should be used inside handlers (aka "Serve").
@ -320,3 +324,38 @@ func (c *Context) ReadString(pref string, args ...any) string {
return text 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
}

View file

@ -18,6 +18,7 @@ var (
ActionNotDefinedErr = errors.New("action was not defined") ActionNotDefinedErr = errors.New("action was not defined")
MapCollisionErr = errors.New("map collision occured") MapCollisionErr = errors.New("map collision occured")
ContextNotExistErr = errors.New("the context does not exist") ContextNotExistErr = errors.New("the context does not exist")
StatusCodeErr = errors.New("not success response status code")
) )
func (wut WrongUpdateType) Error() string { func (wut WrongUpdateType) Error() string {

View file

@ -10,6 +10,7 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type FileConfig = tgbotapi.FileConfig
type PhotoConfig = tgbotapi.PhotoConfig type PhotoConfig = tgbotapi.PhotoConfig
type FileType int type FileType int

View file

@ -65,7 +65,6 @@ func (msg *MessageCompo) MD2() *MessageCompo {
return msg.withParseMode(tgbotapi.ModeMarkdownV2) return msg.withParseMode(tgbotapi.ModeMarkdownV2)
} }
// Set the HTML parsing mode. // Set the HTML parsing mode.
func (msg *MessageCompo) HTML() *MessageCompo { func (msg *MessageCompo) HTML() *MessageCompo {
return msg.withParseMode(tgbotapi.ModeHTML) return msg.withParseMode(tgbotapi.ModeHTML)

View file

@ -1,5 +1,14 @@
package tg 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. // The type represents general update channel.
type UpdateChan struct { type UpdateChan struct {
chn chan *Update chn chan *Update
@ -51,3 +60,23 @@ func (updates *UpdateChan) Close() {
close(chn) 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
}