GramJS

messages.SendMessage

Sends a message to a 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.SendMessage({
      peer: "username",
      message: "Hello there!",
      randomId: BigInt("-4156887774564"),
      noWebpage: true,
      scheduleDate: 43,
    })
  );
  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.Updates = await client.invoke(
    new Api.messages.SendMessage({
      peer: "username",
      message: "Hello there!",
      randomId: BigInt("-4156887774564"),
      noWebpage: true,
      scheduleDate: 43,
    })
  );
  console.log(result); // prints the result
})();

Parameters

NameTypeDescription
flags#Flags, see TL conditional fields
noWebpageflags.1?trueSet this flag to disable generation of the webpage preview
silentflags.5?trueSend this message silently (no notifications for the receivers)
backgroundflags.6?trueSend this message as background message
clearDraftflags.7?trueClear the draft field
peerInputPeerThe destination where the message will be sent
replyToMsgIdflags.0?intThe message ID to which this message will reply to
messagestringThe message
randomIdlongUnique client message ID required to prevent message resending
replyMarkupflags.2?ReplyMarkupReply markup for sending bot buttons
entitiesflags.3?Vector<MessageEntity>Message entities for sending styled text
scheduleDateflags.10?intScheduled message date for scheduled messages

Result

Updates

Possible errors

CodeTypeDescription
401AUTH_KEY_PERM_EMPTYThe temporary auth key must be binded to the permanent auth key to use these methods.
400BOT_DOMAIN_INVALIDBot domain invalid
400BOT_INVALIDThis is not a valid bot
400BUTTON_DATA_INVALIDThe data of one or more of the buttons you provided is invalid
400BUTTON_TYPE_INVALIDThe type of one or more of the buttons you provided is invalid
400BUTTON_URL_INVALIDButton URL invalid
400CHANNEL_INVALIDThe provided channel is invalid
400CHANNEL_PRIVATEYou haven't joined this channel/supergroup
400CHAT_ADMIN_REQUIREDYou must be an admin in this chat to do this
400CHAT_ID_INVALIDThe provided chat id is invalid
400CHAT_RESTRICTEDYou can't send messages in this chat, you were restricted
403CHAT_WRITE_FORBIDDENYou can't write in this chat
400ENCRYPTION_DECLINEDThe secret chat was declined
400ENTITY_MENTION_USER_INVALIDYou mentioned an invalid user
400FROM_MESSAGE_BOT_DISABLEDBots can't use fromMessage min constructors
400INPUT_USER_DEACTIVATEDThe specified user was deleted
400MESSAGE_EMPTYThe provided message is empty
400MESSAGE_TOO_LONGThe provided message is too long
400MSG_ID_INVALIDProvided reply_to_msg_id is invalid
400PEER_ID_INVALIDThe provided peer id is invalid
400PINNED_DIALOGS_TOO_MUCHToo many pinned dialogs
400REPLY_MARKUP_INVALIDThe provided reply markup is invalid
400SCHEDULE_BOT_NOT_ALLOWEDBots cannot schedule messages
400SCHEDULE_DATE_TOO_LATEYou can't schedule a message this far in the future
400SCHEDULE_TOO_MUCHThere are too many scheduled messages
420SLOWMODE_WAIT_XSlowmode is enabled in this chat: you must wait for the specified number of seconds before sending another message to the chat.
400USER_BANNED_IN_CHANNELYou're banned from sending messages in supergroups/channels
400USER_IS_BLOCKEDYou were blocked by this user
400USER_IS_BOTBots can't send messages to other bots
400YOU_BLOCKED_USERYou blocked this user

Can bots use this method?

Yes

Styled text with message entities

How to create styled text with message entities

Scheduled messages

Telegram allows scheduling messages