Skip to content

Commit 8e5f4d6

Browse files
authored
Merge pull request #63 from sevkin/#62
Add WithBaseURL server option.
2 parents db9d1a8 + eaef12b commit 8e5f4d6

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
// Client is a low-level Telegram client
1313
type Client struct {
1414
token string
15+
baseURL string
1516
url string
1617
httpClient *http.Client
1718
nextOffset int
@@ -26,6 +27,7 @@ func NewClient(token string, httpClient *http.Client, baseURL string) *Client {
2627
return &Client{
2728
token: token,
2829
httpClient: httpClient,
30+
baseURL: baseURL,
2931
url: fmt.Sprintf("%s/bot%s/", baseURL, token) + "%s",
3032
}
3133
}

client_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import "fmt"
44

55
// FileURL returns file URL ready for download
66
func (c *Client) FileURL(file *File) string {
7-
return fmt.Sprintf("%s/file/bot%s/%s", apiBaseURL, c.token, file.FilePath)
7+
return fmt.Sprintf("%s/file/bot%s/%s", c.baseURL, c.token, file.FilePath)
88
}

examples/baseurl/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"time"
7+
8+
"github.com/yanzay/tbot/v2"
9+
)
10+
11+
/* Before setup nginx as proxy. See https://habr.com/ru/post/424427/
12+
13+
server {
14+
listen 443 ssl http2;
15+
server_name my-telegram-proxy.server;
16+
17+
# SSL options skipped
18+
19+
location / {
20+
proxy_set_header X-Forwarded-Host $host;
21+
proxy_set_header X-Forwarded-Server $host;
22+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
23+
proxy_pass https://api.telegram.org/;
24+
client_max_body_size 100M;
25+
}
26+
}
27+
*/
28+
29+
func main() {
30+
bot := tbot.New(os.Getenv("TELEGRAM_TOKEN"),
31+
tbot.WithBaseURL("https://my-telegram-proxy.server"))
32+
c := bot.Client()
33+
bot.HandleMessage(".*yo.*", func(m *tbot.Message) {
34+
c.SendChatAction(m.Chat.ID, tbot.ActionTyping)
35+
time.Sleep(1 * time.Second)
36+
c.SendMessage(m.Chat.ID, "hello!")
37+
})
38+
err := bot.Start()
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
}

server.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
type Server struct {
1919
webhookURL string
2020
listenAddr string
21+
baseURL string
2122
httpClient *http.Client
2223
client *Client
2324
token string
@@ -62,12 +63,14 @@ type messageHandler struct {
6263
New creates new Server. Available options:
6364
WithWebook(url, addr string)
6465
WithHTTPClient(client *http.Client)
66+
WithBaseURL(baseURL string)
6567
*/
6668
func New(token string, options ...ServerOption) *Server {
6769
s := &Server{
6870
httpClient: http.DefaultClient,
6971
token: token,
7072
logger: nopLogger{},
73+
baseURL: apiBaseURL,
7174

7275
editMessageHandler: func(*Message) {},
7376
channelPostHandler: func(*Message) {},
@@ -86,7 +89,7 @@ func New(token string, options ...ServerOption) *Server {
8689
opt(s)
8790
}
8891
// bot, err := tgbotapi.NewBotAPIWithClient(token, s.httpClient)
89-
s.client = NewClient(token, s.httpClient, apiBaseURL)
92+
s.client = NewClient(token, s.httpClient, s.baseURL)
9093
return s
9194
}
9295

@@ -99,6 +102,14 @@ func WithWebhook(url, addr string) ServerOption {
99102
}
100103
}
101104

105+
// WithBaseURL sets custom apiBaseURL for server.
106+
// It may be necessary to run the server in some countries
107+
func WithBaseURL(baseURL string) ServerOption {
108+
return func(s *Server) {
109+
s.baseURL = baseURL
110+
}
111+
}
112+
102113
// WithHTTPClient sets custom http client for server.
103114
func WithHTTPClient(client *http.Client) ServerOption {
104115
return func(s *Server) {
@@ -210,7 +221,7 @@ func (s *Server) listenUpdates() (chan *Update, error) {
210221

211222
func (s *Server) longPoolUpdates() (chan *Update, error) {
212223
s.logger.Debugf("fetching updates...")
213-
endpoint := fmt.Sprintf("%s/bot%s/%s", apiBaseURL, s.token, "getUpdates")
224+
endpoint := fmt.Sprintf("%s/bot%s/%s", s.baseURL, s.token, "getUpdates")
214225
req, err := http.NewRequest("GET", endpoint, nil)
215226
if err != nil {
216227
return nil, err

0 commit comments

Comments
 (0)