Posting audio through webhook to Discord using Python

Viewed 709

I have a file that is connected to another one of my programs, and upon being run, it sends an embed to Discord using the following code:

    payload = {"embeds": [
        {
          "author": {
            "name": "EAS Software",
            "icon_url": "https://www.fema.gov/sites/default/files/styles/unicorn_rotator/public/uni-rotator-imgs/eas_logo_rev1_3.png?itok=56iK9r2a"
          },
          "title": lines,
          "description": "Issued "+datetime.now().strftime('%m/%d/%Y %H:%M:%S') + " " + TMZ,
          "color": 0xff9900,
          "fields": [
            {
              "name": "Text Data:",
              "value": args.text
            },
            {
              "name": "EAS Protocol Data:",
              "value": "ZCZC-"+args.org+"-"+args.event+"-"+args.fips+"+"+args.time+"-"+ts_val+"-"+args.calls+"-"
            },
          ],
          "timestamp" : datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.000Z'),
          "footer": {
            "text": "Nerp EAS Logger System"
            }
        }
      ]
    }
    header_data = {'content-type': 'application/json'}
    requests.post(webhook1, json.dumps(payload), headers=header_data)

    print("Succesfully posted.")

And this works, I get an embed to post to Discord. The issue I am having is that another program generates a file, combined.wav, and I would like to send it to the same webhook. I have settled on splitting the File-sending process and the Embed process for ease of debugging, but the issue is that the code that sends the file sends it, but it has no data.

Here's the code I use to send the file:

    dataList = []
    boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
    dataList.append('--' + boundary)
    dataList.append('Content-Disposition: form-data; name=file; filename={0}'.format('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav'))
    fileType = mimetypes.guess_type('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav')[0] or 'application/octet-stream'
    dataList.append('Content-Type: {}'.format(fileType))
    dataList.append('')
    with open('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav') as f:
      dataList.append(f.read())
    dataList.append('--'+boundary+'--')
    dataList.append('')
    body = '\r\n'.join(dataList)
    payload = body
    headers = {
      'Content-Type': 'multipart/form-data; boundary=--------------------------248330258300818905783969',
      'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
    }
    conn.request("POST", "/api/webhooks/688944637209083926/vAv6UScav_j1AUun8GZ1RnYlYd2JvcvGvR1Wv5Db7_av3NhrBTsLWLlxH5PT8b0yQlpy", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

Does anyone know how to send a .WAV file to Discord in Python, or am I just outta luck?

**EDIT I found out that the system named discord-webhooks is cross platform, so I jsut used that using the following code:

#Post Json Embed to Discord!
webhook = DiscordWebhook(url='webhook1')
embed = DiscordEmbed(title=lines, description="Issued "+datetime.now().strftime('%m/%d/%Y %H:%M:%S') + " " + TMZ, color=0xff9900)
embed.set_author(name='EAS Software', icon_url='https://www.fema.gov/sites/default/files/styles/unicorn_rotator/public/uni-rotator-imgs/eas_logo_rev1_3.png?itok=56iK9r2a')
embed.set_footer(text='NERP EAS Logger System')
embed.set_timestamp()
embed.add_embed_field(name='EAS Text Data:', value=args.text, inline=False)
embed.add_embed_field(name='EAS Protocol Data:', value="ZCZC-"+args.org+"-"+args.event+"-"+args.fips+"+"+args.time+"-"+ts_val+"-"+args.calls+"-", inline=False)
webhook.add_embed(embed)
response = webhook.execute()
webhook = DiscordWebhook(url='webhook1')
with open("Output/FULL-ALERT-DATA.wav", "rb") as f:
    webhook.add_file(file=f.read(), filename='Alert-Audio.wav')
response = webhook.execute()
print("Succesfully posted.")

Thanks for the help!

0 Answers
Related