-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathserve.go
More file actions
122 lines (95 loc) · 3.44 KB
/
serve.go
File metadata and controls
122 lines (95 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/chaindead/telegram-mcp/internal/tg"
"github.com/invopop/jsonschema"
mcp "github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/stdio"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v3"
)
func serve(ctx context.Context, cmd *cli.Command) error {
appID := cmd.Int("app-id")
appHash := cmd.String("api-hash")
sessionPath := cmd.String("session")
dryRun := cmd.Bool("dry")
if schemaURL := cmd.String("schema-version"); schemaURL != "" {
jsonschema.Version = schemaURL
}
_, err := os.Stat(sessionPath)
if err != nil {
return fmt.Errorf("session file not found(%s): %w", sessionPath, err)
}
server := mcp.NewServer(stdio.NewStdioServerTransport())
client := tg.New(int(appID), appHash, sessionPath)
if dryRun {
answer, err := client.GetMe(tg.EmptyArguments{})
if err != nil {
return fmt.Errorf("get user: %w", err)
}
data, err := json.MarshalIndent(answer, "", " ")
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
log.Info().RawJSON("answer", data).Msg("Check GetMe: OK")
answer, err = client.GetDialogs(tg.DialogsArguments{Offset: "", OnlyUnread: true})
if err != nil {
return fmt.Errorf("get dialogs: %w", err)
}
log.Info().RawJSON("answer", []byte(answer.Content[0].TextContent.Text)).Msg("Check GetDialogs: OK")
answer, err = client.GetHistory(tg.HistoryArguments{Name: os.Getenv("TG_TEST_USERNAME")})
if err != nil {
return fmt.Errorf("get nickname history: %w", err)
}
answer, err = client.GetHistory(tg.HistoryArguments{Name: "cht[4626931529]"})
if err != nil {
return fmt.Errorf("get chat history: %w", err)
}
answer, err = client.GetHistory(tg.HistoryArguments{Name: "chn[2225853048:8934705438195741763]"})
if err != nil {
return fmt.Errorf("get chan history: %w", err)
}
log.Info().RawJSON("answer", []byte(answer.Content[0].TextContent.Text)).Msg("Check GetHistory: OK")
answer, err = client.SendDraft(tg.DraftArguments{Name: os.Getenv("TG_TEST_USERNAME"), Text: "test draft"})
if err != nil {
log.Err(err).Msg("Check SendDraft: FAIL")
} else {
log.Info().RawJSON("answer", []byte(answer.Content[0].TextContent.Text)).Msg("Check SendDraft: OK")
}
answer, err = client.ReadHistory(tg.ReadArguments{Name: os.Getenv("TG_TEST_USERNAME")})
if err != nil {
log.Err(err).Msg("Check ReadHistory: FAIL")
} else {
log.Info().RawJSON("answer", []byte(answer.Content[0].TextContent.Text)).Msg("Check ReadHistory: OK")
}
return nil
}
err = server.RegisterTool("tg_me", "Get current telegram account info", client.GetMe)
if err != nil {
return fmt.Errorf("register tool: %w", err)
}
err = server.RegisterTool("tg_dialogs", "Get list of telegram dialogs (chats, channels, users)", client.GetDialogs)
if err != nil {
return fmt.Errorf("register dialogs tool: %w", err)
}
err = server.RegisterTool("tg_dialog", "Get messages of telegram dialog", client.GetHistory)
if err != nil {
return fmt.Errorf("register dialogs tool: %w", err)
}
err = server.RegisterTool("tg_send", "Send draft message to dialog", client.SendDraft)
if err != nil {
return fmt.Errorf("register dialogs tool: %w", err)
}
err = server.RegisterTool("tg_read", "Mark dialog messages as read", client.ReadHistory)
if err != nil {
return fmt.Errorf("register read tool: %w", err)
}
if err := server.Serve(); err != nil {
return fmt.Errorf("serve: %w", err)
}
<-ctx.Done()
return nil
}