painor 3 лет назад
Родитель
Сommit
ff09545ee1
5 измененных файлов с 43 добавлено и 12 удалено
  1. 6 5
      gramjs/Utils.ts
  2. 1 1
      gramjs/Version.ts
  3. 33 3
      gramjs/client/users.ts
  4. 2 2
      package-lock.json
  5. 1 1
      package.json

+ 6 - 5
gramjs/Utils.ts

@@ -1182,10 +1182,11 @@ export function getMessageId(message: any): number | undefined {
  * Parses the given phone, or returns `undefined` if it's invalid.
  * Parses the given phone, or returns `undefined` if it's invalid.
  * @param phone
  * @param phone
  */
  */
-export function parsePhone(phone: string | number) {
-    phone = phone.toString().replace(/[+()\s-]/gm, "");
-
-    return !isNaN(parseInt(phone)) ? phone : undefined;
+export function parsePhone(phone: string) {
+    phone = phone.toString().replace(/[()\s-]/gm, "");
+    if (phone.startsWith("+") && phone.split("+").length - 1 == 1) {
+        return !isNaN(Number(phone)) ? phone : undefined;
+    }
 }
 }
 
 
 /**
 /**
@@ -1193,7 +1194,7 @@ export function parsePhone(phone: string | number) {
  * @param id
  * @param id
  */
  */
 export function parseID(id: string) {
 export function parseID(id: string) {
-    const isValid = /^([0-9][0-9]*)$/.test(id);
+    const isValid = /^(-?[0-9][0-9]*)$/.test(id);
 
 
     return isValid ? bigInt(id) : undefined;
     return isValid ? bigInt(id) : undefined;
 }
 }

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.0.0";
+export const version = "2.0.1";

+ 33 - 3
gramjs/client/users.ts

@@ -142,7 +142,12 @@ export async function getEntity(
     const inputs = [];
     const inputs = [];
     for (const x of entityArray) {
     for (const x of entityArray) {
         if (typeof x === "string") {
         if (typeof x === "string") {
-            inputs.push(x);
+            const valid = parseID(x);
+            if (valid) {
+                inputs.push(await client.getInputEntity(valid));
+            } else {
+                inputs.push(x);
+            }
         } else {
         } else {
             inputs.push(await client.getInputEntity(x));
             inputs.push(await client.getInputEntity(x));
         }
         }
@@ -333,6 +338,31 @@ export async function _getEntityFromString(
     client: TelegramClient,
     client: TelegramClient,
     string: string
     string: string
 ) {
 ) {
+    const phone = utils.parsePhone(string);
+    if (phone) {
+        try {
+            const result = await client.invoke(
+                new Api.contacts.GetContacts({
+                    hash: bigInt.zero,
+                })
+            );
+            if (!(result instanceof Api.contacts.ContactsNotModified)) {
+                for (const user of result.users) {
+                    if (user instanceof Api.User && user.phone === phone) {
+                        return user;
+                    }
+                }
+            }
+        } catch (e: any) {
+            if (e.errorMessage === "BOT_METHOD_INVALID") {
+                throw new Error(
+                    "Cannot get entity by phone number as a " +
+                        "bot (try using integer IDs, not strings)"
+                );
+            }
+            throw e;
+        }
+    }
     const id = utils.parseID(string);
     const id = utils.parseID(string);
     if (id != undefined) {
     if (id != undefined) {
         return getInputEntity(client, id);
         return getInputEntity(client, id);
@@ -362,13 +392,13 @@ export async function _getEntityFromString(
                 const pid = utils.getPeerId(result.peer, false);
                 const pid = utils.getPeerId(result.peer, false);
                 if (result.peer instanceof Api.PeerUser) {
                 if (result.peer instanceof Api.PeerUser) {
                     for (const x of result.users) {
                     for (const x of result.users) {
-                        if (x.id === pid) {
+                        if (returnBigInt(x.id).equals(returnBigInt(pid))) {
                             return x;
                             return x;
                         }
                         }
                     }
                     }
                 } else {
                 } else {
                     for (const x of result.chats) {
                     for (const x of result.chats) {
-                        if (x.id === pid) {
+                        if (returnBigInt(x.id).equals(returnBigInt(pid))) {
                             return x;
                             return x;
                         }
                         }
                     }
                     }

+ 2 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
 {
   "name": "telegram",
   "name": "telegram",
-  "version": "2.0.0",
+  "version": "2.0.1",
   "lockfileVersion": 2,
   "lockfileVersion": 2,
   "requires": true,
   "requires": true,
   "packages": {
   "packages": {
     "": {
     "": {
       "name": "telegram",
       "name": "telegram",
-      "version": "2.0.0",
+      "version": "2.0.1",
       "license": "MIT",
       "license": "MIT",
       "dependencies": {
       "dependencies": {
         "@cryptography/aes": "^0.1.1",
         "@cryptography/aes": "^0.1.1",

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "telegram",
   "name": "telegram",
-  "version": "2.0.0",
+  "version": "2.0.1",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "main": "index.js",
   "main": "index.js",
   "types": "index.d.ts",
   "types": "index.d.ts",