Sign in to follow this  
Followers 0
StormyNP

Using Snarl for fancy notifications via TCP/IP

2 posts in this topic

I have seen some other mention of using Snarl For Windows with APIs. Here's a simple example of how to do Snarl notifications simply by opening a TCP/IP socket to it. Nothing extra needed.

Snarl for Windows can be found here:

Snarl's Temporary Link

I apologize for the extra functions, I was already trying to design something bigger. The basics though are very simple.

This is my first posted script, constructive criticism appreciated!

If there are any Growl for Windows examples, I'd love to see some as well.

$snarl_ip = "127.0.0.1"
$snarl_port = 9887
$snarl_command_type = "type=SNP#?version=1.0#?"
$snarl_command_action_register = "action=register#?"
$snarl_command_action_add_class = "action=add_class#?"
$snarl_command_action_notification = "action=notification#?"
$snarl_command_action_unregister = "action=unregister#?"
$snarl_app = "app=AutoIt Script"
$snarl_socket = -1
$snarl_class_detail = "Detailed notifications"
$snarl_class_summary = "Summary notifications"
$snarl_class_warnings = "Warning notifications"

; Example function shows how to register app to Snarl, create a few message classes/levels, and send messages to each.
Example()

Func Example()
    If ConnectToSnarl() <> 1 Then
        Return
    EndIf
    
    ; This would be more for an initial Setup with Snarl. You only need to run this once
    ; per application you want Snarl to know about. Otherwise, no need to keep running this.
    ;RegisterWithSnarl()
    
    ; Code to unregister (or you can just unregister app from Snarl's own App settings)
    ;UnRegisterWithSnarl()
    
    ; Let's test the messages and different message classes for the app!
    ; Note, a 0 for timeout makes the notification "Sticky"
    SnarlMessage( "test 1", "Hello World!", $snarl_class_detail, 10 )
    SnarlMessage( "test 2", "Greetings!", $snarl_class_summary, 10 )
    SnarlMessage( "test 3", "w00t!", $snarl_class_warnings, 10 )
        
    Disconnect()
EndFunc

Func ConnectToSnarl()
    $rc = TCPStartup()
    If $rc <> 1 Then
        Return -1
    EndIf
    $snarl_socket = TCPConnect( $snarl_ip, $snarl_port )
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        Return -1
    EndIf
    Return 1
EndFunc

Func Disconnect()
    $rc = TCPShutdown()
EndFunc

Func SnarlMessage( $in_title, $in_text, $in_classnum, $in_timeout )
    $msg = $snarl_command_action_notification & $snarl_app & "#?class=" & $in_classnum & "#?title=" & $in_title & "#?text=" & $in_text & "#?timeout=" & $in_timeout
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Sleep( 1500 )
EndFunc


Func SendToSnarl( $in_msg )
    TCPSend( $snarl_socket, $snarl_command_type & $in_msg & @CRLF )
    If @error Then
        MsgBox( 4112, "Failure talking to Snarl", $in_msg )
        Return -1
    EndIf
    Return 1
EndFunc

Func RegisterWithSnarl()
    ; register the application so it shows in Snarl tray Apps settings
    ; I find the Sleep delay is necessary to give the Snarl registration a chance to keep up
    $msg = $snarl_command_action_register & $snarl_app
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Sleep( 1500 )
    
    ; add additional classes for the app so user has control over what messages they see
    ; I just added 3 different categories
    $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_detail
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Sleep( 1500 )
    $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_summary
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Sleep( 1500 )
    $msg = $snarl_command_action_add_class & $snarl_app & "#?class=" & $snarl_class_warnings
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Sleep( 1500 )
    
    Return 1
EndFunc

Func UnregisterWithSnarl()
    $msg = $snarl_command_action_unregister & $snarl_app
    If SendToSnarl( $msg ) <> 1 Then
        Return -1
    EndIf
    Return 1
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi there,

 

exactly what i was looking for. Til now i sent the snarl message with a runline, the run command in AutoIt and the snarl_cmd.exe.

I took your code and modified it to build the Snarl.au3, which you can include in your programs.

I attached the Snarl.au3.

In your Script include the Snarl.au3 with

#Include <Snarl.au3>

and send a message for example with

_send_snarl("192.168.0.2","d","Titletext","Messagetext",timeout)

d = detailed / s = summary / w = warning

If timeout isn´t set the default is 10 seconds

 

I hope it is useful for you

 

Snarl.au3

Edited by BOfH73

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0