code cleanup
This commit is contained in:
parent
917be1687f
commit
5336968e05
34 changed files with 193 additions and 115 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/PaulSonOfLars/gotgbot/v2"
|
||||
"github.com/PaulSonOfLars/gotgbot/v2/ext"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func HandleDefaultFormatDownload(
|
||||
|
@ -59,7 +60,7 @@ func HandleDefaultFormatDownload(
|
|||
}
|
||||
|
||||
if len(medias) == 0 {
|
||||
return fmt.Errorf("no formats downloaded")
|
||||
return errors.New("no formats downloaded")
|
||||
}
|
||||
|
||||
isCaptionEnabled := true
|
||||
|
|
|
@ -11,6 +11,8 @@ import (
|
|||
"govd/enums"
|
||||
"govd/models"
|
||||
"govd/util"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func downloadMediaItem(
|
||||
|
@ -25,7 +27,7 @@ func downloadMediaItem(
|
|||
|
||||
format := media.Format
|
||||
if format == nil {
|
||||
return nil, fmt.Errorf("media format is nil")
|
||||
return nil, errors.New("media format is nil")
|
||||
}
|
||||
|
||||
fileName := format.GetFileName()
|
||||
|
@ -79,7 +81,7 @@ func downloadMediaItem(
|
|||
}
|
||||
|
||||
if format.Type == enums.MediaTypeVideo || format.Type == enums.MediaTypeAudio {
|
||||
path, err := getFileThumbnail(format, filePath)
|
||||
path, err := getFileThumbnail(ctx, format, filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get thumbnail: %w", err)
|
||||
}
|
||||
|
|
|
@ -100,8 +100,7 @@ func HandleInline(
|
|||
return util.ErrInlineMediaGroup
|
||||
}
|
||||
err = HandleInlineCached(
|
||||
bot, ctx,
|
||||
dlCtx, cached[0],
|
||||
bot, ctx, cached[0],
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -118,7 +117,6 @@ func HandleInline(
|
|||
func HandleInlineCached(
|
||||
bot *gotgbot.Bot,
|
||||
ctx *ext.Context,
|
||||
dlCtx *models.DownloadContext,
|
||||
media *models.Media,
|
||||
) error {
|
||||
var result gotgbot.InlineQueryResult
|
||||
|
@ -177,7 +175,6 @@ func HandleInlineCached(
|
|||
func HandleInlineCachedResult(
|
||||
bot *gotgbot.Bot,
|
||||
ctx *ext.Context,
|
||||
dlCtx *models.DownloadContext,
|
||||
media *models.Media,
|
||||
) error {
|
||||
format := media.Format
|
||||
|
|
|
@ -25,7 +25,7 @@ func HandleDownloadRequest(
|
|||
) error {
|
||||
chatID := ctx.EffectiveMessage.Chat.Id
|
||||
if dlCtx.Extractor.Type == enums.ExtractorTypeSingle {
|
||||
TypingEffect(bot, ctx, chatID)
|
||||
TypingEffect(bot, chatID)
|
||||
err := HandleDefaultFormatDownload(bot, ctx, taskCtx, dlCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -115,7 +115,7 @@ func SendMedias(
|
|||
inputMediaList = append(inputMediaList, inputMedia)
|
||||
}
|
||||
mediaType := chunk[0].Media.Format.Type
|
||||
SendingEffect(bot, ctx, chatID, mediaType)
|
||||
SendingEffect(bot, chatID, mediaType)
|
||||
msgs, err := bot.SendMediaGroup(
|
||||
chatID,
|
||||
inputMediaList,
|
||||
|
|
|
@ -6,8 +6,6 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"govd/database"
|
||||
"govd/enums"
|
||||
"govd/models"
|
||||
|
@ -17,9 +15,11 @@ import (
|
|||
|
||||
"github.com/PaulSonOfLars/gotgbot/v2"
|
||||
"github.com/PaulSonOfLars/gotgbot/v2/ext"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func getFileThumbnail(
|
||||
ctx context.Context,
|
||||
format *models.MediaFormat,
|
||||
filePath string,
|
||||
) (string, error) {
|
||||
|
@ -29,9 +29,6 @@ func getFileThumbnail(
|
|||
fileBaseName := fileName[:len(fileName)-len(fileExt)]
|
||||
thumbnailFilePath := filepath.Join(fileDir, fileBaseName+".thumb.jpeg")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
if len(format.Thumbnail) > 0 {
|
||||
file, err := util.DownloadFileInMemory(ctx, format.Thumbnail, nil)
|
||||
if err != nil {
|
||||
|
@ -109,7 +106,7 @@ func StoreMedias(
|
|||
var storedMedias []*models.Media
|
||||
|
||||
if len(medias) == 0 {
|
||||
return fmt.Errorf("no media to store")
|
||||
return errors.New("no media to store")
|
||||
}
|
||||
|
||||
for idx, msg := range msgs {
|
||||
|
@ -161,7 +158,6 @@ func FormatCaption(
|
|||
|
||||
func TypingEffect(
|
||||
bot *gotgbot.Bot,
|
||||
ctx *ext.Context,
|
||||
chatID int64,
|
||||
) {
|
||||
bot.SendChatAction(
|
||||
|
@ -173,7 +169,6 @@ func TypingEffect(
|
|||
|
||||
func SendingEffect(
|
||||
bot *gotgbot.Bot,
|
||||
ctx *ext.Context,
|
||||
chatID int64,
|
||||
mediaType enums.MediaType,
|
||||
) {
|
||||
|
|
|
@ -71,8 +71,7 @@ func InlineDownloadResultHandler(
|
|||
select {
|
||||
case media := <-mediaChan:
|
||||
err := core.HandleInlineCachedResult(
|
||||
bot, ctx,
|
||||
dlCtx, media,
|
||||
bot, ctx, media,
|
||||
)
|
||||
if err != nil {
|
||||
core.HandleErrorMessage(bot, ctx, err)
|
||||
|
|
|
@ -46,13 +46,13 @@ func Start() {
|
|||
logDispatcherErrors = false
|
||||
}
|
||||
dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{
|
||||
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
|
||||
Error: func(_ *gotgbot.Bot, _ *ext.Context, err error) ext.DispatcherAction {
|
||||
if logDispatcherErrors {
|
||||
log.Printf("an error occurred while handling update: %v", err)
|
||||
}
|
||||
return ext.DispatcherActionNoop
|
||||
},
|
||||
Panic: func(b *gotgbot.Bot, ctx *ext.Context, r interface{}) {
|
||||
Panic: func(_ *gotgbot.Bot, _ *ext.Context, r any) {
|
||||
if logDispatcherErrors {
|
||||
log.Printf("panic occurred while handling update: %v\n", r)
|
||||
log.Printf("stack trace:\n%s\n", debug.Stack())
|
||||
|
|
|
@ -13,11 +13,11 @@ import (
|
|||
"github.com/PaulSonOfLars/gotgbot/v2"
|
||||
)
|
||||
|
||||
type BotClient struct {
|
||||
type Client struct {
|
||||
gotgbot.BotClient
|
||||
}
|
||||
|
||||
func (b BotClient) RequestWithContext(
|
||||
func (b Client) RequestWithContext(
|
||||
ctx context.Context,
|
||||
token string,
|
||||
method string,
|
||||
|
@ -38,18 +38,18 @@ func (b BotClient) RequestWithContext(
|
|||
return val, err
|
||||
}
|
||||
|
||||
func NewBotClient() BotClient {
|
||||
func NewBotClient() Client {
|
||||
botAPIURL := os.Getenv("BOT_API_URL")
|
||||
if botAPIURL == "" {
|
||||
log.Println("BOT_API_URL is not provided, using default")
|
||||
botAPIURL = gotgbot.DefaultAPIURL
|
||||
}
|
||||
return BotClient{
|
||||
return Client{
|
||||
BotClient: &gotgbot.BaseBotClient{
|
||||
Client: http.Client{
|
||||
Transport: &http.Transport{
|
||||
// avoid using proxy for telegram
|
||||
Proxy: func(r *http.Request) (*url.URL, error) {
|
||||
Proxy: func(_ *http.Request) (*url.URL, error) {
|
||||
return nil, nil
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue