Jump to content

Recommended Posts

Posted (edited)

How would you like to run:

curl -d "Finished running my script. All good." ntfy.sh/Testing_AutoIt_script

or

ConsoleWrite(ntfy_send("**All good**, all the time 😃") & @TAB & @error & @TAB & @extended & @CRLF)
Func ntfy_send($sMsg, $sTopicName = "Testing_AutoIt_script")
    Local Static $iErr = 0, $oErr
    If IsObj($sMsg) Then
        If $sMsg.retcode Then $iErr = "0x" & Hex($sMsg.retcode, 8)
        Return
    EndIf
    If ObjEvent("AutoIt.Error") = "" Then $oErr = ObjEvent("AutoIt.Error", "ntfy_send")
    Local $sUrl = "https://ntfy.sh/" & $sTopicName
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", $sUrl, False) ; Open the connection using POST method (synchronous = False)
    $oHTTP.SetRequestHeader("Markdown", "yes") ; Set Markdown ON
    $oHTTP.SetRequestHeader("Tags", "heavy_check_mark,PC:" & "@ComputerName") ; https://docs.ntfy.sh/publish/#tags-emojis
    $oHTTP.Send($sMsg) ; Send the request
    If ObjEvent("AutoIt.Error") = "ntfy_send" Then $oErr = ObjEvent("AutoIt.Error", "")
    If $iErr Then Return SetError(2, $iErr, "oHTTP.Send FAILED")
    Return SetError(Int($oHTTP.Status <> 200), $oHTTP.Status, ($oHTTP.Status = 200 ? StringReplace($oHTTP.ResponseText, @LF, "") : $oHTTP.StatusText))
EndFunc   ;==>ntfy_send

and be notified in your phone, browser or ...basically any possible way ?!, meet ntfy.sh 🤩

Ok. How would you like to have your own AutoIt receiver for these notifications 🤯

For that you'll need the curl DLL, the curl UDF and something like this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt("TrayAutoPause", 0) ; Script pauses when click on tray icon = OFF/0
Opt("TrayOnEventMode", 0) ; Enable/disable OnEvent functions notifications for the tray = OFF/0
Opt("GUICloseOnESC", 0) ; When ESC is pressed on a GUI the $GUI_EVENT_CLOSE message is sent = ON/1

If Not FileGetSize(@ScriptDir & '\curl-ca-bundle.crt') Then
    MsgBox(262144 + 64, @ScriptName, "The curl stuff needs to be in the same folder for this example", 30)
    Exit
EndIf

If Not @AutoItX64 Then
    MsgBox(262144 + 64, @ScriptName, "It needs to run x64", 30)
    Exit
EndIf


#include "Curl.au3" ; https://www.autoitscript.com/forum/topic/207859-curl-udf-libcurl-with-x64-support
#include <GuiEdit.au3> ;_GUICtrlEdit_AppendText()

; GUI stuff
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 565, 425)
Global $Group1 = GUICtrlCreateGroup("MyTopic", 8, 8, 305, 49)
Global $Input1 = GUICtrlCreateInput("Testing_AutoIt_script", 16, 24, 281, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $Button1 = GUICtrlCreateButton("Gather all in cache", 320, 16, 131, 25)
Global $Button2 = GUICtrlCreateButton("Listen ( start )", 464, 16, 91, 25)
Global $Group2 = GUICtrlCreateGroup("Dump", 8, 96, 545, 321)
Global $Edit1 = GUICtrlCreateEdit("", 16, 120, 529, 289)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $Label1 = GUICtrlCreateLabel("last ""keepalive""", 8, 64, 548, 33, $SS_CENTER)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $g_nfty_CurlPeeked, $g_nfty_ListenStop = 1
Global $__g_nfty_RunningHandles, $__g_nfty_Curl = 0, $__g_nfty_Multi = 0
OnAutoItExitRegister(nfty_Listen_Cleanup)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            If $g_nfty_ListenStop = 1 Then
                GUIDelete()
                Exit
            EndIf
            GUICtrlSetState($Button2, $GUI_DISABLE)
            GUICtrlSetData($Button2, "Listen ( stoping )")
            $g_nfty_ListenStop = 2

        Case $Button1
            $g_nfty_ListenStop = 0
            nfty_Listen_Start(GUICtrlRead($Input1), "?poll=1&since=all")
            nfty_Listen_Perform()
        Case $Button2
            If $g_nfty_ListenStop Then
                GUICtrlSetState($Button1, $GUI_DISABLE)
                GUICtrlSetState($Button2, $GUI_DISABLE)
                $g_nfty_ListenStop = 0
                GUICtrlSetData($Button2, "Listen ( starting )")
                nfty_Listen_Start(GUICtrlRead($Input1))
                If @error Then
                    $g_nfty_ListenStop = 1
                    GUICtrlSetData($Button2, "Listen ( start )")
                    GUICtrlSetState($Button1, $GUI_ENABLE)
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                Else
                    GUICtrlSetData($Button2, "Listen ( stop )")
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                EndIf
            Else
                GUICtrlSetState($Button2, $GUI_DISABLE)
                GUICtrlSetData($Button2, "Listen ( stoping )")
                $g_nfty_ListenStop = 2
            EndIf


    EndSwitch

    Switch $g_nfty_ListenStop
        Case 1 ; was never running
            ContinueLoop
        Case 0 ; not stopped - is running
            nfty_Listen_Perform()
        Case 2 ; request to stop
            nfty_Listen_Cleanup()
            $g_nfty_ListenStop = 1
            GUICtrlSetData($Button2, "Listen ( start )")
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_ENABLE)
    EndSwitch

WEnd

Func nfty_Listen_Start($sTopic, $suffix = "")
    $__g_nfty_Curl = Curl_Easy_Init()
    If Not $__g_nfty_Curl Then
        $g_nfty_ListenStop = 0
        Return SetError(1, 0, 1)
    EndIf

    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_URL, "https://ntfy.sh/" & $sTopic & "/json" & $suffix)
;~  Curl_Easy_Setopt($Curl, $CURLOPT_USERAGENT, "AutoIt/Curl")
    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_FOLLOWLOCATION, 1)
    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_ACCEPT_ENCODING, "")

    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_WRITEFUNCTION, Curl_DataWriteCallback_nfty())
    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_WRITEDATA, $__g_nfty_Curl)

    Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_TCP_KEEPALIVE, 1)
    If $suffix Then Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_TIMEOUT, 10) ; 10 sec timeout

    ;peer verification
    curl_easy_setopt($__g_nfty_Curl, $CURLOPT_CAINFO, @ScriptDir & '\curl-ca-bundle.crt') ;
;~  Curl_Easy_Setopt($__g_nfty_Curl, $CURLOPT_SSL_VERIFYPEER, 0)

    $__g_nfty_Multi = Curl_Multi_Init()
    If Not $__g_nfty_Multi Then
        $g_nfty_ListenStop = 0
        nfty_Listen_Cleanup()
        Return SetError(2, 0, 2)
    EndIf
    Curl_Multi_Add_Handle($__g_nfty_Multi, $__g_nfty_Curl)
EndFunc   ;==>nfty_Listen_Start

Func nfty_Listen_Perform()
    Local Static $MsgsInQueue, $hTimer = 0
    If TimerDiff($hTimer) < 100 Then Return ; no need to ask so often
    $hTimer = TimerInit() ; let's restart the timer

    Curl_Multi_Perform($__g_nfty_Multi, $__g_nfty_RunningHandles)
    Local $CURLMsg = Curl_Multi_Info_Read($__g_nfty_Multi, $MsgsInQueue)
    If DllStructGetData($CURLMsg, "msg") = $CURLMSG_DONE Then
        Local $__g_nfty_Curl = DllStructGetData($CURLMsg, "easy_handle")
        Local $Code = DllStructGetData($CURLMsg, "data")
        If $Code = $CURLE_OK Then
;~          ConsoleWrite('@@(' & @ScriptLineNumber & ') : ' & @MIN & ':' & @SEC & '.' & @MSEC & @TAB & "Content Type: " & Curl_Easy_GetInfo($__g_nfty_Curl, $CURLINFO_CONTENT_TYPE) & @LF)
;~          ConsoleWrite('@@(' & @ScriptLineNumber & ') : ' & @MIN & ':' & @SEC & '.' & @MSEC & @TAB & "Download Size: " & Curl_Easy_GetInfo($__g_nfty_Curl, $CURLINFO_SIZE_DOWNLOAD) & @LF)
;~          ConsoleWrite('@@(' & @ScriptLineNumber & ') : ' & @MIN & ':' & @SEC & '.' & @MSEC & @TAB & '- Header >' & BinaryToString(Curl_Data_Get($__g_nfty_Curl + 1)) & '<' & @CRLF)
;~          ConsoleWrite('@@(' & @ScriptLineNumber & ') : ' & @MIN & ':' & @SEC & '.' & @MSEC & @TAB & '- Html >' & BinaryToString(Curl_Data_Get($__g_nfty_Curl)) & '<' & @CRLF)
        Else
            ConsoleWrite('@@(' & @ScriptLineNumber & ') : ' & @MIN & ':' & @SEC & '.' & @MSEC & @TAB & Curl_Easy_StrError($Code) & @LF)
        EndIf
        nfty_Listen_Cleanup()
        $g_nfty_ListenStop = 1
    EndIf
EndFunc   ;==>nfty_Listen_Perform

Func nfty_Listen_Cleanup()
    If $__g_nfty_Multi Then
        Curl_Multi_Remove_Handle($__g_nfty_Multi, $__g_nfty_Curl)
        Curl_Multi_Cleanup($__g_nfty_Multi)
        $__g_nfty_Multi = 0
    EndIf
    Curl_Easy_Cleanup($__g_nfty_Curl)
    Curl_Data_Cleanup($__g_nfty_Curl)
    Curl_Data_Cleanup($__g_nfty_Curl + 1)
    $__g_nfty_Curl = 0
EndFunc   ;==>nfty_Listen_Cleanup

Func Curl_DataWriteCallback_nfty()
    Static $Ptr = DllCallbackGetPtr(DllCallbackRegister(__Curl_DataWriteCallback_nfty, (@AutoItX64 ? "uint_ptr" : "uint_ptr:cdecl"), "ptr;uint_ptr;uint_ptr;ptr"))
    Return $Ptr
EndFunc   ;==>Curl_DataWriteCallback_nfty

Func __Curl_DataWriteCallback_nfty($Ptr, $Size, $Nmemb, $Handle)
    Local $Length = $Size * $Nmemb, $Data = __Curl_Peek("byte[" & $Length & "]", $Ptr)
    $g_nfty_CurlPeeked = StringReplace(BinaryToString($Data), @LF, @CRLF)
    If StringInStr($g_nfty_CurlPeeked, ',"event":"open","topic":"') Or StringInStr($g_nfty_CurlPeeked, ',"event":"keepalive","topic":"') Then
        ; not important
        GUICtrlSetData($Label1, @MIN & ":" & @SEC & " >" & StringStripCR($g_nfty_CurlPeeked) & "<")
    Else
        _GUICtrlEdit_AppendText($Edit1, $g_nfty_CurlPeeked & @CRLF)
    EndIf
    Return $Length
EndFunc   ;==>__Curl_DataWriteCallback_nfty

Do place your scripts in the bin folder where libcurl-x64.dll is at, or this example will not work as is. This is just an example.

 

FAQ:
Q: Why the curl UDF ?, why not ...
A: Because "the page" never finishes the download ( and is not meant to finish ). Couldn't find a way to peek into the message other than this.

Q: Can you ... 
A: Maybe. But am busy, and not all that good at all these. If you code that you wish it had, do share it in the forum. ( Think GPLv2 )

Q: I sent a message but my receiver script wasn't running at the time / Lost internet for X hours.
A: Text messages are likely to be available for up to 12 hours ( for what I read ). Just click "Gather all in cache" in this example.

Q: How can I ...
A: ntfy has excellent documentation. Read it.

Q: Can I make my messages private ?
A: Yes you can. Read the offers they have available for the service.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

The max. topic name is 64 characters in ntfy. The max. char viewable in the WebUI is about 18 char.
That means that you can have a topic that shows the name, and a hidden randomness added to it.
And lets say that you'd like to change the unique random part of the topic makes impossible to guess but for you to be able to calculate.

Topics like these:

Testing_AutoIt_script______NBlR3rhNisx0Wz4NAWkGbgLWwqmiC5Mowncgk
Testing_AutoIt_script______mTTKMjxeZoP3f2VtMU8M5x1Zn3y5OdmVmQfJd
Testing_AutoIt_script______hGUseiM3rlxFnrhzaFizYj1JjClxBEyAtIi19
Testing_AutoIt_script______C6USjfsYrgOrPQqp7U0DEkgvkiMXDLgSxKRTd
Testing_AutoIt_script______fm8GAGVEXcsuIKe2WTzMM922h4GL5CdqyHxkz
Testing_AutoIt_script______ZhbeFm92lm7OZdnkH7LsZDkqWYfHit4FH10im

that has that random part of it and, can be calculated by you by knowing the variables that creates it ;) 

Spoiler
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Crypt.au3>

Exit main()
Func main()
    Local $Form1 = GUICreate("ntfy topic generator", 900, 100, -1, -1)
    GUISetFont(10)
    Local $Button1 = GUICtrlCreateButton("Generate", 5, 5, 95, 23)
    GUICtrlCreateLabel("Type an up to 18 char long topic, and click ""Generate"" to fill the rest of the topic with a random string to fill the 64 char. limit", 110, 8, 800, 21)
    Local $Input1 = GUICtrlCreateInput("Testing AutoIt script", 8, 35, 880, 23)
    Local $Label1 = GUICtrlCreateLabel("Debug/Info string", 8, 75, 880, 19) ;, -1, $WS_EX_STATICEDGE)
    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Button1
                GUICtrlSetData($Input1, rndmStr($Label1, GUICtrlRead($Input1), @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC, "My password ?"))
;~              GUICtrlSetData($Input1, rndmStr($Label1, GUICtrlRead($Input1), @YEAR & @MON & @MDAY, "My password ?")) ; calculable random
        EndSwitch
    WEnd
EndFunc   ;==>main

Func rndmStr($Label1, $sTopic = "", $sSeed = "", $sSalt = "")
    If $sSeed = "" Then $sSeed = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC
    If $sSalt = "" Then $sSalt = "andPepper"
    If $sTopic = "" Then $sTopic = "a topic for you"
    $sTopic = StringReplace($sTopic, " ", "_")
    Local $sInfo, $iDigits = 10, $aSpace[3], $aSplit = StringSplit($sTopic, "_____", 1)
    $sTopic = $aSplit[1] & "______"
    $sInfo = "StrLen: " & StringLen($aSplit[1]) & ' >' & $aSplit[1] & '<  '
    For $i = StringLen($sTopic) To 22
        $sTopic &= "_"
    Next
    $iDigits = 64 - StringLen($sTopic)
    $sTopic &= StringLeft(_HashULN($sSeed, $sSalt), $iDigits) ; re-calculable-random(ish)
    $sInfo &= " - Your Topic (" & StringLen($sTopic) & " digits)"
    GUICtrlSetData($Label1, $sInfo) ; lazy topic length feedback
    ConsoleWrite($sInfo & ' >' & (StringLen($sTopic) > 65 ? " is too long !" : $sTopic) & '<' & @CRLF)
    Return $sTopic
EndFunc   ;==>rndmStr

Func _HashULN($sStr, $sSalt = "a random string you invent") ; upper/lower/number
    Return StringReplace(StringReplace(StringReplace(StringReplace(_Base64Encode(_Crypt_HashData( _
            $sStr & $sSalt, $CALG_SHA_512)), @CRLF, ""), "/", ""), "+", ""), "=", "") ; StrLen: 86
EndFunc   ;==>_HashULN

Func _Base64Encode($input)
    $input = Binary($input)
    Local $struct = DllStructCreate("byte[" & BinaryLen($input) & "]")
    DllStructSetData($struct, 1, $input)
    Local $strc = DllStructCreate("int")
    Local $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($struct), "int", DllStructGetSize($struct), _
            "int", 1, "ptr", 0, "ptr", DllStructGetPtr($strc))
    If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating buffer length
    Local $a = DllStructCreate("char[" & DllStructGetData($strc, 1) & "]")
    $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($struct), "int", DllStructGetSize($struct), _
            "int", 1, "ptr", DllStructGetPtr($a), "ptr", DllStructGetPtr($strc))
    If @error Or Not $a_Call[0] Then Return SetError(2, 0, "") ; error encoding
    Return DllStructGetData($a, 1)
EndFunc   ;==>_Base64Encode

 

 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...