You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

474 lines
17 KiB
Plaintext

;---FUNCTIONS-----------------------------------------------------------------------
; Misc Functions that are called by both the Video and Social Media poster
; -------------------------------Discord-------------------------------
PostToDiscordChannel(Message,WebhookChannel){
; Don't try to post a message if there is no webhook url
if(WebhookChannel = "")
Return
; Msgbox % "Message: " Message
; Msgbox % "WebhookChannel: " WebhookChannel
; Replace all the json forbidden characters
SingleQuote = "
ReplacedQuote = \"
Message := StrReplace(Message, "\", "\\") ; Replace Tabs
Message := StrReplace(Message, SingleQuote, ReplacedQuote) ; Replace single quote
Message := StrReplace(Message, "`r", "") ; Replace Carriage return
Message := StrReplace(Message, A_Tab, "\t") ; Replace Tabs
Message := StrReplace(Message, "`n", "\n") ; Escape New Line Character
Message := StrReplace(Message, "`f", "\f") ; Replace Tabs
; Convert into json string
JsonString=
(
{
"content": "%Message%"
}
)
try WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
try WebRequest.Open("POST", WebhookChannel, false)
try WebRequest.SetRequestHeader("Content-Type", "application/json")
try WebRequest.Send(JsonString)
}
; -------------------------------/Discord-------------------------------
; -------------------------------TelegramAPI-------------------------------
TelegramMsgBox(Text:="", TelegramBotToken := "", TelegramBotChatID :=""){
; Send Images:
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=68417&p=294332#p294332
; Msgbox % "Text: " Text
; Replace all forbidden characters - https://www.ascii-code.com/
Text := StrReplace(Text, "`n", "%0A") ; New Line
; Text := StrReplace(Text, "`n", "%0A") ; New Line
; Text := StrReplace(Text, " ", "%23") ; New Line
; Text :=
; Text := StrReplace(Text, "`n", "%0A") ; New Line
; Text := StrReplace(Text, "`%", "%25") ; percent with
; Msgbox % "Text: " Text
ErrorLoggingPath = %A_ScriptDir%\Lib\ErrorLogging\check.rups
loop 3
{
UrlDownloadToFile https://api.telegram.org/bot%TelegramBotToken%/sendmessage?chat_id=%TelegramBotChatID%&text=%Text%, %ErrorLoggingPath%
sleep 1000
ifexist %ErrorLoggingPath%
{
break
}
if A_index = 3
{
Message = Post failed due to API Issue.
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
Return "Failed"
; MsgBox, 16,, something went wrong with sending
}
}
FileRead, TelegramOutput, %ErrorLoggingPath%
TelegramOutput := "API Result: " . TelegramOutput
; Msgbox % "TelegramOutput: " TelegramOutput
filedelete %ErrorLoggingPath%
SaveOrPostProgress(Message:=TelegramOutput,PostType:="ErrorLoggingTextFile")
if(InStr(TelegramOutput, "error_code")){
Return "Failed"
}
}
; -------------------------------/TelegramAPI-------------------------------
; Telegram Message API
; https://core.telegram.org/bots/api
;------------------------------------------------
SendTelegramMessage(token, chatID, text := "", ParseMode := "MarkdownV2") ; you could add more options; compare the Telegram API docs
{
; ParseMode := "MarkdownV2"
; msgbox
; Msgbox % "text: " text
url_str := "https://api.telegram.org/bot" token "/sendMessage"
objParam := { "chat_id" : chatID
,"parse_mode" : ParseMode
,"text" : text }
CreateFormData(postData, hdr_ContentType, objParam)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url_str, true)
whr.SetRequestHeader("Content-Type", hdr_ContentType)
; whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko") ; ???????
whr.Option(6) := False ; No auto redirect
whr.Send(postData)
whr.WaitForResponse()
json_resp := whr.ResponseText
whr := ; free COM object
; Msgbox % "json_resp: " json_resp
; if(InStr(json_resp, "error_code"))
Return json_resp
}
/*
*bold \*text*
_italic \*text_
__underline__
~strikethrough~
*bold _italic bold ~italic bold strikethrough~ __underline italic bold___ bold*
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)
`inline fixed-width code`
```
pre-formatted fixed-width code block
```
```python
pre-formatted fixed-width code block written in the Python programming language
```
*/
; -------------------------------Telegram Image Sending-------------------------------
; https://www.autohotkey.com/boards/viewtopic.php?t=68417
; https://core.telegram.org/bots/api
SendTelegramPhoto(token, chatID, file, caption := "", ParseMode := "MarkdownV2") ; you could add more options; compare the Telegram API docs
{
; ParseMode := "MarkdownV2"
url_str := "https://api.telegram.org/bot" token "/sendPhoto"
objParam := { "chat_id" : chatID
, "photo" : [file]
, "parse_mode" : ParseMode
, "caption" : caption}
CreateFormData(postData, hdr_ContentType, objParam)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url_str, true)
whr.SetRequestHeader("Content-Type", hdr_ContentType)
; whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko") ; ???????
whr.Option(6) := False ; No auto redirect
whr.Send(postData)
whr.WaitForResponse()
json_resp := whr.ResponseText
whr := ; free COM object
; Msgbox % "json_resp: " json_resp
; if(InStr(json_resp, "error_code"))
Return json_resp
}
; -------------------------------Telegram Image Sending-------------------------------
; https://www.autohotkey.com/boards/viewtopic.php?t=68417
SendTelegramVideo(token, chatID, file, caption := "", ParseMode := "MarkdownV2") ; you could add more options; compare the Telegram API docs
{
; ParseMode := "MarkdownV2"
url_str := "https://api.telegram.org/bot" token "/sendVideo?caption=" caption
objParam := { "chat_id" : chatID
, "video" : [file]
, "parse_mode" : ParseMode }
CreateFormData(postData, hdr_ContentType, objParam)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url_str, true)
whr.SetRequestHeader("Content-Type", hdr_ContentType)
; whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko") ; ???????
whr.Option(6) := False ; No auto redirect
whr.Send(postData)
whr.WaitForResponse()
json_resp := whr.ResponseText
whr := ; free COM object
; Msgbox % "json_resp: " json_resp
; if(InStr(json_resp, "error_code"))
Return json_resp
}
; -------------------------------Telegram File Sending-------------------------------
; https://www.autohotkey.com/boards/viewtopic.php?t=68417
SendTelegramFile(token, chatID, file, caption := "", ParseMode := "MarkdownV2") ; you could add more options; compare the Telegram API docs
{
; ParseMode := "MarkdownV2"
url_str := "https://api.telegram.org/bot" token "/sendDocument?caption=" caption
objParam := { "chat_id" : chatID
, "document" : [file]
, "parse_mode" : ParseMode }
CreateFormData(postData, hdr_ContentType, objParam)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url_str, true)
whr.SetRequestHeader("Content-Type", hdr_ContentType)
; whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko") ; ???????
whr.Option(6) := False ; No auto redirect
whr.Send(postData)
whr.WaitForResponse()
json_resp := whr.ResponseText
whr := ; free COM object
; Msgbox % "json_resp: " json_resp
; if(InStr(json_resp, "error_code"))
Return json_resp
}
; -------------------------------/Telegram API Image Sending-------------------------------
ASCIISTRReplace(Message){
; Msgbox % "Message of function: " Message
; https://tech.saigonist.com/b/code/escaping-special-characters-markdown.html
; Replace all forbidden characters - https://www.ascii-code.com/
LineBreakChar = `%0A ; Used for API
; @ todo: replace with regex
Message := StrReplace(Message, "\", "\\") ; replace all new line characters with the global charater
Message := StrReplace(Message, "*", "\*") ; replace all new line characters with the global charater
; Message := StrReplace(Message, "`n", LineBreakChar) ; replace all new line characters with the global charater
Message := StrReplace(Message, "!", "\!") ; replace all new line characters with the global charater
Message := StrReplace(Message, "_", "\_") ; replace all new line characters with the global charater
Message := StrReplace(Message, "{", "\{") ; replace all new line characters with the global charater
Message := StrReplace(Message, "}", "\}") ; replace all new line characters with the global charater
Message := StrReplace(Message, "[", "\[") ; replace all new line characters with the global charater
Message := StrReplace(Message, "]", "\]") ; replace all new line characters with the global charater
Message := StrReplace(Message, "(", "\(") ; replace all new line characters with the global charater
Message := StrReplace(Message, ")", "\)") ; replace all new line characters with the global charater
Message := StrReplace(Message, "#", "\#") ; replace all new line characters with the global charater
Message := StrReplace(Message, "+", "\+") ; replace all new line characters with the global charater
Message := StrReplace(Message, "-", "\-") ; replace all new line characters with the global charater
Message := StrReplace(Message, ".", "\.") ; replace all new line characters with the global charater
Message := StrReplace(Message, ">", "\>") ; replace all new line characters with the global charater
Message := StrReplace(Message, "<", "\<") ; replace all new line characters with the global charater
Message := StrReplace(Message, "=", "\=") ; replace all new line characters with the global charater
Message := StrReplace(Message, "|", "\|") ; replace all new line characters with the global charater
Message := StrReplace(Message, "~", "\~") ; replace all new line characters with the global charater
Message := StrReplace(Message, "`", "\`") ; replace all new line characters with the global charater
Message := StrReplace(Message, "$", "\$") ; replace all new line characters with the global charater
; Message := StrReplace(Message, "%", "\%") ; replace all new line characters with the global charater
Message := StrReplace(Message, "&", "\&") ; replace all new line characters with the global charater
; Msgbox % "replaced Message: " Message
Return Message
}
; -------------------------------Discord Images-------------------------------
UploadImageToDiscord(Webhook, Message:="", Filepath:=""){
filepath := [Filepath] ; Create object
objParam := {file: filepath, content: Message}
CreateFormData(PostData, hdr_ContentType, objParam)
HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
HTTP.Open("POST", Webhook, true)
HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
HTTP.SetRequestHeader("Content-Type", hdr_ContentType)
HTTP.SetRequestHeader("Pragma", "no-cache")
HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
HTTP.Send(PostData)
HTTP.WaitForResponse()
; msgbox % HTTP.ResponseText
return HTTP.ResponseText
}
; -------------------------------/Discord Images-------------------------------
;---------------------------Discord Messages---------------------
UploadMessageToDiscord(Webhook, Message:=""){
; filepath := [Filepath] ; Create object
objParam := {file: filepath, content: Message}
CreateFormData(PostData, hdr_ContentType, objParam)
HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
HTTP.Open("POST", Webhook, true)
HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
HTTP.SetRequestHeader("Content-Type", hdr_ContentType)
HTTP.SetRequestHeader("Pragma", "no-cache")
HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
HTTP.Send(PostData)
HTTP.WaitForResponse()
; msgbox % HTTP.ResponseText
return HTTP.ResponseText
}
;---------------------------/Discord Messages---------------------
; -------------------------------CreateFormData - Creates "multipart/form-data" for http post-------------------------------
; Used for WinHttp.WinHttpRequest.5.1, Msxml2.XMLHTTP ...
CreateFormData(ByRef retData, ByRef retHeader, objParam) {
New CreateFormData(retData, retHeader, objParam)
}
; Used for WinInet
CreateFormData_WinInet(ByRef retData, ByRef retHeader, objParam) {
New CreateFormData(safeArr, retHeader, objParam)
size := safeArr.MaxIndex() + 1
VarSetCapacity(retData, size, 1)
DllCall("oleaut32\SafeArrayAccessData", "ptr", ComObjValue(safeArr), "ptr*", pdata)
DllCall("RtlMoveMemory", "ptr", &retData, "ptr", pdata, "ptr", size)
DllCall("oleaut32\SafeArrayUnaccessData", "ptr", ComObjValue(safeArr))
}
Class CreateFormData {
__New(ByRef retData, ByRef retHeader, objParam) {
CRLF := "`r`n"
Boundary := this.RandomBoundary()
BoundaryLine := "------------------------------" . Boundary
; Loop input paramters
binArrs := []
For k, v in objParam
{
If IsObject(v) {
For i, FileName in v
{
str := BoundaryLine . CRLF
. "Content-Disposition: form-data; name=""" . k . """; filename=""" . FileName . """" . CRLF
. "Content-Type: " . this.MimeType(FileName) . CRLF . CRLF
binArrs.Push( BinArr_FromString(str) )
binArrs.Push( BinArr_FromFile(FileName) )
binArrs.Push( BinArr_FromString(CRLF) )
}
}
Else {
str := BoundaryLine . CRLF
. "Content-Disposition: form-data; name=""" . k """" . CRLF . CRLF
. v . CRLF
binArrs.Push( BinArr_FromString(str) )
}
}
str := BoundaryLine . "--" . CRLF
binArrs.Push( BinArr_FromString(str) )
retData := BinArr_Join(binArrs*)
retHeader := "multipart/form-data; boundary=----------------------------" . Boundary
}
RandomBoundary() {
str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
Sort, str, D| Random
str := StrReplace(str, "|")
Return SubStr(str, 1, 12)
}
MimeType(FileName) {
n := FileOpen(FileName, "r").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"
}
}
;#############################################################################################################
; Update: 2015-6-4 - Added BinArr_ToFile()
BinArr_FromString(str) {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 2 ; adTypeText
oADO.Mode := 3 ; adModeReadWrite
oADO.Open
oADO.Charset := "UTF-8"
oADO.WriteText(str)
oADO.Position := 0
oADO.Type := 1 ; adTypeBinary
oADO.Position := 3 ; Skip UTF-8 BOM
return oADO.Read, oADO.Close
}
BinArr_FromFile(FileName) {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 1 ; adTypeBinary
oADO.Open
oADO.LoadFromFile(FileName)
return oADO.Read, oADO.Close
}
BinArr_Join(Arrays*) {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 1 ; adTypeBinary
oADO.Mode := 3 ; adModeReadWrite
oADO.Open
For i, arr in Arrays
oADO.Write(arr)
oADO.Position := 0
return oADO.Read, oADO.Close
}
BinArr_ToString(BinArr, Encoding := "UTF-8") {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 1 ; adTypeBinary
oADO.Mode := 3 ; adModeReadWrite
oADO.Open
oADO.Write(BinArr)
oADO.Position := 0
oADO.Type := 2 ; adTypeText
oADO.Charset := Encoding
return oADO.ReadText, oADO.Close
}
BinArr_ToFile(BinArr, FileName) {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 1 ; adTypeBinary
oADO.Open
oADO.Write(BinArr)
oADO.SaveToFile(FileName, 2)
oADO.Close
}
; -------------------------------/CreateFormData - Creates "multipart/form-data" for http post-------------------------------