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
.cursor/
log.txt
mcp.log

View file

@ -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

12
auth.go
View file

@ -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,
},
},
}

View file

@ -13,7 +13,6 @@ import (
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{
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 {
// 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{})

View file

@ -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
}