GramJS

messages.SendEncryptedService

Sends a service 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.SendEncryptedService({
      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.SendEncryptedService({
      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
peerInputEncryptedChatSecret chat ID
randomIdlongUnique client message ID required to prevent message resending
databytesTL-serialization of DecryptedMessage type, encrypted with a key generated during chat initialization

Result

messages.SentEncryptedMessage

Possible errors

CodeTypeDescription
400DATA_INVALIDEncrypted data invalid.
400ENCRYPTION_DECLINEDThe secret chat was declined.
400ENCRYPTION_ID_INVALIDThe provided secret chat ID is invalid.
400MSG_WAIT_FAILEDA waiting call returned an error.
403USER_DELETEDYou can't send this secret message because the other participant deleted their account.
403USER_IS_BLOCKEDYou were blocked by this user.

Can bots use this method?

No

DecryptedMessage

Object describes the contents of an encrypted message.