-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
FILE_REFERENCE_EXPIRED
in ctx.DownloadMedia (#62)
* feat: add thumbSize for photo media download * docs: fix readme sqlite session * feat: add GetMediaFileNameWithId and GetMediaFileName for getting media's filenames * feat: add Downloader example * fix: fix nil pointer diref panic * feat: add reply for downloaded file * fix: set correct thumbSizes * ref: rollback go mod changes * ref: run go mod tidy * fix: add fillUserIdFromMessage for UpdateNewChannelMessage * ref: remove image * ref: move helpers to functions/mediaHelpers * ref: get creds from env * ref: remove unnecessary changes * fix: fix nil pointer deref in EffectiveUser * ref: remove redundant fix --------- Co-authored-by: pibragimov <pibragimov@whoosh.bike>
- Loading branch information
Showing
9 changed files
with
261 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/celestix/gotgproto" | ||
"github.com/celestix/gotgproto/dispatcher/handlers" | ||
"github.com/celestix/gotgproto/dispatcher/handlers/filters" | ||
"github.com/celestix/gotgproto/ext" | ||
"github.com/celestix/gotgproto/functions" | ||
"github.com/celestix/gotgproto/sessionMaker" | ||
"github.com/go-faster/errors" | ||
"log" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
// Type of client to login to, can be of 2 types: | ||
// 1.) Bot (Fill BotToken in this case) | ||
// 2.) User (Fill Phone in this case) | ||
clientType := gotgproto.ClientType{ | ||
BotToken: os.Getenv("TG_BOT_TOKEN"), | ||
} | ||
|
||
appIdEnv := os.Getenv("TG_APP_ID") | ||
appId, err := strconv.Atoi(appIdEnv) | ||
if err != nil { | ||
log.Fatalln("failed to convert app id to int:", err) | ||
} | ||
|
||
client, err := gotgproto.NewClient( | ||
// Get AppID from https://my.telegram.org/apps | ||
appId, | ||
// Get ApiHash from https://my.telegram.org/apps | ||
os.Getenv("TG_API_HASH"), | ||
// ClientType, as we defined above | ||
clientType, | ||
// Optional parameters of client | ||
&gotgproto.ClientOpts{ | ||
InMemory: true, | ||
Session: sessionMaker.SimpleSession(), | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatalln("failed to start client:", err) | ||
} | ||
|
||
clientDispatcher := client.Dispatcher | ||
|
||
// This Message Handler will download any media passed to bot | ||
clientDispatcher.AddHandlerToGroup(handlers.NewMessage(filters.Message.Media, download), 1) | ||
|
||
fmt.Printf("client (@%s) has been started...\n", client.Self.Username) | ||
|
||
err = client.Idle() | ||
if err != nil { | ||
log.Fatalln("failed to start client:", err) | ||
} | ||
} | ||
|
||
func download(ctx *ext.Context, update *ext.Update) error { | ||
filename, err := functions.GetMediaFileNameWithId(update.EffectiveMessage.Media) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get media file name") | ||
} | ||
|
||
_, err = ctx.DownloadMedia( | ||
update.EffectiveMessage.Media, | ||
ext.DownloadOutputPath(filename), | ||
nil, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to download media") | ||
} | ||
|
||
msg := fmt.Sprintf(`File "%s" downloaded`, filename) | ||
_, err = ctx.Reply(update, msg, nil) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to reply") | ||
} | ||
|
||
fmt.Println(msg) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.