gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.18k stars 171 forks source link

VERIFICATION CODE #550

Open kshontop opened 1 year ago

kshontop commented 1 year ago

I create a bot with Telegraf that allows you to connect your Telegram account to the bot except that when I put the verification code, my account does not connect.

const { Telegraf } = require("telegraf");
const { TelegramClient, Api } = require("telegram");
const { StringSession } = require("telegram/sessions");
const { PhoneNumberInvalidError } = require("telegram/errors");
const validator = require("validator");
const pool = require("./src/database.js");

const BOT_TOKEN = "";
const apiID = 123456;
const apiHash = "";

const bot = new Telegraf(BOT_TOKEN);
const client = new TelegramClient(new StringSession(""), apiID, apiHash, {
  botToken: BOT_TOKEN
});

bot.command(/.*/, async (ctx) => {
  const phoneNumber = ctx.message.text.replace(/\D/g, ''); // Supprimer les caractères non numériques
  if (validator.isMobilePhone(phoneNumber, "any")) {
    try {
      await client.connect({
        phoneNumber: phoneNumber // Passer le numéro de téléphone correctement
      });

      const { phone_code_hash } = await client.invoke(
        new Api.auth.SendCode({
          phoneNumber: phoneNumber,
          apiId: apiID,
          apiHash: apiHash,
          settings: new Api.CodeSettings(),
        })
      );
      ctx.reply("Veuillez entrer le code de vérification :");

      // Attendre la saisie du code de vérification par l'utilisateur
      bot.command(/.*/, async (ctx) => {
        const verificationCode = ctx.message.text;
        try {
          const { user } = await client.invoke(
            new Api.auth.SignIn({
              phoneNumber: phoneNumber,
              phoneCodeHash: phone_code_hash,
              phoneCode: verificationCode
            })
          );

          if (user) {
            ctx.reply("Vous êtes connecté sur le compte avec succès !");
          } else {
            ctx.reply("Échec de la connexion. Veuillez vérifier le code de vérification.");
          }
        } catch (error) {
          ctx.reply(`Erreur lors de la connexion : ${error}`);
        }
      });

    } catch (error) {
      ctx.reply(`Erreur lors de la connexion : ${error}`);
    }
  } else {
    ctx.reply("Numéro de téléphone invalide !");
  }
});

bot.startPolling();
painor commented 1 year ago

are you sure that's how Telegraf works? multiple bot.command and all.

What was the error you saw?

anyway if you send the verification over telegram it will expire directly;

kshontop commented 1 year ago

Do you have a discord to talk about it in more detail?