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

+ 1 - 1
gramjs/Helpers.ts

@@ -61,7 +61,7 @@ export function betterConsoleLog(object: { [key: string]: any }) {
     const toPrint: { [key: string]: any } = {};
     for (const key in object) {
         if (object.hasOwnProperty(key)) {
-            if (!key.startsWith("_") && key!="originalArgs") {
+            if (!key.startsWith("_") && key != "originalArgs") {
                 toPrint[key] = object[key];
             }
         }

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "1.10.10";
+export const version = "1.11.0";

+ 5 - 0
gramjs/client/TelegramClient.ts

@@ -26,6 +26,7 @@ import { _handleUpdate, _updateLoop } from "./updates";
 import { Session } from "../sessions";
 import { inspect } from "util";
 import { Album, AlbumEvent } from "../events/Album";
+import { CallbackQuery, CallbackQueryEvent } from "../events/CallbackQuery";
 
 /**
  * The TelegramClient uses several methods in different files to provide all the common functionality in a nice interface.</br>
@@ -907,6 +908,10 @@ export class TelegramClient extends TelegramBaseClient {
         callback: { (event: NewMessageEvent): void },
         event: NewMessage
     ): void;
+    addEventHandler(
+        callback: { (event: CallbackQueryEvent): void },
+        event: CallbackQuery
+    ): void;
     addEventHandler(
         callback: { (event: AlbumEvent): void },
         event: Album

+ 14 - 14
gramjs/crypto/RSA.ts

@@ -9,26 +9,26 @@ import {
 
 const PUBLIC_KEYS = [
     {
-        fingerprint: bigInt('-3414540481677951611'),
+        fingerprint: bigInt("-3414540481677951611"),
         n: bigInt(
-            '2937959817066933702298617714945612856538843112005886376816255642404751219133084745514657634448776440866'
-            + '1701890505066208632169112269581063774293102577308490531282748465986139880977280302242772832972539403531'
-            + '3160108704012876427630091361567343395380424193887227773571344877461690935390938502512438971889287359033'
-            + '8945177273024525306296338410881284207988753897636046529094613963869149149606209957083647645485599631919'
-            + '2747663615955633778034897140982517446405334423701359108810182097749467210509584293428076654573384828809'
-            + '574217079944388301239431309115013843331317877374435868468779972014486325557807783825502498215169806323',
+            "2937959817066933702298617714945612856538843112005886376816255642404751219133084745514657634448776440866" +
+                "1701890505066208632169112269581063774293102577308490531282748465986139880977280302242772832972539403531" +
+                "3160108704012876427630091361567343395380424193887227773571344877461690935390938502512438971889287359033" +
+                "8945177273024525306296338410881284207988753897636046529094613963869149149606209957083647645485599631919" +
+                "2747663615955633778034897140982517446405334423701359108810182097749467210509584293428076654573384828809" +
+                "574217079944388301239431309115013843331317877374435868468779972014486325557807783825502498215169806323"
         ),
         e: 65537,
     },
     {
-        fingerprint: bigInt('-5595554452916591101'),
+        fingerprint: bigInt("-5595554452916591101"),
         n: bigInt(
-            '2534288944884041556497168959071347320689884775908477905258202659454602246385394058588521595116849196570822' +
-            '26493991806038180742006204637761354248846321625124031637930839216416315647409595294193595958529411668489405859523' +
-            '37613333022396096584117954892216031229237302943701877588456738335398602461675225081791820393153757504952636234951' +
-            '32323782003654358104782690612092797248736680529211579223142368426126233039432475078545094258975175539015664775146' +
-            '07193514399690599495696153028090507215003302390050778898553239175099482557220816446894421272976054225797071426466' +
-            '60768825302832201908302295573257427896031830742328565032949',
+            "2534288944884041556497168959071347320689884775908477905258202659454602246385394058588521595116849196570822" +
+                "26493991806038180742006204637761354248846321625124031637930839216416315647409595294193595958529411668489405859523" +
+                "37613333022396096584117954892216031229237302943701877588456738335398602461675225081791820393153757504952636234951" +
+                "32323782003654358104782690612092797248736680529211579223142368426126233039432475078545094258975175539015664775146" +
+                "07193514399690599495696153028090507215003302390050778898553239175099482557220816446894421272976054225797071426466" +
+                "60768825302832201908302295573257427896031830742328565032949"
         ),
         e: 65537,
     },

+ 281 - 0
gramjs/events/CallbackQuery.ts

@@ -0,0 +1,281 @@
+import { EntityLike } from "../define";
+import { EventBuilder, EventCommonSender } from "./common";
+import { Api } from "../tl";
+import { toSignedLittleBuffer } from "../Helpers";
+import { TelegramClient } from "..";
+import { _getEntityPair, getInputPeer } from "../Utils";
+import { EditMessageParams, SendMessageParams } from "../client/messages";
+
+export interface NewCallbackQueryInterface {
+    chats: EntityLike[];
+    func?: { (event: CallbackQuery): boolean };
+    fromUsers: EntityLike[];
+    blacklistUsers: EntityLike[];
+    pattern?: RegExp;
+}
+
+export const NewCallbackQueryDefaults: NewCallbackQueryInterface = {
+    chats: [],
+    fromUsers: [],
+    blacklistUsers: [],
+};
+
+/**
+ * Occurs whenever you sign in as a bot and a user
+ * clicks one of the inline buttons on your messages.
+ * Note that the `chats` parameter will **not** work with normal
+ * IDs or peers if the clicked inline button comes from a "via bot"
+ * message. The `chats` parameter also supports checking against the
+ * `chat_instance` which should be used for inline callbacks.
+ *
+ * @example
+ * ```ts
+ * async function printQuery(event: NewCallbackQueryEvent) {
+ *     // TODO
+ * }
+ * ```
+ */
+export class CallbackQuery extends EventBuilder {
+    match?: RegExp;
+
+    private _noCheck: boolean;
+
+    constructor(inlineQueryParams: Partial<NewCallbackQueryInterface> = {}) {
+        const { chats, func, pattern } = inlineQueryParams;
+        super({ chats, func, blacklistChats: false });
+
+        this.match = pattern;
+
+        this._noCheck = [this.chats, this.func, this.match].every(
+            (i) => i === null || i === undefined
+        );
+    }
+
+    build(update: Api.TypeUpdate | Api.TypeUpdates, others: any = null) {
+        if (update instanceof Api.UpdateBotCallbackQuery) {
+            console.log("returning her!");
+            return new CallbackQueryEvent(update, update.peer, update.msgId);
+        } else if (update instanceof Api.UpdateInlineBotCallbackQuery) {
+            const b = toSignedLittleBuffer(update.msgId.id, 8);
+            const msgId = b.readInt32LE();
+            const peerId = b.readInt32LE(4);
+            const peer =
+                peerId < 0
+                    ? new Api.PeerChannel({ channelId: -peerId })
+                    : new Api.PeerUser({ userId: peerId });
+            return new CallbackQueryEvent(update, peer, msgId);
+        }
+    }
+
+    filter(event: CallbackQueryEvent) {
+        if (this._noCheck) return event;
+
+        if (this.chats) {
+            let inside = this.chats.includes(
+                event.query.chatInstance as unknown as EntityLike
+            );
+            if (event.chatId) {
+                inside = inside || this.chats.includes(event.chatId);
+            }
+
+            if (inside === this.blacklistChats) {
+                return;
+            }
+        }
+
+        if (this.match) {
+            const data = new TextDecoder().decode(event.query.data);
+            const result = this.match.exec(data);
+            if (result) {
+                event.patternMatch = result;
+            } else {
+                return;
+            }
+        }
+
+        if (this.func) {
+            return this.func(event);
+        }
+
+        return true;
+    }
+}
+
+export interface AnswerCallbackQueryParams {
+    message: string;
+    cacheTime: number;
+    url: string;
+    alert: boolean;
+}
+
+export class CallbackQueryEvent extends EventCommonSender {
+    /**
+     * The original {@link Api.UpdateBotCallbackQuery} or {@link Api.UpdateInlineBotCallbackQuery} object.
+     */
+    query: Api.UpdateBotCallbackQuery | Api.UpdateInlineBotCallbackQuery;
+
+    /**
+     * The regex match object returned from successfully matching the
+     * query `data` with the provided pattern in your event handler.
+     */
+    patternMatch: RegExpMatchArray | undefined;
+
+    private _message: Api.Message | undefined;
+    private _answered: boolean;
+
+    constructor(
+        query: Api.UpdateBotCallbackQuery | Api.UpdateInlineBotCallbackQuery,
+        peer: Api.TypePeer,
+        msgId: number
+    ) {
+        super({
+            msgId,
+            chatPeer: peer,
+            broadcast: false,
+        });
+        this.query = query;
+        this.patternMatch = undefined;
+        this._senderId = query.userId;
+        this._message = undefined;
+        this._answered = false;
+    }
+
+    _setClient(client: TelegramClient) {
+        super._setClient(client);
+        const [sender, inputSender] = _getEntityPair(
+            this._senderId!,
+            this._entities,
+            client._entityCache
+        );
+        this._sender = sender;
+        this._inputSender = inputSender;
+    }
+
+    get id() {
+        return this.query.queryId;
+    }
+
+    get messageId() {
+        return this._messageId!;
+    }
+
+    get data() {
+        return this.query.data;
+    }
+
+    get chatInstance() {
+        return this.query.chatInstance;
+    }
+
+    async getMessage() {
+        if (this._message) {
+            return this._message;
+        }
+
+        const chat = this.isChannel ? await this.getInputChat() : undefined;
+        if (!chat) return;
+
+        const messages = await this._client!.getMessages(chat, {
+            ids: this._messageId,
+        });
+        this._message = messages[0];
+
+        return this._message;
+    }
+
+    async _refetchSender() {
+        if (this._entities.has(this._senderId!)) {
+            this._sender = this._entities.get(this._senderId!);
+        }
+
+        if (!this._sender) return;
+
+        this._inputSender = getInputPeer(this._chat);
+        if (!this._inputSender.hasOwnProperty("accessHash")) {
+            try {
+                this._inputSender = this._client!._entityCache.get(
+                    this._senderId
+                );
+            } catch (e) {
+                const m = await this.getMessage();
+                if (m) {
+                    this._sender = m._sender;
+                    this._inputSender = m._inputSender;
+                }
+            }
+        }
+    }
+
+    async answer({
+        message,
+        cacheTime,
+        url,
+        alert,
+    }: Partial<AnswerCallbackQueryParams> = {}) {
+        if (this._answered) return;
+
+        return await this._client!.invoke(
+            new Api.messages.SetBotCallbackAnswer({
+                queryId: this.query.queryId,
+                cacheTime,
+                alert,
+                message,
+                url,
+            })
+        ).then((res) => {
+            this._answered = true;
+            return res;
+        });
+    }
+
+    get viaInline() {
+        return this.query instanceof Api.UpdateInlineBotCallbackQuery;
+    }
+
+    async respond(params: SendMessageParams = {}) {
+        await this.answer();
+        const inputChat = await this.getInputChat();
+        await this._client!.sendMessage(inputChat!, params);
+    }
+
+    async reply(params: SendMessageParams = {}) {
+        await this.answer();
+        params.replyTo = this.messageId;
+        const inputChat = await this.getInputChat();
+        await this._client!.sendMessage(inputChat!, params);
+    }
+
+    async edit(params: EditMessageParams) {
+        if ((this.query.msgId as any) instanceof Api.InputBotInlineMessageID) {
+            return await this._client!.editMessage(this.messageId, params).then(
+                async (res) => {
+                    await this.answer();
+                    return res;
+                }
+            );
+        } else {
+            const inputChat = await this.getInputChat();
+
+            return await this._client!.editMessage(inputChat!, params).then(
+                async (res) => {
+                    await this.answer();
+                    return res;
+                }
+            );
+        }
+    }
+
+    async delete({ revoke } = { revoke: false }) {
+        if (this._client) {
+            return this._client.deleteMessages(
+                await this.getInputChat(),
+                [this.messageId as any],
+                { revoke }
+            );
+        }
+    }
+
+    get sender() {
+        return this._sender;
+    }
+}

+ 27 - 0
gramjs/events/common.ts

@@ -5,6 +5,7 @@ import type { TelegramClient } from "..";
 
 import { isArrayLike } from "../Helpers";
 import { utils } from "../";
+import { SenderGetter } from "../tl/custom/senderGetter";
 
 /** @hidden */
 export async function _intoIdSet(
@@ -173,3 +174,29 @@ export class EventCommon extends ChatGetter {
         return this._client;
     }
 }
+export class EventCommonSender extends SenderGetter {
+    _eventName = "Event";
+    _entities: Map<number, Entity>;
+    _messageId?: number;
+
+    constructor({
+        chatPeer = undefined,
+        msgId = undefined,
+        broadcast = undefined,
+    }: EventCommonInterface) {
+        super();
+        ChatGetter.initChatClass(this, { chatPeer, broadcast });
+        SenderGetter.initChatClass(this, { chatPeer, broadcast });
+        this._entities = new Map();
+        this._client = undefined;
+        this._messageId = msgId;
+    }
+
+    _setClient(client: TelegramClient) {
+        this._client = client;
+    }
+
+    get client() {
+        return this._client;
+    }
+}

+ 1 - 1
gramjs/network/MTProtoSender.ts

@@ -403,7 +403,7 @@ export class MTProtoSender {
                 );
                 this._sendQueue.append(ack);
                 this._lastAcks.push(ack);
-                if (this._lastAcks.length >= 10){
+                if (this._lastAcks.length >= 10) {
                     this._lastAcks.shift();
                 }
                 this._pendingAck.clear();

+ 1 - 1
gramjs/tl/custom/chatGetter.ts

@@ -64,7 +64,7 @@ export class ChatGetter {
     }
 
     async getInputChat() {
-        if (!this._inputChat && this.chatId && this._client) {
+        if (!this.inputChat && this.chatId && this._client) {
             try {
                 const target = this.chatId;
                 for await (const dialog of this._client.iterDialogs({

+ 2 - 2
package-lock.json

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

+ 1 - 1
package.json

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