How to Send a Message with an Image Attachment in a C# Telegram Bot

I’m trying to send a message with an image attached through my C# telegram bot. Here’s an example of what I’m aiming to achieve:

I have the following code, but it’s not sending the image properly:

// var filePath = "<b>Hello</b>\n"
// + "<a href=\"Image\\image.jpg\">&#8203;</a>";
string filePath = @"D:\image.jpg";

bot.SendTextMessageAsync(chatId, "<a href='" + filePath + "'>My Image</a>", ParseMode.Html, false);

Please help me understand how to make it work!

You’re treating a local file path like a URL. That won’t work with HTML links in Telegram messages since the bot can’t access files on your machine. If you need to send it as an attachment instead of displaying the image inline, use SendDocumentAsync:

string filePath = @"D:\image.jpg";
using var stream = System.IO.File.OpenRead(filePath);
var document = new InputOnlineFile(stream, "image.jpg");
await bot.SendDocumentAsync(chatId, document, caption: "<b>Hello</b>");

This sends the file as a downloadable attachment. Documents keep original quality and show as file attachments rather than compressed images. Really useful for screenshots or images where you need the original resolution.

You’re using SendTextMessageAsync - that’s for text only. For images, use SendPhotoAsync instead:

string filePath = @"D:\image.jpg";
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    var inputFile = new InputOnlineFile(fileStream, "image.jpg");
    await bot.SendPhotoAsync(chatId, inputFile, caption: "<b>My Image</b>", parseMode: ParseMode.Html);
}

Make sure you’ve got the file management using directive and double-check your file path. I’ve used this approach in production bots - works like a charm.

first, make sure your file exists with File.Exists(filePath) before sending. telegram compresses photos with SendPhotoAsync - if that’s a prob, use SendDocumentAsync like olivia said. also, remember, your code’s trying to send html links, but telegram can’t access local file paths.