I am trying to upload a file using the telegram bot api. The file is sent, but the recipient sees the encoded file name if it contains Russian letters. As I found out, the file name is encoded by Base64 and Utf-8
string url = "https://api.telegram.org/bot" + _keyBotTelegram + "/sendDocument";
string ttt = @"K:\ .txt";
string fileName = ttt.Split('\\').Last();
var bot = new Telegram.Bot.Api("129368276:AAEW-Q7Jv8qWaFvwYk2iD4mXZyt9Q");
using (var stream = System.IO.File.Open(pathToDocument, FileMode.Open))
{
FileToSend fts = new FileToSend();
fts.Content = stream;
fts.Filename = pathToDocument.Split('\\').Last();
var test = await bot.SendDocumentAsync(ChatID, fts, caption);
}
File name
How to send unicode files to the client to save the full file name, including file extension
Tried to encode the file name in Utf8
Encoding utf8 = Encoding.UTF8;
Encoding unicode = Encoding.Unicode;
byte[] unicodeBytes = unicode.GetBytes(fileName);
byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);
fileName = utf8String;

source
share