Browse Source

Add commentTo functionality to sendMessage/sendFile/sendAlbum

painor 3 years ago
parent
commit
5bf80ab4ac
7 changed files with 73 additions and 7 deletions
  1. 1 1
      gramjs/Version.ts
  2. 43 0
      gramjs/client/messages.ts
  3. 23 2
      gramjs/client/uploads.ts
  4. 1 1
      gramjs/client/users.ts
  5. 2 2
      package-lock.json
  6. 1 1
      package.json
  7. 2 0
      publish_npm.js

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.5.1";
+export const version = "2.5.3";

+ 43 - 0
gramjs/client/messages.ts

@@ -525,6 +525,12 @@ export interface SendMessageParams {
     /** If set, the message won't send immediately, and instead it will be scheduled to be automatically sent at a later time. */
     schedule?: DateLike;
     noforwards?: boolean;
+    /** Similar to ``replyTo``, but replies in the linked group of a broadcast channel instead (effectively leaving a "comment to" the specified message).
+
+     This parameter takes precedence over ``replyTo``.
+     If there is no linked chat, `SG_ID_INVALID` is thrown.
+     */
+    commentTo?: number | Api.Message;
 }
 
 /** interface used for forwarding messages */
@@ -710,6 +716,7 @@ export async function sendMessage(
         supportStreaming,
         schedule,
         noforwards,
+        commentTo,
     }: SendMessageParams = {}
 ) {
     if (file) {
@@ -732,9 +739,16 @@ export async function sendMessage(
             scheduleDate: schedule,
             buttons: buttons,
             noforwards: noforwards,
+            commentTo: commentTo,
         });
     }
     entity = await client.getInputEntity(entity);
+    if (commentTo != undefined) {
+        console.log("this is a comment");
+        const discussionData = await getCommentData(client, entity, commentTo);
+        entity = discussionData.entity;
+        replyTo = discussionData.replyTo;
+    }
     let markup, request;
     if (message && message instanceof Api.Message) {
         if (buttons == undefined) {
@@ -1133,4 +1147,33 @@ export async function markAsRead(
     }
 }
 
+/** @hidden */
+export async function getCommentData(
+    client: TelegramClient,
+    entity: EntityLike,
+    message: number | Api.Message
+) {
+    const result = await client.invoke(
+        new Api.messages.GetDiscussionMessage({
+            peer: entity,
+            msgId: utils.getMessageId(message),
+        })
+    );
+    const relevantMessage = result.messages[0];
+    let chat;
+    for (const c of result.chats) {
+        if (
+            relevantMessage.peerId instanceof Api.PeerChannel &&
+            c.id.eq(relevantMessage.peerId.channelId)
+        ) {
+            chat = c;
+            break;
+        }
+    }
+    return {
+        entity: utils.getInputPeer(chat),
+        replyTo: relevantMessage.id,
+    };
+}
+
 // TODO do the rest

+ 23 - 2
gramjs/client/uploads.ts

@@ -8,6 +8,7 @@ import path from "path";
 import { promises as fs } from "fs";
 import { errors, utils } from "../index";
 import { _parseMessageText } from "./messageParse";
+import { getCommentData } from "./messages";
 
 interface OnProgress {
     // Float between 0 and 1.
@@ -226,6 +227,12 @@ export interface SendFileInterface {
     /** How many workers to use to upload the file. anything above 16 is unstable. */
     workers?: number;
     noforwards?: boolean;
+    /** Similar to ``replyTo``, but replies in the linked group of a broadcast channel instead (effectively leaving a "comment to" the specified message).
+
+     This parameter takes precedence over ``replyTo``.
+     If there is no linked chat, `SG_ID_INVALID` is thrown.
+     */
+    commentTo?: number | Api.Message;
 }
 
 interface FileToMediaInterface {
@@ -451,6 +458,7 @@ export async function _sendAlbum(
         scheduleDate,
         workers = 1,
         noforwards,
+        commentTo,
     }: SendFileInterface
 ) {
     entity = await client.getInputEntity(entity);
@@ -470,7 +478,13 @@ export async function _sendAlbum(
     for (const c of caption) {
         captions.push(await _parseMessageText(client, c, parseMode));
     }
-    replyTo = utils.getMessageId(replyTo);
+    if (commentTo != undefined) {
+        const discussionData = await getCommentData(client, entity, commentTo);
+        entity = discussionData.entity;
+        replyTo = discussionData.replyTo;
+    } else {
+        replyTo = utils.getMessageId(replyTo);
+    }
     const albumFiles = [];
     for (const file of files) {
         let { fileHandle, media, image } = await _fileToMedia(client, {
@@ -561,6 +575,7 @@ export async function sendFile(
         scheduleDate,
         workers = 1,
         noforwards,
+        commentTo,
     }: SendFileInterface
 ) {
     if (!file) {
@@ -570,7 +585,13 @@ export async function sendFile(
         caption = "";
     }
     entity = await client.getInputEntity(entity);
-    replyTo = utils.getMessageId(replyTo);
+    if (commentTo != undefined) {
+        const discussionData = await getCommentData(client, entity, commentTo);
+        entity = discussionData.entity;
+        replyTo = discussionData.replyTo;
+    } else {
+        replyTo = utils.getMessageId(replyTo);
+    }
     if (Array.isArray(file)) {
         return await _sendAlbum(client, entity, {
             file: file,

+ 1 - 1
gramjs/client/users.ts

@@ -61,7 +61,7 @@ export async function invoke<R extends Api.AnyRequest>(
             ) {
                 if (e.seconds <= client.floodSleepThreshold) {
                     client._log.info(
-                        `Sleeping for ${e.seconds}s on flood wait`
+                        `Sleeping for ${e.seconds}s on flood wait (Caused by ${request.className})`
                     );
                     await sleep(e.seconds * 1000);
                 } else {

+ 2 - 2
package-lock.json

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

+ 1 - 1
package.json

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

+ 2 - 0
publish_npm.js

@@ -1,5 +1,7 @@
 const { exec } = require("child_process");
 const fs = require("fs");
+fs.rmSync("dist", { recursive: true, force: true });
+
 const tsc = exec("tsc");
 tsc.on("close", (code) => {
   if (code === 0) {