GramJS

messages.SendEncrypted

Sends a text message to a secret chat.

Example

const { Api, TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");

const session = new StringSession(""); // You should put your string session here
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
  await client.connect(); // This assumes you have already authenticated with .start()

  const result = await client.invoke(
    new Api.messages.SendEncrypted({
      peer: new Api.InputEncryptedChat({
        chatId: 43,
        accessHash: BigInt("-4156887774564"),
      }),
      randomId: BigInt("-4156887774564"),
      data: Buffer.from("arbitrary data here"),
    })
  );
  console.log(result); // prints the result
})();
import { Api, TelegramClient } from "telegram";
import { StringSession } from "telegram/sessions";

const session = new StringSession(""); // You should put your string session here
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
  await client.connect(); // This assumes you have already authenticated with .start()

  const result: Api.messages.SentEncryptedMessage = await client.invoke(
    new Api.messages.SendEncrypted({
      peer: new Api.InputEncryptedChat({
        chatId: 43,
        accessHash: BigInt("-4156887774564"),
      }),
      randomId: BigInt("-4156887774564"),
      data: Buffer.from("arbitrary data here"),
    })
  );
  console.log(result); // prints the result
})();

Parameters

NameTypeDescription
flags#Flags, see TL conditional fields
silentflags.0?trueSend encrypted message without a notification
peerInputEncryptedChatSecret chat ID
randomIdlongUnique client message ID, necessary to avoid message resending
databytesTL-serialization of DecryptedMessage type, encrypted with a key that was created during chat initialization

Result

messages.SentEncryptedMessage

Possible errors

CodeTypeDescription
400CHAT_ID_INVALIDThe provided chat id is invalid.
400DATA_INVALIDEncrypted data invalid.
400DATA_TOO_LONGData too long.
400ENCRYPTION_DECLINEDThe secret chat was declined.
400MSG_WAIT_FAILEDA waiting call returned an error.
403USER_IS_BLOCKEDYou were blocked by this user.

Can bots use this method?

No

DecryptedMessage

Object describes the contents of an encrypted message.