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

Result

Updates

Possible errors

CodeTypeDescription
400BOT_PAYMENTS_DISABLEDPlease enable bot payments in botfather before calling this method.
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_FORWARDS_RESTRICTEDYou can't forward messages from a protected chat.
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_POLL_FORBIDDENYou can't send polls in this chat.
403CHAT_SEND_STICKERS_FORBIDDENYou can't send stickers in this chat.
403CHAT_WRITE_FORBIDDENYou can't write in this chat.
400CURRENCY_TOTAL_AMOUNT_INVALIDThe total amount of all prices is invalid.
400EMOTICON_INVALIDThe specified emoji is invalid.
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.
400FILE_REFERENCE_EMPTYAn empty file reference was specified.
400FILE_REFERENCE_EXPIREDFile reference expired, it must be refetched as described in the documentation.
400GAME_BOT_INVALIDBots can't send another bot's game.
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.
400PAYMENT_PROVIDER_INVALIDThe specified payment provider is invalid.
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_ANSWER_INVALIDOne of the poll answers is not acceptable.
400POLL_OPTION_DUPLICATEDuplicate poll options provided.
400POLL_OPTION_INVALIDInvalid poll option provided.
400POLL_QUESTION_INVALIDOne of the poll questions is not acceptable.
400QUIZ_CORRECT_ANSWERS_EMPTYNo correct quiz answer was specified.
400QUIZ_CORRECT_ANSWERS_TOO_MUCHYou specified too many correct answers in a quiz, quizzes can only have one right answer!
400QUIZ_CORRECT_ANSWER_INVALIDAn invalid value was provided to the correct_answers field.
400QUIZ_MULTIPLE_INVALIDQuizzes can't have the multiple_choice flag set!
500RANDOM_ID_DUPLICATEYou provided a random ID that was already used.
400REPLY_MARKUP_BUY_EMPTYReply markup for buy button empty.
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.
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.
400TTL_MEDIA_INVALIDInvalid media Time To Live was provided.
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.
400VIDEO_CONTENT_TYPE_INVALIDThe video's content type is invalid.
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

File references

How to handle file references.