Skip to content

Commit

Permalink
feat: Ability to process updates across multiple gorutines (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
rantanevich authored Sep 16, 2024
1 parent 6f8231d commit d82ce2e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
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

0 comments on commit d82ce2e

Please sign in to comment.