alik0211 / mtproto-core

Telegram API JS (MTProto) client library for Node.js and browser
https://mtproto-core.js.org
GNU General Public License v3.0
630 stars 113 forks source link

Working Sample On users.getFullUser #224

Closed thestonytony closed 2 years ago

thestonytony commented 2 years ago

Dear All, I'm very new to javascript programming, I'm developing a simple client to read message from user, and need to replying back, so far I've been able to login, get updates on short messages , from code I took from here discussuion here also. Since not much sample is available out there. I know all methods available on telegram documentation, I hope someone will help me complete the "puzzle" for me.

This is what I've made so far api.js :

const path = require("path");
const MTProto = require("@mtproto/core");
const { sleep } = require("@mtproto/core/src/utils/common");
class API {
  constructor({ test } = { test: false }) {
    this.mtproto = new MTProto({
      api_id: "*hidden*",
      api_hash: "*hidden*",
      test,
      storageOptions: {
        path: path.resolve(__dirname, "./telegramData.json"),
      },
    });
  }

  async call(method, params, options = {}) {
    console.log(method);
    console.log(params);
    console.log(options);
    try {
      const result = await this.mtproto.call(method, params, options);
      console.log(result);
      return result;
    } catch (error) {
      console.error(error);
      const { error_code, error_message } = error;

      if (error_code === 420) {
        const seconds = Number(error_message.split("FLOOD_WAIT_")[1]);
        const ms = seconds * 1000;

        await sleep(ms);

        return this.call(method, params, options);
      }

      if (error_code === 303) {
        const [type, dcIdAsString] = error_message.split("_MIGRATE_");
        const dcId = Number(dcIdAsString);

        // If auth.sendCode call on incorrect DC need change default DC, because
        // call auth.signIn on incorrect DC return PHONE_CODE_EXPIRED error
        if (type === "PHONE") {
          await this.mtproto.setDefaultDc(dcId);
        } else {
          Object.assign(options, { dcId });
        }

        return this.call(method, params, options);
      }

      return Promise.reject(error);
    }
  }

  async getUserID(xid) {
    try {
      console.log('Function getuid');
      console.log(xid)
      const user = await this.call("users.getFullUser", { id: { xid : 'InputUser' } },{});
      return user;
    } catch (error) {
      return error;
    }
  }

  async getUser() {
    try {
      const user = await this.call("users.getFullUser", {
        id: {
          _: "inputUserSelf",
        },
      });

      return user;
    } catch (error) {
      return null;
    }
  }
  sendCode(phone) {
    try {
      return this.call("auth.sendCode", {
        phone_number: phone,
        settings: {
          _: "codeSettings",
        },
      });
    } catch (error) {
      throw error;
    }
  }

  signIn({ code, phone, phone_code_hash }) {
    return this.call("auth.signIn", {
      phone_code: code,
      phone_number: phone,
      phone_code_hash: phone_code_hash,
    });
  }

  signUp({ phone, phone_code_hash }) {
    return this.call("auth.signUp", {
      phone_number: phone,
      phone_code_hash: phone_code_hash,
      first_name: "MTProto",
      last_name: "Core",
    });
  }

  getPassword() {
    return this.call("account.getPassword");
  }

  checkPassword({ srp_id, A, M1 }) {
    return this.call("auth.checkPassword", {
      password: {
        _: "inputCheckPasswordSRP",
        srp_id,
        A,
        M1,
      },
    });
  }
}

module.exports = API;

main.js

const MTProto = require("@mtproto/core");
const prompt = require("prompt");
const API = require("./api");
const api = new API();
async function main() {
    api.mtproto.updates.on('updateShortMessage', (updateInfo) => {
        console.log('updateShortMessage:', updateInfo);
        const { user_id,message,date} = updateInfo;
        console.log('date : ' + date);
        console.log('userid : ' + user_id);
        console.log('message : ' + message);
        const user = api.getUserID(user_id);
        console.log(user);
      });

  const user = await api.getUser();
  console.log("User 1st try: ", user);
  if (!user) {
    const { phone } = await prompt.get("phone");
    const { phone_code_hash } = await api.sendCode(phone);
    const { code } = await prompt.get("code");
    try {
      const signInResult = await api.signIn({
        code,
        phone,
        phone_code_hash,
      });
      console.log(signInResult);

      if (signInResult._ === "auth.authorizationSignUpRequired") {
        const singUpResult = await api.signUp({
          phone,
          phone_code_hash,
        });
        console.log(singUpResult);
      }

      const newUser = await api.getUser();
      console.log("User 2nd try: ", newUser);
    } catch (error) {
      if (error.error_message !== "SESSION_PASSWORD_NEEDED") {
        console.log(`error:`, error);

        return;
      }

      // 2FA...
    }
  }
  else
  {
      console.log('Here......')
  }
  const result = await api.call("help.getNearestDc");
  console.log(result);
}

main();

My question is, can I get userFull from updates by using only user_id I received from short message Update ? If not, what is the suitable approach for me to obtain the user full info, I need the phone number to match with my database, and also send a reply message to the user.

Any help, will be so much appriciated

thestonytony commented 2 years ago

I guess I'll try my luck somewhere else. This library not for me