This is an old revision of the document!
Discord
Discord Bots
Webhooks
AutoHoktey v2
Send a message:
#Requires AutoHotkey v2 webhookURL := "https://discord.com/api/webhooks/..." http := ComObject("WinHttp.WinHttpRequest.5.1") http.Open("POST", webhookURL) http.SetRequestHeader("Content-Type", "application/json") http.Send('{"content": "test"}')
Send files/images:
#Requires AutoHotkey v2 data := CreateFormData(Map( "content", "A message with text and two images", "files[1]", "@image1.jpg", "files[2]", "@image2.png", )) http := ComObject("WinHTTP.WinHTTPRequest.5.1") http.Open("POST", "https://discord.com/api/webhooks/.../...") http.SetRequestHeader("Content-Type", data.contentType) http.Send(data.body) /** * Creates a COM WinHttpRequest compatible `multipart/form-data` body, * supporting file attachments like cURL. * * CreateFormData() by tmplinshi, AHK Topic: https://autohotkey.com/boards/viewtopic.php?t=7647 * Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731 * Modified version by SKAN, 09/May/2016 * Ported to AHKv2 and modified by G33kDude * * @param {Map} formData The fields to encode in the form body * * @return {Object} An object with properties `body` and `contentType` * * @example * data := CreateFormData(Map( * "content", "A message with text and two images", * "files[1]", "@image1.jpg", * "files[2]", "@image2.png", * )) * http := ComObject("WinHTTP.WinHTTPRequest.5.1") * http.Open("POST", "https://discord.com/api/webhooks/.../...") * http.SetRequestHeader("Content-Type", data.contentType) * http.Send(data.body) */ CreateFormData(formData) { boundary := Format( "{:016x}{:016x}", Random(0, 0x7FFFFFFFFFFFFFFF), Random(0, 0x7FFFFFFFFFFFFFFF) ) buf := Buffer() AppendStr(str) { oldSize := buf.Size buf.Size += strSize := StrPut(str, "UTF-8") - 1 StrPut(str, buf.Ptr + oldSize, strSize, "UTF-8") } AppendFile(file) { file.Pos := 0 oldSize := buf.Size buf.Size += file.Length file.RawRead(buf.ptr + oldSize, file.Length) } for k, v in formData { if v ~= "^@" { ; A file, following the cURL convention filePath := LTrim(v, "@") SplitPath filePath, &fileName file := FileOpen(filePath, "r") AppendStr( "--" boundary "`r`n" 'Content-Disposition: form-data; name="' k '"; filename="' fileName '"`r`n' "Content-Type: " MimeType(file) "`r`n`r`n" ) AppendFile(file) AppendStr("`r`n") continue } AppendStr( "--" boundary "`r`n" 'Content-Disposition: form-data; name="' k '"`r`n`r`n' v "`r`n" ) } AppendStr("--" boundary "--") return { body: SafeArrayFromBuffer(buf), contentType: "multipart/form-data; boundary=" boundary, } MimeType(file) { file.Pos := 0 n := file.ReadUInt() return ( (n = 0x474E5089) ? "image/png" : (n = 0x38464947) ? "image/gif" : (n & 0xFFFF = 0x4D42 ) ? "image/bmp" : (n & 0xFFFF = 0xD8FF ) ? "image/jpeg" : (n & 0xFFFF = 0x4949 ) ? "image/tiff" : (n & 0xFFFF = 0x4D4D ) ? "image/tiff" : "application/octet-stream" ) } SafeArrayFromBuffer(buf) { arr := ComObjArray(0x11, buf.Size) ; Create SAFEARRAY = VT_ARRAY|VT_UI1 DllCall("RtlMoveMemory", "Ptr", NumGet(ComObjValue(arr), 8 + A_PtrSize, "Ptr"), "Ptr", buf, "Ptr", buf.Size) return arr } }
AutoHoktey v1
Send a message:
#Requires AutoHotkey v1 webhookURL := "https://discord.com/api/webhooks/..." http := ComObject("WinHttp.WinHttpRequest.5.1") http.Open("POST", webhookURL) http.SetRequestHeader("Content-Type", "application/json") http.Send("{""content"": ""test""}")
Send files/images: https://www.autohotkey.com/boards/viewtopic.php?t=86273