1

I'm on a channel that sends messages that contains a text, with a link (that link has not an image) and an image (tip product from amazon):

enter image description here

I tried with this code and it's similar:

bot.telegram.sendMessage('mychannel', `Hello https:/path/to/image.jpg`)

And it works it similar, but it remains the link. SO how can i put that way with image preview but not the link?

Thank you

Mattia
  • 199
  • 1
  • 1
  • 12

2 Answers2

1

One workaround(trick) would be to insert the link but use an empty character unicode (like from https://emptycharacter.com/)

Here is an example (I inserted the photo URL with an empty character) enter image description here

some sample code to get you started:

const Telegraf = require("telegraf");

const bot = new Telegraf(" ... ");

const CHAT_ID = ... ;

function getHiddenLink(url, parse_mode = "markdown") {
  const emptyChar = "‎"; // copied and pasted the char from https://emptycharacter.com/

  switch (parse_mode) {
    case "markdown":
      return `[${emptyChar}](${url})`;
    case "HTML":
      return `<a href="${url}">${emptyChar}</a>`;
    default:
      throw new Error("invalid parse_mode");
  }
}


// Option 1: sending with MARKDOWN syntax
bot.telegram.sendMessage(
  CHAT_ID,
  `
some test text in markdown
${getHiddenLink("https://i.stack.imgur.com/TPKR5.png", "markdown")}
`,
  {
    parse_mode: "markdown",
  }
);


// Another option: sending with HTML syntax
bot.telegram.sendMessage(
  CHAT_ID,
  `
some test text in HTML
${getHiddenLink("https://i.stack.imgur.com/TPKR5.png", "HTML")}
`,
  {
    parse_mode: "HTML",
  }
);

Here we just create a new function getHiddenLink() that accepts the URL and parse_mode (HTML or markdown) and just craft a new URL representation with the empty character as the link-text and return it.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • You put the character before or after the link? Or you changed one link character? – Mattia Sep 11 '20 at 18:09
  • @Mattia I've added a sample code and an explanation on how-to.. take a look please – Tibebes. M Sep 11 '20 at 18:51
  • Ok thank you i understand almost all, only a question. the parse_mode passed on the second parameter okay, but the other parameter on .sendMessage()? What's that? The real parse of the text? – Mattia Sep 11 '20 at 19:59
  • 1
    the last argument on `sendMessage()` is actually how we pass paramaters to the bot API itself. (we are telling the bot-api that we are sending a string that is formatted in either MD or HTML when we pass a JSON object with `parse_mode` key set.) – Tibebes. M Sep 11 '20 at 20:03
  • a curiosity, i can do it also with a local image? I've putted the path to the image instead of the link but it doesn't work – Mattia Sep 14 '20 at 00:49
  • No, that wouldn't work. The picture has to be uploaded to some server first (this is just a link-preview afterall) – Tibebes. M Sep 14 '20 at 05:07
0

There is no way to send a preview without a link. You can try to have some by your own telegram account and you will see it is impossible. Instead of preview, add some caption to photos.

Amir
  • 36
  • 3
  • Yes i know with caption, but using sendPhoto and seand directly it cover all the message. SO i would to create something like that, with a margin to the message frame limit – Mattia Sep 11 '20 at 18:38