|
@@ -9,12 +9,7 @@ import bigInt from "big-integer";
|
|
// UserMethods {
|
|
// UserMethods {
|
|
// region Invoking Telegram request
|
|
// region Invoking Telegram request
|
|
|
|
|
|
-/**
|
|
|
|
- * Invokes a MTProtoRequest (sends and receives it) and returns its result
|
|
|
|
- * @param client
|
|
|
|
- * @param request
|
|
|
|
- * @returns {Promise}
|
|
|
|
- */
|
|
|
|
|
|
+/** @hidden */
|
|
export async function invoke<R extends Api.AnyRequest>(
|
|
export async function invoke<R extends Api.AnyRequest>(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
request: R
|
|
request: R
|
|
@@ -80,6 +75,7 @@ export async function invoke<R extends Api.AnyRequest>(
|
|
throw new Error(`Request was unsuccessful ${attempt} time(s)`);
|
|
throw new Error(`Request was unsuccessful ${attempt} time(s)`);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function getMe(
|
|
export async function getMe(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
inputPeer = false
|
|
inputPeer = false
|
|
@@ -110,6 +106,7 @@ export async function getMe(
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function isBot(client: TelegramClient) {
|
|
export async function isBot(client: TelegramClient) {
|
|
if (client._bot === undefined) {
|
|
if (client._bot === undefined) {
|
|
const me = await client.getMe();
|
|
const me = await client.getMe();
|
|
@@ -120,6 +117,7 @@ export async function isBot(client: TelegramClient) {
|
|
return client._bot;
|
|
return client._bot;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function isUserAuthorized(client: TelegramClient) {
|
|
export async function isUserAuthorized(client: TelegramClient) {
|
|
try {
|
|
try {
|
|
await client.invoke(new Api.updates.GetState());
|
|
await client.invoke(new Api.updates.GetState());
|
|
@@ -129,6 +127,7 @@ export async function isUserAuthorized(client: TelegramClient) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function getEntity(
|
|
export async function getEntity(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
entity: EntityLike | EntityLike[]
|
|
entity: EntityLike | EntityLike[]
|
|
@@ -152,12 +151,13 @@ export async function getEntity(
|
|
const lists = new Map<number, any[]>([
|
|
const lists = new Map<number, any[]>([
|
|
[_EntityType.USER, []],
|
|
[_EntityType.USER, []],
|
|
[_EntityType.CHAT, []],
|
|
[_EntityType.CHAT, []],
|
|
- [_EntityType.CHANNEL, []],
|
|
|
|
|
|
+ [_EntityType.CHANNEL, []]
|
|
]);
|
|
]);
|
|
for (const x of inputs) {
|
|
for (const x of inputs) {
|
|
try {
|
|
try {
|
|
lists.get(_entityType(x))!.push(x);
|
|
lists.get(_entityType(x))!.push(x);
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
}
|
|
}
|
|
let users = lists.get(_EntityType.USER)!;
|
|
let users = lists.get(_EntityType.USER)!;
|
|
let chats = lists.get(_EntityType.CHAT)!;
|
|
let chats = lists.get(_EntityType.CHAT)!;
|
|
@@ -166,7 +166,7 @@ export async function getEntity(
|
|
if (users.length) {
|
|
if (users.length) {
|
|
users = await client.invoke(
|
|
users = await client.invoke(
|
|
new Api.users.GetUsers({
|
|
new Api.users.GetUsers({
|
|
- id: users,
|
|
|
|
|
|
+ id: users
|
|
})
|
|
})
|
|
);
|
|
);
|
|
}
|
|
}
|
|
@@ -213,6 +213,7 @@ export async function getEntity(
|
|
return single ? result[0] : result;
|
|
return single ? result[0] : result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function getInputEntity(
|
|
export async function getInputEntity(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
peer: EntityLike
|
|
peer: EntityLike
|
|
@@ -222,7 +223,8 @@ export async function getInputEntity(
|
|
try {
|
|
try {
|
|
return utils.getInputPeer(peer);
|
|
return utils.getInputPeer(peer);
|
|
// eslint-disable-next-line no-empty
|
|
// eslint-disable-next-line no-empty
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
// Next in priority is having a peer (or its ID) cached in-memory
|
|
// Next in priority is having a peer (or its ID) cached in-memory
|
|
try {
|
|
try {
|
|
// 0x2d45687 == crc32(b'Peer')
|
|
// 0x2d45687 == crc32(b'Peer')
|
|
@@ -236,7 +238,8 @@ export async function getInputEntity(
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// eslint-disable-next-line no-empty
|
|
// eslint-disable-next-line no-empty
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
// Then come known strings that take precedence
|
|
// Then come known strings that take precedence
|
|
if (typeof peer == "string") {
|
|
if (typeof peer == "string") {
|
|
if (["me", "this", "self"].includes(peer)) {
|
|
if (["me", "this", "self"].includes(peer)) {
|
|
@@ -250,7 +253,8 @@ export async function getInputEntity(
|
|
return client.session.getInputEntity(peer);
|
|
return client.session.getInputEntity(peer);
|
|
}
|
|
}
|
|
// eslint-disable-next-line no-empty
|
|
// eslint-disable-next-line no-empty
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
// Only network left to try
|
|
// Only network left to try
|
|
if (typeof peer === "string") {
|
|
if (typeof peer === "string") {
|
|
return utils.getInputPeer(await _getEntityFromString(client, peer));
|
|
return utils.getInputPeer(await _getEntityFromString(client, peer));
|
|
@@ -266,9 +270,9 @@ export async function getInputEntity(
|
|
id: [
|
|
id: [
|
|
new Api.InputUser({
|
|
new Api.InputUser({
|
|
userId: peer.userId,
|
|
userId: peer.userId,
|
|
- accessHash: bigInt.zero,
|
|
|
|
- }),
|
|
|
|
- ],
|
|
|
|
|
|
+ accessHash: bigInt.zero
|
|
|
|
+ })
|
|
|
|
+ ]
|
|
})
|
|
})
|
|
);
|
|
);
|
|
if (users.length && !(users[0] instanceof Api.UserEmpty)) {
|
|
if (users.length && !(users[0] instanceof Api.UserEmpty)) {
|
|
@@ -283,7 +287,7 @@ export async function getInputEntity(
|
|
}
|
|
}
|
|
} else if (peer instanceof Api.PeerChat) {
|
|
} else if (peer instanceof Api.PeerChat) {
|
|
return new Api.InputPeerChat({
|
|
return new Api.InputPeerChat({
|
|
- chatId: peer.chatId,
|
|
|
|
|
|
+ chatId: peer.chatId
|
|
});
|
|
});
|
|
} else if (peer instanceof Api.PeerChannel) {
|
|
} else if (peer instanceof Api.PeerChannel) {
|
|
try {
|
|
try {
|
|
@@ -292,9 +296,9 @@ export async function getInputEntity(
|
|
id: [
|
|
id: [
|
|
new Api.InputChannel({
|
|
new Api.InputChannel({
|
|
channelId: peer.channelId,
|
|
channelId: peer.channelId,
|
|
- accessHash: bigInt.zero,
|
|
|
|
- }),
|
|
|
|
- ],
|
|
|
|
|
|
+ accessHash: bigInt.zero
|
|
|
|
+ })
|
|
|
|
+ ]
|
|
})
|
|
})
|
|
);
|
|
);
|
|
|
|
|
|
@@ -308,11 +312,12 @@ export async function getInputEntity(
|
|
throw new Error(
|
|
throw new Error(
|
|
`Could not find the input entity for ${JSON.stringify(peer)}.
|
|
`Could not find the input entity for ${JSON.stringify(peer)}.
|
|
Please read https://` +
|
|
Please read https://` +
|
|
- "docs.telethon.dev/en/latest/concepts/entities.html to" +
|
|
|
|
- " find out more details."
|
|
|
|
|
|
+ "docs.telethon.dev/en/latest/concepts/entities.html to" +
|
|
|
|
+ " find out more details."
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function _getEntityFromString(
|
|
export async function _getEntityFromString(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
string: string
|
|
string: string
|
|
@@ -322,7 +327,7 @@ export async function _getEntityFromString(
|
|
try {
|
|
try {
|
|
const result = await client.invoke(
|
|
const result = await client.invoke(
|
|
new Api.contacts.GetContacts({
|
|
new Api.contacts.GetContacts({
|
|
- hash: 0,
|
|
|
|
|
|
+ hash: 0
|
|
})
|
|
})
|
|
);
|
|
);
|
|
if (!(result instanceof Api.contacts.ContactsNotModified)) {
|
|
if (!(result instanceof Api.contacts.ContactsNotModified)) {
|
|
@@ -336,7 +341,7 @@ export async function _getEntityFromString(
|
|
if (e.errorMessage === "BOT_METHOD_INVALID") {
|
|
if (e.errorMessage === "BOT_METHOD_INVALID") {
|
|
throw new Error(
|
|
throw new Error(
|
|
"Cannot get entity by phone number as a " +
|
|
"Cannot get entity by phone number as a " +
|
|
- "bot (try using integer IDs, not strings)"
|
|
|
|
|
|
+ "bot (try using integer IDs, not strings)"
|
|
);
|
|
);
|
|
}
|
|
}
|
|
throw e;
|
|
throw e;
|
|
@@ -348,13 +353,13 @@ export async function _getEntityFromString(
|
|
if (isInvite) {
|
|
if (isInvite) {
|
|
const invite = await client.invoke(
|
|
const invite = await client.invoke(
|
|
new Api.messages.CheckChatInvite({
|
|
new Api.messages.CheckChatInvite({
|
|
- hash: username,
|
|
|
|
|
|
+ hash: username
|
|
})
|
|
})
|
|
);
|
|
);
|
|
if (invite instanceof Api.ChatInvite) {
|
|
if (invite instanceof Api.ChatInvite) {
|
|
throw new Error(
|
|
throw new Error(
|
|
"Cannot get entity from a channel (or group) " +
|
|
"Cannot get entity from a channel (or group) " +
|
|
- "that you are not part of. Join the group and retry"
|
|
|
|
|
|
+ "that you are not part of. Join the group and retry"
|
|
);
|
|
);
|
|
} else if (invite instanceof Api.ChatInviteAlready) {
|
|
} else if (invite instanceof Api.ChatInviteAlready) {
|
|
return invite.chat;
|
|
return invite.chat;
|
|
@@ -389,6 +394,7 @@ export async function _getEntityFromString(
|
|
throw new Error(`Cannot find any entity corresponding to "${string}"`);
|
|
throw new Error(`Cannot find any entity corresponding to "${string}"`);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function getPeerId(
|
|
export async function getPeerId(
|
|
client: TelegramClient,
|
|
client: TelegramClient,
|
|
peer: EntityLike,
|
|
peer: EntityLike,
|
|
@@ -410,6 +416,7 @@ export async function getPeerId(
|
|
return utils.getPeerId(peer, addMark);
|
|
return utils.getPeerId(peer, addMark);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function _getPeer(client: TelegramClient, peer: EntityLike) {
|
|
export async function _getPeer(client: TelegramClient, peer: EntityLike) {
|
|
if (!peer) {
|
|
if (!peer) {
|
|
return undefined;
|
|
return undefined;
|
|
@@ -418,10 +425,11 @@ export async function _getPeer(client: TelegramClient, peer: EntityLike) {
|
|
return new cls({
|
|
return new cls({
|
|
userId: i,
|
|
userId: i,
|
|
channelId: i,
|
|
channelId: i,
|
|
- chatId: i,
|
|
|
|
|
|
+ chatId: i
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function _getInputDialog(client: TelegramClient, dialog: any) {
|
|
export async function _getInputDialog(client: TelegramClient, dialog: any) {
|
|
try {
|
|
try {
|
|
if (dialog.SUBCLASS_OF_ID == 0xa21c9795) {
|
|
if (dialog.SUBCLASS_OF_ID == 0xa21c9795) {
|
|
@@ -431,15 +439,17 @@ export async function _getInputDialog(client: TelegramClient, dialog: any) {
|
|
} else if (dialog.SUBCLASS_OF_ID == 0xc91c90b6) {
|
|
} else if (dialog.SUBCLASS_OF_ID == 0xc91c90b6) {
|
|
//crc32(b'InputPeer')
|
|
//crc32(b'InputPeer')
|
|
return new Api.InputDialogPeer({
|
|
return new Api.InputDialogPeer({
|
|
- peer: dialog,
|
|
|
|
|
|
+ peer: dialog
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
return new Api.InputDialogPeer({
|
|
return new Api.InputDialogPeer({
|
|
- peer: dialog,
|
|
|
|
|
|
+ peer: dialog
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export async function _getInputNotify(client: TelegramClient, notify: any) {
|
|
export async function _getInputNotify(client: TelegramClient, notify: any) {
|
|
try {
|
|
try {
|
|
if (notify.SUBCLASS_OF_ID == 0x58981615) {
|
|
if (notify.SUBCLASS_OF_ID == 0x58981615) {
|
|
@@ -448,12 +458,14 @@ export async function _getInputNotify(client: TelegramClient, notify: any) {
|
|
}
|
|
}
|
|
return notify;
|
|
return notify;
|
|
}
|
|
}
|
|
- } catch (e) {}
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
+ }
|
|
return new Api.InputNotifyPeer({
|
|
return new Api.InputNotifyPeer({
|
|
- peer: await client.getInputEntity(notify),
|
|
|
|
|
|
+ peer: await client.getInputEntity(notify)
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/** @hidden */
|
|
export function _selfId(client: TelegramClient) {
|
|
export function _selfId(client: TelegramClient) {
|
|
return client._selfInputPeer ? client._selfInputPeer.userId : undefined;
|
|
return client._selfInputPeer ? client._selfInputPeer.userId : undefined;
|
|
}
|
|
}
|