Coded file name Telegram bot upload file

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);
                // "Report",).SendPhoto("@channel Name or chat_id", fts, "My Text");
            }

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

// Create two different encodings.
        Encoding utf8 = Encoding.UTF8;
        Encoding unicode = Encoding.Unicode;

        // Convert the string into a byte array.
        byte[] unicodeBytes = unicode.GetBytes(fileName);

        // Perform the conversion from one encoding to the other.
        byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);

        // Convert the new byte[] into a char[] and then into a string.
        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;

Result File Name

+4
source share

Source: https://habr.com/ru/post/1668508/


All Articles