feat: add 2fa support, recreate session

This commit is contained in:
John Doe 2025-04-07 13:07:40 +03:00
parent c511f846dd
commit 66bd1b7dcc
6 changed files with 30 additions and 9 deletions

View file

@ -87,14 +87,19 @@ Before you can use the server, you need to connect to the Telegram API.
1. Get the API ID and hash from [Telegram API](https://my.telegram.org/auth)
2. Run the following command:
> __Note:__
> If you have 2FA enabled: add --password <2fa_password>
> __Note:__
> If you want to override existing session: add --new
```bash
telegram-mcp auth --app-id <your-api-id> --api-hash <your-api-hash> --phone <your-phone-number>
```
Enter the code you received from Telegram to connect to the API.
📩 Enter the code you received from Telegram to connect to the API.
The password may be required if you have two-factor authentication enabled.
3. Done!
### Client Configuration

View file

@ -49,7 +49,7 @@ tasks:
tag:
desc: Create a new tag
cmds:
- git tag -a v0.1.7
- git push origin v0.1.7
- git tag -a v0.1.8
- git push origin v0.1.8

View file

@ -13,6 +13,8 @@ import (
func authCommand(_ context.Context, cmd *cli.Command) error {
phone := cmd.String("phone")
newSession := cmd.Bool("new")
pass := cmd.String("password")
appID := cmd.Root().Int("app-id")
apiHash := cmd.Root().String("api-hash")
sessionPath := cmd.Root().String("session")
@ -24,7 +26,7 @@ func authCommand(_ context.Context, cmd *cli.Command) error {
Int64("app-id", appID).
Msg("Authenticate with Telegram")
err := tg.Auth(phone, appID, apiHash, sessionPath)
err := tg.Auth(phone, appID, apiHash, sessionPath, pass, newSession)
if err != nil {
log.Fatal().Err(err).Msg("Failed to authenticate with Telegram")
}

View file

@ -14,7 +14,11 @@ import (
"github.com/rs/zerolog/log"
)
func Auth(phone string, appID int64, appHash string, sessionPath string) error {
func Auth(phone string, appID int64, appHash string, sessionPath string, password string, newSession bool) error {
if newSession {
_ = os.Remove(sessionPath)
}
client := telegram.NewClient(int(appID), appHash, telegram.Options{
SessionStorage: &telegram.FileSessionStorage{
Path: sessionPath,
@ -28,8 +32,8 @@ 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(_ context.Context, _ *tg.AuthSentCode) (string, error) {
fmt.Print("Enter code: ")
flow := auth.NewFlow(auth.Constant(phone, password, 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 "", fmt.Errorf("read code: %w", err)

10
main.go
View file

@ -77,6 +77,16 @@ func main() {
Required: true,
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "password",
Usage: "Password for 2FA if exists",
HideDefault: true,
},
&cli.BoolFlag{
Name: "new",
Usage: "Remove old session and create new one",
HideDefault: true,
},
},
Action: authCommand,
},

View file

@ -48,7 +48,7 @@ func serve(ctx context.Context, cmd *cli.Command) error {
log.Info().RawJSON("answer", []byte(answer.Content[0].TextContent.Text)).Msg("Check GetDialogs: OK")
answer, err = client.GetHistory(tg.HistoryArguments{Name: "lalal", Offset: 5574})
answer, err = client.GetHistory(tg.HistoryArguments{Name: os.Getenv("TG_TEST_USERNAME")})
if err != nil {
return fmt.Errorf("get histore: %w", err)
}