Audio and Voice Messages

audio tests

These two types of messages are pretty similar. Audio is MP3-encoded file that can be played in music player. A voice file has OGG format and is not shown in music player.


send audio method

This is the code to send an MP3 soundtrack. You might be wondering why some parameters are commented out? That's because this MP3 file has metadata on it and Telegram does a good job at reading it.

Message message = await botClient.SendAudioAsync(
    chatId: chatId,
    audio: InputFile.FromUri(""),
    performer: "Joel Thomas Hunger",
    title: "Fun Guitar and Ukulele",
    duration: 91, // in seconds
    cancellationToken: cancellationToken);

audio message

And a user can see the audio in Music Player:

music player

Method returns an audio message. Let's take a look at the value of message.Audio property in JSON format:

  "duration": 91,
  "mime_type": "audio/mpeg",
  "title": "Fun Guitar and Ukulele",
  "performer": "Joel Thomas Hunger",
  "file_id": "CQADBAADKQADA3oUUKalqDOOcqesAg",
  "file_size": 1102154


send voice method

A voice message is an OGG audio file. Let's send it differently this time by uploading the file from disk alongside with an HTTP request.

To run this example, download the NFL Commentary voice file to your disk.

A value is passed for duration because Telegram can't figure that out from a file's metadata.

⚠️ Replace /path/to/voice-nfl_commentary.ogg with an actual file path.

await using Stream stream = System.IO.File.OpenRead("/path/to/voice-nfl_commentary.ogg");
Message message = await botClient.SendVoiceAsync(
    chatId: chatId,
    voice: InputFile.FromStream(stream),
    duration: 36,
    cancellationToken: cancellationToken);

voice message

A voice message is returned from the method. Inspect the message.Voice property to learn more.