Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to process updates across multiple goroutines #112

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

// call methods.SetWebhook if needed

go b.StartWebhook(ctx)

http.ListenAndServe(":2000", b.WebhookHandler())
Expand Down Expand Up @@ -183,6 +183,7 @@ b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
- `WithAllowedUpdates(params AllowedUpdates)` - set [allowed_updates](https://core.telegram.org/bots/api#getupdates) for getUpdates method
- `WithUpdatesChannelCap(cap int)` - set updates channel capacity, by default 1024
- `WithWebhookSecretToken(webhookSecretToken string)` - set X-Telegram-Bot-Api-Secret-Token header sent from telegram servers to confirm validity of update
- `WithWorkers` - set the number of workers that are processing the Updates channel, by default 1
- `UseTestEnvironment()` - use test environment

## Message.Text and CallbackQuery.Data handlers
Expand Down Expand Up @@ -215,8 +216,8 @@ b.UnregisterHandler(handlerID)
```

Match Types:
- `MatchTypeExact`
- `MatchTypePrefix`
- `MatchTypeExact`
- `MatchTypePrefix`
- `MatchTypeContains`

You can use `RegisterHandlerRegexp` to match by regular expression.
Expand Down Expand Up @@ -245,7 +246,7 @@ For some methods, like `SendPhoto`, `SendAudio` etc, you can send file by file p
To send a file by URL or FileID, you can use `&models.InputFileString{Data: string}`:

```go
// file id of uploaded image
// file id of uploaded image
inputFileData := "AgACAgIAAxkDAAIBOWJimnCJHQJiJ4P3aasQCPNyo6mlAALDuzEbcD0YSxzjB-vmkZ6BAQADAgADbQADJAQ"
// or URL image path
// inputFileData := "https://example.com/image.png"
Expand Down Expand Up @@ -293,7 +294,7 @@ media1 := &models.InputMediaPhoto{
}

media2 := &models.InputMediaPhoto{
Media: "attach://image.png",
Media: "attach://image.png",
Caption: "2",
MediaAttachment: bytes.NewReader(fileContent),
}
Expand All @@ -315,11 +316,11 @@ bot.SendMediaGroup(ctx, params)

### `EscapeMarkdown(s string) string`

Escape special symbols for Telegram MarkdownV2 syntax
Escape special symbols for Telegram MarkdownV2 syntax

### `EscapeMarkdownUnescaped(s string) string`

Escape only unescaped special symbols for Telegram MarkdownV2 syntax
Escape only unescaped special symbols for Telegram MarkdownV2 syntax

### `RandomString(n int) string`

Expand Down
17 changes: 13 additions & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
defaultPollTimeout = time.Minute
defaultUpdatesChanCap = 1024
defaultCheckInitTimeout = time.Second * 5
defaultWorkers = 1
)

type HttpClient interface {
Expand All @@ -36,6 +37,7 @@ type Bot struct {
skipGetMe bool
webhookSecretToken string
testEnvironment bool
workers int

defaultHandlerFunc HandlerFunc

Expand Down Expand Up @@ -79,6 +81,7 @@ func New(token string, options ...Option) (*Bot, error) {
errorsHandler: defaultErrorsHandler,
debugHandler: defaultDebugHandler,
checkInitTimeout: defaultCheckInitTimeout,
workers: defaultWorkers,

updates: make(chan *models.Update, defaultUpdatesChanCap),
}
Expand All @@ -104,8 +107,10 @@ func New(token string, options ...Option) (*Bot, error) {
func (b *Bot) StartWebhook(ctx context.Context) {
wg := &sync.WaitGroup{}

wg.Add(1)
go b.waitUpdates(ctx, wg)
wg.Add(b.workers)
for i := 0; i < b.workers; i++ {
go b.waitUpdates(ctx, wg)
}

wg.Wait()
}
Expand All @@ -114,10 +119,14 @@ func (b *Bot) StartWebhook(ctx context.Context) {
func (b *Bot) Start(ctx context.Context) {
wg := &sync.WaitGroup{}

wg.Add(2)
go b.waitUpdates(ctx, wg)
wg.Add(1)
go b.getUpdates(ctx, wg)

wg.Add(b.workers)
for i := 0; i < b.workers; i++ {
go b.waitUpdates(ctx, wg)
}

wg.Wait()
}

Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ func WithWebhookSecretToken(webhookSecretToken string) Option {
}
}

// WithWorkers allows setting the number of workers that are processing the Updates channel
func WithWorkers(workers int) Option {
return func(b *Bot) {
b.workers = workers
}
}

// UseTestEnvironment allows to use test environment
func UseTestEnvironment() Option {
return func(b *Bot) {
Expand Down
Loading