Skip to content

Commit

Permalink
Stop getUpdates on conflict. Add Raw method.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Aug 12, 2024
1 parent d10c6d9 commit 673c016
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"

Check failure on line 10 in bot.go

View workflow job for this annotation

GitHub Actions / test (1.19.x)

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/log/slog)

Check failure on line 10 in bot.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/log/slog)
Expand Down Expand Up @@ -55,10 +56,6 @@ func NewBot(token string, opts ...Option) (*Bot, error) {
}
bot.ctx, bot.cancelFunc = context.WithCancel(options.ctx)

if bot.apiServer == "" {
bot.apiServer = defaultAPIServer
}

me, err := bot.GetMe()
if err != nil {
return nil, err
Expand All @@ -78,7 +75,7 @@ func (bot *Bot) buildURL(method string) string {
func (bot *Bot) decodeResponse(data []byte, target interface{}) error {
response := new(Response)
if err := json.Unmarshal(data, response); err != nil {
return fmt.Errorf("Decode response error (%s)", err.Error())
return fmt.Errorf("decode response error: %w", err)
}

if !response.Ok {
Expand All @@ -91,7 +88,7 @@ func (bot *Bot) decodeResponse(data []byte, target interface{}) error {
}

if err := json.Unmarshal(response.Result, target); err != nil {
return fmt.Errorf("Decode result error (%s)", err.Error())
return fmt.Errorf("decode result error: %w", err)
}

return nil
Expand Down Expand Up @@ -180,6 +177,10 @@ func (bot *Bot) Start(allowedUpdates ...string) {
updates, err := bot.getUpdates(bot.offset+1, allowedUpdates...)
if err != nil {
bot.logger.ErrorContext(bot.ctx, "Get updates error", "error", err)
httpErr := HTTPError{}
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusConflict {
bot.cancelFunc()
}
}

for _, update := range updates {
Expand Down Expand Up @@ -272,6 +273,21 @@ func (bot *Bot) GetMe() (*User, error) {
return me, err
}

// Raw - send any method and return raw response
func (bot *Bot) Raw(method string, data any) ([]byte, error) {
request, err := newPostRequest(bot.ctx, bot.buildURL(method), data)
if err != nil {
return nil, err
}

response, err := bot.httpClient.Do(request)
if err != nil {
return nil, err
}

return handleResponse(response)
}

// Use this method to send text messages.
func (bot *Bot) SendMessage(chatID ChatID, text string, options *SendMessageOptions) (*Message, error) {
params := sendMessageParams{
Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newPostRequest(ctx context.Context, url string, data interface{}) (*http.Re
body := new(bytes.Buffer)
if data != nil {
if err := json.NewEncoder(body).Encode(data); err != nil {
return nil, fmt.Errorf("Encode data error (%s)", err.Error())
return nil, fmt.Errorf("encode data error: %w", err)
}
}

Expand Down

0 comments on commit 673c016

Please sign in to comment.