Sending a proper http request using Discord Webhook

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
loumizhu
Posts: 28
Joined: 18 Aug 2016, 22:07

Sending a proper http request using Discord Webhook

28 Feb 2023, 07:57

[Mod edit: Topic moved to v1 help, based on posted script.]

Hello 👋🏻 wonderful persons of ahk community
I'm using a script I found, to send messages to my discord server (without opening browser or app) just an inputbox to type a msg and bye
It send it as a bot , and does the job quite good

but when it comes to some messages with special character, it fails

i'm not a pro of http requests and don't know the real problem.
I figured that the message in the content part is not encoded properly
I tried URLencode function on it. it works, but in Discord you see the message encoded too
It is unredable of course (a block of text with a lot of %20)

i don't how the text should be encoded

any help ?

Code: Select all

Send_Msg_to_Discord(msg,Url="MyWebhookURL"){
   ;Default parmaeter is url of general Of my server

   EncodedMsg := UrlEncode(msg)
   
   postdata=
   (
   {
      "content": "%EncodedMsg%"
   }
   ) ;Use https://leovoel.github.io/embed-visualizer/ to generate above webhook code

   WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
   WebRequest.Open("POST", url, false)
   WebRequest.SetRequestHeader("Content-Type", "application/json")
   WebRequest.Send(postdata)  
}

; urlEncode [by RaptorX]
urlEncode(url){
   f = %A_FormatInteger%
   SetFormat, Integer, Hex
   While (RegexMatch(url,"\W", var))
       StringReplace, url, url, %var%, % asc(var), All
   StringReplace, url, url, 0x, `%, All
   SetFormat, Integer, %f%
   return url
}
 
loumizhu
Posts: 28
Joined: 18 Aug 2016, 22:07

Re: Sending a proper http request using Discord Webhook

28 Feb 2023, 16:52

I found the solution

It had to be webhook json formatted as indicated here
https://birdie0.github.io/discord-webhooks-guide/json.html

I did a stupid double quote variable because I forgot how to have backslash and quote in an expression ....

Code: Select all

Send_Msg_to_Discord(msg,Url="webhookurl"){
   ;Default parmaeter is url of general Of my Server

   EncodedMsg := JsonReady(msg)
   
   postdata=
   (
   {
      "content": "%EncodedMsg%"
   }
   ) ;Use https://leovoel.github.io/embed-visualizer/ to generate above webhook code

   WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
   WebRequest.Open("POST", url, false)
   WebRequest.SetRequestHeader("Content-Type", "application/json")
   WebRequest.Send(postdata)  
}


JsonReady(msg) {
q="
dq=
(
\"
)
    msg := StrReplace(msg, q, dq) ; Replace double quote with \"
    msg := StrReplace(msg, "/", "\\/") ; Replace slash with \/
    msg := StrReplace(msg, "`n", "\n") ; Replace newline with \n
    msg := StrReplace(msg, "`r", "\r") ; Replace carriage return with \r
    msg := StrReplace(msg, "`t", "\t") ; Replace horizontal tab with \t
    msg := StrReplace(msg, "`b", "\b") ; Replace backspace with \b
    msg := StrReplace(msg, "`f", "\f") ; Replace form feed with \f
    return % msg
}
[Mod edit: [code][/code] tags added.]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], CoffeeChaton, Google [Bot], wjt936826577 and 164 guests