feat: add telegram proxy support (#1764)

* Add support for reverse proxy of telegram API

* Add Telegram API proxy hint

---------

Co-authored-by: Athurg Feng <athurg@gooth.org>
This commit is contained in:
Athurg Gooth
2023-05-29 13:29:21 +08:00
committed by GitHub
parent beb4d8ccb9
commit ce64894abe
7 changed files with 48 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
)
// downloadFileId download file with fileID, return the filepath and blob.
@@ -23,13 +24,19 @@ func (r *Robot) downloadFileID(ctx context.Context, fileID string) (string, []by
// downloadFilepath download file with filepath, you can get filepath by calling GetFile.
func (r *Robot) downloadFilepath(ctx context.Context, filePath string) ([]byte, error) {
token := r.handler.RobotToken(ctx)
if token == "" {
return nil, ErrNoToken
apiURL, err := r.apiURL(ctx)
if err != nil {
return nil, err
}
uri := "https://api.telegram.org/file/bot" + token + "/" + filePath
resp, err := http.Get(uri)
idx := strings.LastIndex(apiURL, "/bot")
if idx < 0 {
return nil, ErrInvalidToken
}
fileURL := apiURL[:idx] + "/file" + apiURL[idx:]
resp, err := http.Get(fileURL + "/" + filePath)
if err != nil {
return nil, fmt.Errorf("fail to http.Get: %s", err)
}