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,
      noforwards: true,
      scheduleDate: 43,
      sendAs: "username",
    })
  );
  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,
      noforwards: true,
      scheduleDate: 43,
      sendAs: "username",
    })
  );
  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
noforwardsflags.14?trueOnly for bots, disallows forwarding and saving of the messages, even if the destination chat doesn't have content protection enabled
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
sendAsflags.13?InputPeerSend this message as the specified peer

Result

Updates

Possible errors

CodeTypeDescription
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.
400ENTITIES_TOO_LONGYou provided too many styled message entities.
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_INVALIDInvalid message ID provided.
400PEER_ID_INVALIDThe provided peer id is invalid.
400PINNED_DIALOGS_TOO_MUCHToo many pinned dialogs.
400POLL_OPTION_INVALIDInvalid poll option provided.
500RANDOM_ID_DUPLICATEYou provided a random ID that was already used.
400REPLY_MARKUP_INVALIDThe provided reply markup is invalid.
400REPLY_MARKUP_TOO_LONGThe specified reply_markup is too long.
400SCHEDULE_BOT_NOT_ALLOWEDBots cannot schedule messages.
400SCHEDULE_DATE_TOO_LATEYou can't schedule a message this far in the future.
400SCHEDULE_STATUS_PRIVATECan't schedule until user is online, if the user's last seen timestamp is hidden by their privacy settings.
400SCHEDULE_TOO_MUCHThere are too many scheduled messages.
400SEND_AS_PEER_INVALIDYou can't send messages as the specified peer.
420SLOWMODE_WAIT_%dSlowmode is enabled in this chat: wait %d seconds before sending another message to this chat.
400USER_BANNED_IN_CHANNELYou're banned from sending messages in supergroups/channels.
403USER_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