tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

I warn to use SendMessageAlbum send photo and Video and set caption,but can't success #2763

Closed Jox2y1MOQAu1NQ closed 8 months ago

Jox2y1MOQAu1NQ commented 9 months ago
    private void SendMessageAlbum(long chatId) 
    {
        var messageContents = new List<Telegram.Td.Api.InputMessageContent>();
        foreach (string item in listBox1.Items)
        {
            if (item.Equals(string.Empty) || !System.IO.File.Exists(item))
                continue;
            string[] strFilePath = item.Split('\\');
            string[] strFileName = strFilePath[strFilePath.Length - 1].Split('.');
            string strFileType = strFileName[strFileName.Length - 1].ToLower();
            if (strFileType.Contains("jpg"))
            {
                messageContents.Add(new Telegram.Td.Api.InputMessagePhoto()
                {
                    Photo = new Telegram.Td.Api.InputFileLocal(item),
                    //use this did not achieve the effect
                    Caption = new Telegram.Td.Api.FormattedText(textBox1.Text, null)
                });
            }
            else if (strFileType.Contains("mp4"))
            {
                messageContents.Add(new Telegram.Td.Api.InputMessageVideo()
                {
                    Video = new Telegram.Td.Api.InputFileLocal(item),
                    Caption = new Telegram.Td.Api.FormattedText(textBox1.Text, null)
                });
            }
        }
        if (!textBox1.Text.Equals(string.Empty))
        {
        //use this callback 400 result
        //error {
        //  code = 400
        //  message = "Invalid message content type"
        // }
        messageContents.Add(new Telegram.Td.Api.InputMessageText()
            {
                Text = new Telegram.Td.Api.FormattedText()
                {
                    Text = textBox1.Text,
                }
            });
        }
        var msg = new Telegram.Td.Api.SendMessageAlbum(
                chatId : chatId,
                messageThreadId:0,
                replyTo: null, 
                options: new Telegram.Td.Api.MessageSendOptions(),
                messageContents.ToArray()
            );
        TelegramSend(msg, TelegramSendRequestResult);
    }
levlam commented 9 months ago

You can't use InputMessageText in albums. Album caption is a caption of any message in the album if there is only one message with caption, so you need to add it only to one of the messages.