1
0
painor 3 жил өмнө
parent
commit
ff09545ee1

+ 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.
  * @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
  */
 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;
 }

+ 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 = [];
     for (const x of entityArray) {
         if (typeof x === "string") {
-            inputs.push(x);
+            const valid = parseID(x);
+            if (valid) {
+                inputs.push(await client.getInputEntity(valid));
+            } else {
+                inputs.push(x);
+            }
         } else {
             inputs.push(await client.getInputEntity(x));
         }
@@ -333,6 +338,31 @@ export async function _getEntityFromString(
     client: TelegramClient,
     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);
     if (id != undefined) {
         return getInputEntity(client, id);
@@ -362,13 +392,13 @@ export async function _getEntityFromString(
                 const pid = utils.getPeerId(result.peer, false);
                 if (result.peer instanceof Api.PeerUser) {
                     for (const x of result.users) {
-                        if (x.id === pid) {
+                        if (returnBigInt(x.id).equals(returnBigInt(pid))) {
                             return x;
                         }
                     }
                 } else {
                     for (const x of result.chats) {
-                        if (x.id === pid) {
+                        if (returnBigInt(x.id).equals(returnBigInt(pid))) {
                             return x;
                         }
                     }

+ 2 - 2
package-lock.json

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

+ 1 - 1
package.json

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