Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add telegram proxy support #1764

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/system_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func (upsert SystemSettingUpsert) Validate() error {
if upsert.Value == "" {
return nil
}
// Robot Token with Reverse Proxy shoule like `http.../bot<token>`
if strings.HasPrefix(upsert.Value, "http") {
slashIndex := strings.LastIndexAny(upsert.Value, "/")
if strings.HasPrefix(upsert.Value[slashIndex:], "/bot") {
return nil
}
return fmt.Errorf("token start with `http` must end with `/bot<token>`")
}
fragments := strings.Split(upsert.Value, ":")
if len(fragments) != 2 {
return fmt.Errorf(systemSettingUnmarshalError, settingName)
Expand Down
17 changes: 12 additions & 5 deletions plugin/telegram/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
)

// downloadFileId download file with fileID, return the filepath and blob.
Expand All @@ -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
}

idx := strings.LastIndex(apiURL, "/bot")
if idx < 0 {
return nil, ErrInvalidToken
}

uri := "https://api.telegram.org/file/bot" + token + "/" + filePath
resp, err := http.Get(uri)
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)
}
Expand Down
12 changes: 4 additions & 8 deletions plugin/telegram/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package telegram
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)

var ErrNoToken = errors.New("token is empty")

func (r *Robot) postForm(ctx context.Context, apiPath string, formData url.Values, result any) error {
token := r.handler.RobotToken(ctx)
if token == "" {
return ErrNoToken
apiURL, err := r.apiURL(ctx)
if err != nil {
return err
}

uri := "https://api.telegram.org/bot" + token + apiPath
resp, err := http.PostForm(uri, formData)
resp, err := http.PostForm(apiURL+apiPath, formData)
if err != nil {
return fmt.Errorf("fail to http.PostForm: %s", err)
}
Expand Down
19 changes: 18 additions & 1 deletion plugin/telegram/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package telegram

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/usememos/memos/common/log"
Expand Down Expand Up @@ -32,7 +34,7 @@ func (r *Robot) Start(ctx context.Context) {

for {
updates, err := r.GetUpdates(ctx, offset)
if err == ErrNoToken {
if err == ErrInvalidToken {
time.Sleep(noTokenWait)
continue
}
Expand Down Expand Up @@ -79,3 +81,18 @@ func (r *Robot) Start(ctx context.Context) {
}
}
}

var ErrInvalidToken = errors.New("token is invalid")

func (r *Robot) apiURL(ctx context.Context) (string, error) {
token := r.handler.RobotToken(ctx)
if token == "" {
return "", ErrInvalidToken
}

if strings.HasPrefix(token, "http") {
return token, nil
}

return "https://api.telegram.org/bot" + token, nil
}
5 changes: 4 additions & 1 deletion web/src/components/Settings/SystemSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ const SystemSection = () => {
<div className="flex flex-row items-center">
<div className="w-auto flex items-center">
<span className="text-sm mr-1">{t("setting.system-section.telegram-robot-token")}</span>
<HelpButton icon="help" url="https://usememos.com/docs/integration/telegram-bot" />
<HelpButton
hint={t("setting.system-section.telegram-robot-token-description")}
url="https://usememos.com/docs/integration/telegram-bot"
/>
</div>
</div>
<Button onClick={handleSaveTelegramRobotToken}>{t("common.save")}</Button>
Expand Down
2 changes: 1 addition & 1 deletion web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
"additional-style-placeholder": "Additional CSS code",
"additional-script-placeholder": "Additional JavaScript code",
"telegram-robot-token": "Telegram Robot Token",
"telegram-robot-token-description": "Get from @BotFather of Telegram",
"telegram-robot-token-description": "Telegram Robot Token or API Proxy like `http.../bot<token>`",
"telegram-robot-token-placeholder": "Your Telegram Robot token",
"openai-api-key": "OpenAI: API Key",
"openai-api-key-description": "Get API key",
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
"ignore-version-upgrade": "忽略版本升级",
"telegram-robot-token": "Telegram 机器人 Token",
"telegram-robot-token-description": "从 Telegram 的 @BotFather 处获取",
"telegram-robot-token-description": "Telegram 机器人Token或`http.../bot<token>`格式的代理地址",
"telegram-robot-token-placeholder": "Telegram 的机器人 Token",
"openai-api-host": "OpenAI:API Host",
"openai-api-host-placeholder": "默认:https://api.openai.com/",
Expand Down