Skip to content

Commit

Permalink
Add support for Telegram test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
caalberts authored and demget committed Aug 9, 2024
1 parent c901979 commit 50ec802
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
14 changes: 12 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// It also handles API errors, so you only need to unwrap
// result field from json data.
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
url := b.URL + "/bot" + b.Token + "/" + method
url := b.url(method)

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
Expand Down Expand Up @@ -71,6 +71,16 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
return data, extractOk(data)
}

func (b *Bot) url(method string) string {
url := b.URL + "/bot" + b.Token

if b.useTestEnv {
url += "/test"
}

return url + "/" + method
}

func (b *Bot) sendFiles(method string, files map[string]File, params map[string]string) ([]byte, error) {
rawFiles := make(map[string]interface{})
for name, f := range files {
Expand Down Expand Up @@ -116,7 +126,7 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]
}
}()

url := b.URL + "/bot" + b.Token + "/" + method
url := b.url(method)

resp, err := b.client.Post(url, writer.FormDataContentType(), pipeReader)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telebot
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -118,3 +119,35 @@ func TestExtractMessage(t *testing.T) {
_, err = extractMessage(data)
require.NoError(t, err)
}

func TestBot_url(t *testing.T) {
url := "https://api.telegram.com"
token := "my-token"
method := "my-method"

tests := []struct {
name string
useTestEnv bool
expectedURL string
}{
{"when bot is not set to test environment", false, fmt.Sprintf("%s/bot%s/%s", url, token, method)},
{"when bot is set to test environment", true, fmt.Sprintf("%s/bot%s/test/%s", url, token, method)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
settings := Settings{
URL: url,
Token: token,
Offline: true,
UseTestEnv: tt.useTestEnv,
}
bot, err := NewBot(settings)

assert.NoError(t, err)

url := bot.url(method)

assert.Equal(t, tt.expectedURL, url)
})
}
}
6 changes: 6 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewBot(pref Settings) (*Bot, error) {
synchronous: pref.Synchronous,
verbose: pref.Verbose,
parseMode: pref.ParseMode,
useTestEnv: pref.UseTestEnv,
client: client,
}

Expand Down Expand Up @@ -80,6 +81,7 @@ type Bot struct {
synchronous bool
verbose bool
parseMode ParseMode
useTestEnv bool
stop chan chan struct{}
client *http.Client

Expand Down Expand Up @@ -122,6 +124,10 @@ type Settings struct {

// Offline allows to create a bot without network for testing purposes.
Offline bool

// UseTestEnv is used to create a bot on Telegram test environment
// https://core.telegram.org/bots/webapps#using-bots-in-the-test-environment
UseTestEnv bool
}

var defaultOnError = func(err error, c Context) {
Expand Down
2 changes: 2 additions & 0 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestNewBot(t *testing.T) {
pref.Poller = &LongPoller{Timeout: time.Second}
pref.Updates = 50
pref.ParseMode = ModeHTML
pref.UseTestEnv = true
pref.Offline = true

b, err = NewBot(pref)
Expand All @@ -76,6 +77,7 @@ func TestNewBot(t *testing.T) {
assert.Equal(t, pref.Poller, b.Poller)
assert.Equal(t, 50, cap(b.Updates))
assert.Equal(t, ModeHTML, b.parseMode)
assert.Equal(t, pref.UseTestEnv, b.useTestEnv)
}

func TestBotHandle(t *testing.T) {
Expand Down

0 comments on commit 50ec802

Please sign in to comment.