I create a bot (@mp3lyric_bot_test) in telegram and set it as administrator in my channel (@mp3lyric_test). Now I want to make a request to send an mp3 to channel using telegram api.
my mp3 is in web, something like this: http://bayanbox.ir/download/7028444634071302239/Sound-1.mp3
At first i download mp3 with this method:
public static Task<byte[]> DownloadAsync(string requestUriSt)
{
var requestUri = new Uri(requestUriSt);
byte[] fileBytes;
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
using (var responseMessage = await httpClient.SendAsync(request))
{
fileBytes = await responseMessage.Content.ReadAsByteArrayAsync();
var audioString = Encoding.UTF8.GetString(fileBytes, 0, fileBytes.Length);
}
}
}
return fileBytes;
}
- if there is a way to send mp3 directly without download, please tell me how? thanks.
Then send that byte array (fileBytes) using this code:
-
my bot token is 247655935:AAEhpYCeoXA5y7V8Z3WrVcNJ3AaChORjfvw
using (var client = new HttpClient()) { var uri = new Uri("https://api.telegram.org/bot247655935:AAEhpYCeoXA5y7V8Z3WrVcNJ3AaChORjfvw/sendAudio"); using (var multipartFormDataContent = new MultipartFormDataContent( "SendAudio----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { multipartFormDataContent.Add( new StringContent("@mp3lyric_test"), string.Format(""{0}"", "chat_id") ); multipartFormDataContent.Add( new StreamContent(new MemoryStream(fileBytes)), '"' + "audio" + '"' ); using (var message = await client.PostAsync(uri, multipartFormDataContent)) { var contentString = await message.Content.ReadAsStringAsync(); } } }
I have two error:
-
“Request Entity Too Large” when my mp3 is about 6mb or 7mb or … (not using http://bayanbox.ir/download/7028444634071302239/Sound-1.mp3)
-
error_code:400, description:”Bad Request: URL must be in UTF-8″ (after using that mp3 for test that is 28kb)
2
Answers
I chaged my codes for send the byte array (fileBytes) and now it works:
To send a new AudioFile you use the SendAudio method but with the InputFile field.
First create an
InputFile
object, then pass those bytes in theaudio
parameter of theSendAudio
methodIf you need to resend the same
AudioFile
to another user, then you can use theString
option as theaudio
parameter in theSendAudio