Skip to content

Instantly share code, notes, and snippets.

@HDR
Last active April 8, 2024 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HDR/7d5d4ce8bbe4b715d788a9bc9f99e02d to your computer and use it in GitHub Desktop.
Save HDR/7d5d4ce8bbe4b715d788a9bc9f99e02d to your computer and use it in GitHub Desktop.

Sending Voice Messages via Discord Bots

Voice Message

Requirements:

  • An .ogg format audio file
  • A Discord bot Token

Notes:

  • files created via the vorbis encoder may not play back on mobile, therfore it's suggested to use the opus encoder for your .ogg files
  • No other data like content, embeds or attachments can be sent along with voice messages

Requesting a upload url

A upload url is required to upload the audio file, this is obtained via http post request

Url: https://discord.com/api/v10/channels/<channelid>/attachments

Method: POST

Data:

{
  "files": [
    {
      "filename": "voice-message.ogg",
      "file_size": <size_in_bytes>,
      "id": "2"
    }
  ]
}

Headers:

{
    "Content-Type": "application/json",
    "Authorization": "Bot <bot_token>"
}

Response:

{
    "attachments": [
        {
            "id": 2,
            "upload_url": "<url>",
            "upload_filename": "<filename>"
        }
    ]
}
  • upload_url - The target url for the file upload
  • upload_filename - The filename used when posting the voice message to a channel

Uploading the audio file

Uploading the file is as simple as sending a http put request with your .ogg to the upload_url returned from the previous http request

Url: upload_url

Method: PUT

Data:

Raw file data(.ogg file)

Headers:

{
    "Content-Type": "audio/ogg",
    "Authorization": "Bot <bot_token>"
}

Sending the voice message to a channel

When sending a voice message there are some limitations and required values to be aware of.

Limitations

  • No additional data like contents, embeds or attachments can be sent with the voice message

Required data fields

  • "flags": 8192 - This coresponds to IS_VOICE_MESSAGE, and it is required to embed the voice message.

Required Attachment data fields

  • "id": "0" - Attachment ID
  • "filename": "voice-message.ogg" - Filename
  • "uploaded_filename": "<upload_filename>" - This requires the upload_filename returned from the first http post request
  • "duration_secs": <time> - This value can pretty much be anything, but it will show the incorrect audio duration until clicked if it's not accurate
  • "waveform": "<base64 encoded byte array>" - This dictates what the waveform preview looks like to users, it takes a base64 encoded byte array containing up to 256 entries, you can use pretty much any base64 encoded array here if you don't care about what the waveform looks like.

Url: https://discord.com/api/v10/channels/<channelid>/messages

Method: POST

Data:

{
  "flags": 8192,
  "attachments": [
    {
      "id": "0",
      "filename": "voice-message.ogg",
      "uploaded_filename": "<filename>",
      "duration_secs": <duration>,
      "waveform": "<base64 encoded byte array>"
    }
  ]
}

Headers:

{
    "Content-Type": "application/json",
    "Authorization": "Bot <bot_token>"
}

Response: Code 200

{
    raw_message_data
}

Waveform Example

Byte Array:

[
   105,  197,  58,   85,   175,  84,   113,  37,   89,   206,  197,  112,
   236,  133,  63,   243,  75,   52,   42,   31,   233,  110,  180,  220,
   194,  106,  81,   245,  214,  184,  154,  244,  30,   140,  131,  50,
   146,  72,   40,   244,  93,   133,  125,  224,  157,  219,  126,  74,
   253,  171,  71,   102,  212,  128,  155,  18,   158,  77,   72,   10,
   196,  7,    77,   84,   239,  15,   2,    134,  106,  213,  193,  205,
   65,   116,  255,  60,   123,  97,   47,   107,  31,   61,   145,  146,
   119,  19,   96,   44,   125,  0,    184,  156,  21,   120,  142,  208,
   100,  157,  54,   145,  238,  52,   248,  121,  20,   112,  26,   3,
   118,  161,  131,  101,  229,  47,   62,   170,  8,    69,   177,  54,
   220,  170,  254,  177,  236,  88,   97,   238,  29,   86,   175,  179,
   200,  39,   183,  114,  47,   250,  206,  67,   22,   109,  80,   105,
   10,   58,   188,  15,   198,  89,   16,   90,   63,   150,  99,   201,
   78,   72,   35,   194,  169,  234,  30,   231,  250,  93,   62,   38,
   39,   34,   205,  166,  20,   35,   188,  81,   253,  105,  26,   136,
   102,  147,  232,  67,   61,   108,  27,   0,    24,   218,  251,  44,
   67,   153,  147,  203,  146,  121,  154,  225,  168,  250,  127,  54,
   111,  76,   69,   68,   230,  112,  176,  154,  110,  152,  59,   5,
   139,  128,  218,  255,  206,  110,  89,   47,   216,  206,  54,   57,
   5,    220,  70,   105,  207,  43,   129,  74,   166,  244,  130,  161,
   236,  78,   134,  31,   51,   134,  59,   45,   11,   64,   140,  247,
   59,   137,  237,  78
]

Base64 encoded byte array:

acU6Va9UcSVZzsVw7IU/80s0Kh/pbrTcwmpR9da4mvQejIMykkgo9F2FfeCd235K/atHZtSAmxKeTUgKxAdNVO8PAoZq1cHNQXT/PHthL2sfPZGSdxNgLH0AuJwVeI7QZJ02ke40+HkUcBoDdqGDZeUvPqoIRbE23Kr+sexYYe4dVq+zyCe3ci/6zkMWbVBpCjq8D8ZZEFo/lmPJTkgjwqnqHuf6XT4mJyLNphQjvFH9aRqIZpPoQz1sGwAY2vssQ5mTy5J5muGo+n82b0xFROZwsJpumDsFi4Da/85uWS/YzjY5BdxGac8rgUqm9IKh7E6GHzOGOy0LQIz3O4ntTg==

Result:

Documentation

Discord's Developer Portal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment