This commit is contained in:
John Doe 2025-04-02 03:05:45 +03:00
parent 82ec7335cb
commit d877986dfb
6 changed files with 23 additions and 42 deletions

1
.gitignore vendored
View file

@ -71,3 +71,4 @@ bin/
.env .env
.cursor/ .cursor/
log.txt log.txt
mcp.log

View file

@ -29,7 +29,6 @@ linters:
- ineffassign - ineffassign
- staticcheck - staticcheck
- unused - unused
- depguard
- asciicheck - asciicheck
- bodyclose - bodyclose
- canonicalheader - canonicalheader
@ -65,41 +64,12 @@ linters-settings:
lll: lll:
line-length: 150 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: wrapcheck:
ignoreSigs: ignoreSigs:
- github.com/pkg/errors.Wrap( - github.com/pkg/errors.Wrap(
- github.com/pkg/errors.Wrapf( - github.com/pkg/errors.Wrapf(
- github.com/pkg/errors.New( - github.com/pkg/errors.New(
- fmt.Errorf
gocyclo: gocyclo:
min-complexity: 15 min-complexity: 15

12
auth.go
View file

@ -3,7 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "strconv"
"github.com/chaindead/telegram-mcp/internal/tg" "github.com/chaindead/telegram-mcp/internal/tg"
@ -34,7 +34,7 @@ func authCommand(_ context.Context, cmd *cli.Command) error {
Command string `json:"command"` Command string `json:"command"`
Env struct { Env struct {
AppID string `json:"TG_APP_ID"` AppID string `json:"TG_APP_ID"`
ApiHash string `json:"TG_API_HASH"` APIHash string `json:"TG_API_HASH"`
} `json:"env"` } `json:"env"`
} `json:"telegram"` } `json:"telegram"`
}{ }{
@ -42,16 +42,16 @@ func authCommand(_ context.Context, cmd *cli.Command) error {
Command string `json:"command"` Command string `json:"command"`
Env struct { Env struct {
AppID string `json:"TG_APP_ID"` AppID string `json:"TG_APP_ID"`
ApiHash string `json:"TG_API_HASH"` APIHash string `json:"TG_API_HASH"`
} `json:"env"` } `json:"env"`
}{ }{
Command: "telegram-mcp", Command: "telegram-mcp",
Env: struct { Env: struct {
AppID string `json:"TG_APP_ID"` AppID string `json:"TG_APP_ID"`
ApiHash string `json:"TG_API_HASH"` APIHash string `json:"TG_API_HASH"`
}{ }{
AppID: fmt.Sprintf("%d", appID), AppID: strconv.FormatInt(appID, 10),
ApiHash: apiHash, APIHash: apiHash,
}, },
}, },
} }

View file

@ -13,7 +13,6 @@ import (
) )
func main() { func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{ log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr, Out: os.Stderr,

View file

@ -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 { if err := client.Run(context.Background(), func(ctx context.Context) error {
// Authenticate if needed // 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: ") fmt.Print("Enter code: ")
code, err := bufio.NewReader(os.Stdin).ReadString('\n') code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("read code: %w", err)
} }
return strings.TrimSpace(code), nil return strings.TrimSpace(code), nil
})), auth.SendCodeOptions{}) })), auth.SendCodeOptions{})

View file

@ -29,6 +29,7 @@ const (
DefaultDialogsLimit = 100 DefaultDialogsLimit = 100
) )
// nolint:lll
type DialogsArguments struct { 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"` 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"` 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, Users: d.Users,
} }
default: default:
return fmt.Errorf("unexpected dialogs response type") return errors.New("unexpected dialogs response type")
} }
result = make([]DialogInfo, 0, len(dialogs.Dialogs)) result = make([]DialogInfo, 0, len(dialogs.Dialogs))
@ -108,8 +109,13 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) {
continue continue
} }
var who string
if message.FromID != nil {
who = message.FromID.String()
}
info.LastMessages = append(info.LastMessages, MessageInfo{ info.LastMessages = append(info.LastMessages, MessageInfo{
Who: message.FromID.String(), Who: who,
When: time.Unix(int64(message.Date), 0).Format(time.DateTime), When: time.Unix(int64(message.Date), 0).Format(time.DateTime),
Text: message.Message, Text: message.Message,
IsUnread: !message.Out, IsUnread: !message.Out,
@ -135,6 +141,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) {
info.IsVerified = user.Verified info.IsVerified = user.Verified
result = append(result, info) result = append(result, info)
break break
} }
@ -154,6 +161,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) {
info.Title = chat.Title info.Title = chat.Title
result = append(result, info) result = append(result, info)
break break
} }
@ -174,6 +182,7 @@ func (c *Client) GetDialogs(args DialogsArguments) (*mcp.ToolResponse, error) {
info.IsVerified = channel.Verified info.IsVerified = channel.Verified
result = append(result, info) result = append(result, info)
break break
} }
} }
@ -206,5 +215,6 @@ func getUserName(user *tg.User) string {
if user.LastName != "" { if user.LastName != "" {
name += " " + user.LastName name += " " + user.LastName
} }
return name return name
} }