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 😃") & @CRLF)
Func ntfy_send($sMsg, $sTopicName = "Testing_AutoIt_script")
    Local $iErr, $sRet, $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("Tags", "heavy_check_mark,My script") ; Set custom headers
;~  $oHTTP.SetRequestHeader("Tags", "x") ; Set custom headers ; https://docs.ntfy.sh/emojis
    $oHTTP.SetRequestHeader("Markdown", "yes") ; Set Markdown ON
    $oHTTP.SetRequestHeader("Accept", "application/json") ; Optional: Set the Accept header to specify we expect a JSON response
    $oHTTP.Send($sMsg); Send the request
    If $oHTTP.Status = 200 Then ; Check the response status and retrieve the response text
        $iErr = 0
        $sRet = "Response: " & $oHTTP.ResponseText
    Else
        $iErr = 1
        $sRet = "Error: " & $oHTTP.Status & " - " & $oHTTP.StatusText
    EndIf
    Return SetError($iErr, $oHTTP.Status, $sRet)
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

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...