From d877986dfb919e8f7adf163c255655589a1a3d57 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 2 Apr 2025 03:05:45 +0300 Subject: [PATCH] refactor --- .gitignore | 1 + .golangci.yaml | 32 +------------------------------- auth.go | 12 ++++++------ cmd/test/main.go | 1 - internal/tg/auth.go | 5 +++-- internal/tg/dialogs.go | 14 ++++++++++++-- 6 files changed, 23 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 15e0e62..bcfb3eb 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,4 @@ bin/ .env .cursor/ log.txt +mcp.log diff --git a/.golangci.yaml b/.golangci.yaml index f03b29d..bbfc961 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -29,7 +29,6 @@ linters: - ineffassign - staticcheck - unused - - depguard - asciicheck - bodyclose - canonicalheader @@ -65,41 +64,12 @@ linters-settings: lll: line-length: 150 - goimports: - local-prefixes: "gitlab.com/v8s/trating" - - depguard: - rules: - configuration: - files: - - $all - - "!**/internal/config/*.go" - deny: - - pkg: "github.com/spf13/viper" - desc: Should be used only in config package, to avoid boiler plate - - replace-std: - list-mode: lax - files: - - "**/internal/**/*.go" - deny: - - pkg: "errors" - desc: Use github.com/pkg/errors for proper callstack logging (check README.md) - - pkg: "log" - desc: Use github.com/rs/zerolog/log as replacement - - importas: - no-unaliased: true - alias: - # enforce easycfg like usage - - pkg: github.com/spf13/pflag - alias: cfg - wrapcheck: ignoreSigs: - github.com/pkg/errors.Wrap( - github.com/pkg/errors.Wrapf( - github.com/pkg/errors.New( + - fmt.Errorf gocyclo: min-complexity: 15 diff --git a/auth.go b/auth.go index f1a8ab1..dee7fcb 100644 --- a/auth.go +++ b/auth.go @@ -3,7 +3,7 @@ package main import ( "context" "encoding/json" - "fmt" + "strconv" "github.com/chaindead/telegram-mcp/internal/tg" @@ -34,7 +34,7 @@ func authCommand(_ context.Context, cmd *cli.Command) error { Command string `json:"command"` Env struct { AppID string `json:"TG_APP_ID"` - ApiHash string `json:"TG_API_HASH"` + APIHash string `json:"TG_API_HASH"` } `json:"env"` } `json:"telegram"` }{ @@ -42,16 +42,16 @@ func authCommand(_ context.Context, cmd *cli.Command) error { Command string `json:"command"` Env struct { AppID string `json:"TG_APP_ID"` - ApiHash string `json:"TG_API_HASH"` + APIHash string `json:"TG_API_HASH"` } `json:"env"` }{ Command: "telegram-mcp", Env: struct { AppID string `json:"TG_APP_ID"` - ApiHash string `json:"TG_API_HASH"` + APIHash string `json:"TG_API_HASH"` }{ - AppID: fmt.Sprintf("%d", appID), - ApiHash: apiHash, + AppID: strconv.FormatInt(appID, 10), + APIHash: apiHash, }, }, } diff --git a/cmd/test/main.go b/cmd/test/main.go index 8475c27..11a03fa 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -13,7 +13,6 @@ import ( ) func main() { - zerolog.TimeFieldFormat = zerolog.TimeFormatUnix log.Logger = log.Output(zerolog.ConsoleWriter{ Out: os.Stderr, diff --git a/internal/tg/auth.go b/internal/tg/auth.go index 5909c55..80e0d71 100644 --- a/internal/tg/auth.go +++ b/internal/tg/auth.go @@ -28,12 +28,13 @@ func Auth(phone string, appID int64, appHash string, sessionPath string) error { if err := client.Run(context.Background(), func(ctx context.Context) error { // Authenticate if needed - flow := auth.NewFlow(auth.Constant(phone, "", auth.CodeAuthenticatorFunc(func(ctx context.Context, _ *tg.AuthSentCode) (string, error) { + flow := auth.NewFlow(auth.Constant(phone, "", auth.CodeAuthenticatorFunc(func(_ context.Context, _ *tg.AuthSentCode) (string, error) { fmt.Print("Enter code: ") code, err := bufio.NewReader(os.Stdin).ReadString('\n') if err != nil { - return "", err + return "", fmt.Errorf("read code: %w", err) } + return strings.TrimSpace(code), nil })), auth.SendCodeOptions{}) diff --git a/internal/tg/dialogs.go b/internal/tg/dialogs.go index be6bd5c..f42fbe6 100644 --- a/internal/tg/dialogs.go +++ b/internal/tg/dialogs.go @@ -29,6 +29,7 @@ const ( DefaultDialogsLimit = 100 ) +// nolint:lll type DialogsArguments struct { Type DialogType `json:"type,omitempty" jsonschema:"description=Filter dialogs by type (user, chat, channel or empty for all),enum=,enum=user,enum=chat,enum=channel"` Limit int `json:"limit,omitempty" jsonschema:"description=Maximum number of dialogs to return (max: 100),default=100"` @@ -86,7 +87,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) { Users: d.Users, } default: - return fmt.Errorf("unexpected dialogs response type") + return errors.New("unexpected dialogs response type") } result = make([]DialogInfo, 0, len(dialogs.Dialogs)) @@ -108,8 +109,13 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) { continue } + var who string + if message.FromID != nil { + who = message.FromID.String() + } + info.LastMessages = append(info.LastMessages, MessageInfo{ - Who: message.FromID.String(), + Who: who, When: time.Unix(int64(message.Date), 0).Format(time.DateTime), Text: message.Message, IsUnread: !message.Out, @@ -135,6 +141,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) { info.IsVerified = user.Verified result = append(result, info) + break } @@ -154,6 +161,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) { info.Title = chat.Title result = append(result, info) + break } @@ -174,6 +182,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) { info.IsVerified = channel.Verified result = append(result, info) + break } } @@ -206,5 +215,6 @@ func getUserName(user *tg.User) string { if user.LastName != "" { name += " " + user.LastName } + return name }