I’m working with a Telegram bot that needs to send documents to users. The bot sends files successfully but there’s an issue with filenames that contain Cyrillic characters. When the file reaches the recipient, the filename appears as encoded text instead of the original name.
string apiUrl = "https://api.telegram.org/bot" + botToken + "/sendDocument";
string documentPath = @"D:\Мой документ.docx";
string originalName = documentPath.Split('\\').Last();
var telegramBot = new Telegram.Bot.Api("YOUR_BOT_TOKEN");
using (var fileStream = System.IO.File.Open(documentPath, FileMode.Open))
{
FileToSend document = new FileToSend();
document.Content = fileStream;
document.Filename = documentPath.Split('\\').Last();
var result = await telegramBot.SendDocumentAsync(chatId, document, messageCaption);
}
I suspect the filename gets encoded with Base64 and UTF-8 somewhere in the process. I tried converting the filename to UTF-8 encoding manually:
Encoding utf8Encoding = Encoding.UTF8;
Encoding unicodeEncoding = Encoding.Unicode;
byte[] unicodeByteArray = unicodeEncoding.GetBytes(originalName);
byte[] utf8ByteArray = Encoding.Convert(unicodeEncoding, utf8Encoding, unicodeByteArray);
char[] utf8CharArray = new char[utf8Encoding.GetCharCount(utf8ByteArray, 0, utf8ByteArray.Length)];
utf8Encoding.GetChars(utf8ByteArray, 0, utf8ByteArray.Length, utf8CharArray, 0);
string convertedName = new string(utf8CharArray);
originalName = convertedName;
This approach didn’t solve the problem. The filename still appears garbled on the client side. How can I properly handle Unicode filenames so they display correctly when users receive the documents through the Telegram bot?