GramJS

messages.SendMedia

Send a media

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.SendMedia({
      peer: "username",
      media: new Api.InputMediaUploadedPhoto({
        file: await client.uploadFile({
          file: new CustomFile(
            "file.bin",
            fs.statSync("../file.bin").size,
            "../file.bin"
          ),
          workers: 1,
        }),
        stickers: [
          new Api.InputDocument({
            id: BigInt("-4156887774564"),
            accessHash: BigInt("-4156887774564"),
            fileReference: Buffer.from("arbitrary data here"),
          }),
        ],
        ttlSeconds: 43,
      }),
      message: "Hello there!",
      randomId: BigInt("-4156887774564"),
      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.SendMedia({
      peer: "username",
      media: new Api.InputMediaUploadedPhoto({
        file: await client.uploadFile({
          file: new CustomFile(
            "file.bin",
            fs.statSync("../file.bin").size,
            "../file.bin"
          ),
          workers: 1,
        }),
        stickers: [
          new Api.InputDocument({
            id: BigInt("-4156887774564"),
            accessHash: BigInt("-4156887774564"),
            fileReference: Buffer.from("arbitrary data here"),
          }),
        ],
        ttlSeconds: 43,
      }),
      message: "Hello there!",
      randomId: BigInt("-4156887774564"),
      scheduleDate: 43,
    })
  );
  console.log(result); // prints the result
})();

Parameters

NameTypeDescription
flags#Flags, see TL conditional fields
silentflags.5?trueSend message silently (no notification should be triggered)
backgroundflags.6?trueSend message in background
clearDraftflags.7?trueClear the draft
peerInputPeerDestination
replyToMsgIdflags.0?intMessage ID to which this message should reply to
mediaInputMediaAttached media
messagestringCaption
randomIdlongRandom ID to avoid resending the same message
replyMarkupflags.2?ReplyMarkupReply markup for bot keyboards
entitiesflags.3?Vector<MessageEntity>Message entities for styled text
scheduleDateflags.10?intScheduled message date for scheduled messages

Result

Updates

Possible errors

CodeTypeDescription
400BROADCAST_PUBLIC_VOTERS_FORBIDDENYou can't forward polls with public voters
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_RESTRICTEDYou can't send messages in this chat, you were restricted
403CHAT_SEND_GIFS_FORBIDDENYou can't send gifs in this chat
403CHAT_SEND_MEDIA_FORBIDDENYou can't send media in this chat
403CHAT_SEND_STICKERS_FORBIDDENYou can't send stickers in this chat.
403CHAT_WRITE_FORBIDDENYou can't write in this chat
400EXTERNAL_URL_INVALIDExternal URL invalid
400FILE_PARTS_INVALIDThe number of file parts is invalid
400FILE_PART_LENGTH_INVALIDThe length of a file part is invalid
400IMAGE_PROCESS_FAILEDFailure while processing image
400INPUT_USER_DEACTIVATEDThe specified user was deleted
400MD5_CHECKSUM_INVALIDThe MD5 checksums do not match
400MEDIA_CAPTION_TOO_LONGThe caption is too long
400MEDIA_EMPTYThe provided media object is invalid
400MEDIA_INVALIDMedia invalid
400MSG_ID_INVALIDInvalid message ID provided
400PEER_ID_INVALIDThe provided peer id is invalid
400PHOTO_EXT_INVALIDThe extension of the photo is invalid
400PHOTO_INVALID_DIMENSIONSThe photo dimensions are invalid
400PHOTO_SAVE_FILE_INVALIDInternal issues, try again later
400POLL_ANSWERS_INVALIDInvalid poll answers were provided
400POLL_OPTION_DUPLICATEDuplicate poll options provided
400POLL_OPTION_INVALIDInvalid poll option provided
400QUIZ_CORRECT_ANSWERS_EMPTYNo correct quiz answer was specified
400QUIZ_CORRECT_ANSWER_INVALIDAn invalid value was provided to the correct_answers field
400REPLY_MARKUP_BUY_EMPTYReply markup for buy button empty
400REPLY_MARKUP_INVALIDThe provided reply markup is invalid
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.
400TTL_MEDIA_INVALIDInvalid media Time To Live was provided
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
400WEBPAGE_CURL_FAILEDFailure while fetching the webpage with cURL
400WEBPAGE_MEDIA_EMPTYWebpage media empty
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