Skip to content

Commit

Permalink
docs(examples): refactor updates example
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 18, 2022
1 parent 5034c1a commit d24e845
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
20 changes: 10 additions & 10 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ You can do it manually, see `bot-auth-manual` for example.

Please don't share `APP_ID` or `APP_HASH`, it can't be easily rotated.

| Name | Description | Features |
|---------------------------------------------|---------------------|----------|
| [auth](auth/main.go) | User authentication from terminal input | Custom `UserAuthenticator`
| [bot-auth-manual](bot-auth-manual/main.go) | Bot authentication | `session.Storage`, setup without environment variables
| [bot-echo](bot-echo/main.go) | Echo bot | UpdateDispatcher, message sender
| [bot-upload](bot-upload/main.go) | One-shot uploader for bot | NoUpdates flag, uploads with MIME, custom file name and as audio, resolving peer by username, HTML message
| [gif-download](gif-download/main.go) | Saved gif backup (and restore) for user | Download, upload, middlewares with rate limit, unpack
| [bg-run](bg-run/main.go) | Using client without Run | contrib/bg package
| [pretty-print](pretty-print/main.go) | Pretty-print requests, responses and updates | The tgp package, middleware and custom UpdateHandler for all updates
| [updates-gap](updates-gap-recover/main.go) | Updates engine (gap recover) example | The `updates` package that recovers missed updates
| Name | Description | Features |
|-----------------------------------------|----------------------------------------------|----------|
| [auth](auth/main.go) | User authentication from terminal input | Custom `UserAuthenticator`
| [bot-auth-manual](bot-auth-manual/main.go) | Bot authentication | `session.Storage`, setup without environment variables
| [bot-echo](bot-echo/main.go) | Echo bot | UpdateDispatcher, message sender
| [bot-upload](bot-upload/main.go) | One-shot uploader for bot | NoUpdates flag, uploads with MIME, custom file name and as audio, resolving peer by username, HTML message
| [gif-download](gif-download/main.go) | Saved gif backup (and restore) for user | Download, upload, middlewares with rate limit, unpack
| [bg-run](bg-run/main.go) | Using client without Run | contrib/bg package
| [pretty-print](pretty-print/main.go) | Pretty-print requests, responses and updates | The tgp package, middleware and custom UpdateHandler for all updates
| [updates](updates/main.go) | Updates engine example | The `updates` package that recovers missed updates

## Environment variables

Expand Down
18 changes: 13 additions & 5 deletions examples/updates-gap-recover/main.go → examples/updates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ func run(ctx context.Context) error {
log, _ := zap.NewDevelopment(zap.IncreaseLevel(zapcore.InfoLevel), zap.AddStacktrace(zapcore.FatalLevel))
defer func() { _ = log.Sync() }()

d := tg.NewUpdateDispatcher()
gaps := updates.New(updates.Config{
Handler: telegram.UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error {
log.Info("Updates", zap.Any("updates", u))
return nil
}),
Logger: log.Named("gaps"),
Handler: d,
Logger: log.Named("gaps"),
})

// Initializing client from environment.
Expand All @@ -51,6 +49,16 @@ func run(ctx context.Context) error {
return err
}

// Setup message update handlers.
d.OnNewChannelMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewChannelMessage) error {
log.Info("Channel message", zap.Any("message", update.Message))
return nil
})
d.OnNewMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewMessage) error {
log.Info("Message", zap.Any("message", update.Message))
return nil
})

return client.Run(ctx, func(ctx context.Context) error {
// Note: you need to be authenticated here.

Expand Down

0 comments on commit d24e845

Please sign in to comment.