2015-06-05 19:22:47 +00:00
###############################################################################
2016-01-29 19:14:55 +00:00
# $Id$
2015-06-05 19:22:47 +00:00
#
# A module to send notifications to Pushalot.
#
# written 2015 by Talkabout B <talk dot about at gmx dot de>
#
###############################################################################
#
# Definition:
# define <name> Pushalot <token> [<source>]
#
# Example:
# define PushNotification Pushalot 123234 FHEM
#
#
# You can send messages via the following command:
2015-06-06 09:50:48 +00:00
# set <Pushalot_device> message "<message>" ["<title>"] ["<image>"] ["<link>"] ["<link_title>"] [<important>] [<silent>]
2015-06-05 19:22:47 +00:00
#
# Examples:
# set PushNotification message "This is my message."
# set PushNotification message "This is my message." "With Title"
2015-06-06 09:50:48 +00:00
# set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png"
# set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com""
# set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title"
# set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title" True False
2016-02-02 19:23:13 +00:00
# set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title" True False 5
2015-06-05 19:22:47 +00:00
#
# Explantation:
#
# - The first parameter is the message to send
# - The second parameter is an optional title for the message
2015-06-06 09:50:48 +00:00
# - The third parameter is an optional image for the message
# - The fourth parameter is an optional link for the message
# - The fifth parameter is an optional link title for the message
2015-06-05 19:22:47 +00:00
# - The sixth parameter defines whether the message should be marked as important
# - The seventh parameter defines whether the message should be delivered silently
2016-02-02 19:23:13 +00:00
# - The eigth parameter defines the "time to live" in seconds for the message. After this time the message is automatically purged. Note: The Pushalot service is checking
# messages for purge every 5 minutes
2015-06-05 19:22:47 +00:00
#
# For further documentation
# https://pushalot.com/api:
package main ;
use HttpUtils ;
use utf8 ;
use JSON ;
use URI::Escape ;
my % sets = (
"message" = > 1
) ;
#------------------------------------------------------------------------------
sub Pushalot_Initialize ($$)
#------------------------------------------------------------------------------
{
my ( $ hash ) = @ _ ;
$ hash - > { DefFn } = "Pushalot_Define" ;
$ hash - > { SetFn } = "Pushalot_Set" ;
$ hash - > { AttrList } = "disable:0,1" ;
Log3 $ hash , 3 , "Pushalot initialized" ;
return undef ;
}
#------------------------------------------------------------------------------
sub Pushalot_Define ($$)
#------------------------------------------------------------------------------
{
my ( $ hash , $ def ) = @ _ ;
my @ args = split ( "[ \t]+" , $ def ) ;
if ( int ( @ args ) < 1 )
{
return "Invalid number of arguments: define <name> Pushalot <token> [<source>]" ;
}
my ( $ name , $ type , $ token , $ source ) = @ args ;
$ hash - > { STATE } = 'Initialized' ;
$ hash - > { helper } { Url } = "https://pushalot.com/api/sendmessage" ;
if ( defined ( $ token ) )
{
$ hash - > { Token } = $ token ;
}
else
{
return "Token and/or user missing." ;
}
if ( defined ( $ source ) )
{
$ hash - > { Source } = $ source ;
}
else
{
$ hash - > { Source } = '' ;
}
Log3 $ hash , 3 , "Pushalot defined for token: " . $ token ;
return undef ;
}
#------------------------------------------------------------------------------
sub Pushalot_Set ($@)
#------------------------------------------------------------------------------
{
my ( $ hash , $ name , $ cmd , @ args ) = @ _ ;
if ( ! defined ( $ sets { $ cmd } ) )
{
return "Unknown argument " . $ cmd . ", choose one of " . join ( " " , sort keys % sets ) ;
}
if ( @ args < 1 )
{
return "Argument \"message\" missing" ;
}
if ( AttrVal ( $ name , "disable" , 0 ) == 1 )
{
return "Device is disabled" ;
}
if ( $ cmd eq 'message' )
{
return Pushalot_Send ( $ hash , Pushalot_Build_Body ( $ hash , @ args ) ) ;
}
}
2015-06-06 09:50:48 +00:00
#------------------------------------------------------------------------------
2015-06-05 19:22:47 +00:00
sub Pushalot_Build_Body ($@)
2015-06-06 09:50:48 +00:00
#------------------------------------------------------------------------------
2015-06-05 19:22:47 +00:00
{
my ( $ hash , @ args ) = @ _ ;
my $ string = join ( " " , @ args ) ;
2016-02-02 19:23:13 +00:00
my @ matches = ( $ string =~ /"[^"]*"| True| False| \d+/g ) ;
2015-06-05 19:22:47 +00:00
2016-02-02 19:23:13 +00:00
my ( $ message , $ title , $ image , $ link , $ linkTitle , $ important , $ silent , $ timeToLive ) = @ matches ;
2015-06-05 19:22:47 +00:00
2016-02-02 19:23:13 +00:00
$ message =~ s/^[\s"]+|[\s"]+$//g ;
$ title =~ s/^[\s"]+|[\s"]+$//g ;
$ image =~ s/^[\s"]+|[\s"]+$//g ;
$ link =~ s/^[\s"]+|[\s"]+$//g ;
$ linkTitle =~ s/^[\s"]+|[\s"]+$//g ;
$ important =~ s/^[\s"]+|[\s"]+$//g ;
$ silent =~ s/^[\s"]+|[\s"]+$//g ;
$ timeToLive =~ s/^[\s"]+|[\s"]+$//g ;
2015-06-05 19:22:47 +00:00
if ( $ message eq "" )
{
$ message = $ string ;
}
return
"AuthorizationToken="
. $ hash - > { Token }
. "&Source=" . $ hash - > { Source }
. "&Body=" . uri_escape ( $ message )
. "&Title=" . uri_escape ( $ title )
2016-02-02 19:23:13 +00:00
. ( $ image ? "&Image=" . uri_escape ( $ image ) : "" )
. ( $ link ? "&Link=" . uri_escape ( $ link ) : "" )
. ( $ linkTitle ? "&LinkTitle=" . uri_escape ( $ linkTitle ) : "" )
2015-06-05 19:22:47 +00:00
. "&IsImportant=" . $ important
2016-02-02 19:23:13 +00:00
. "&IsSilent=" . $ silent
. "&TimeToLive=" . $ timeToLive ;
2015-06-05 19:22:47 +00:00
}
#------------------------------------------------------------------------------
sub Pushalot_Send ($$)
#------------------------------------------------------------------------------
{
my ( $ hash , $ body ) = @ _ ;
2015-06-06 09:50:48 +00:00
my $ params = {
url = > $ hash - > { helper } { Url } ,
timeout = > 10 ,
hash = > $ hash ,
data = > $ body ,
message = > $ body ,
method = > "POST" ,
callback = > \ & Pushalot_Callback
} ;
HttpUtils_NonblockingGet ( $ params ) ;
2015-06-05 19:22:47 +00:00
2015-06-06 09:50:48 +00:00
return undef ;
2015-06-05 19:22:47 +00:00
}
2015-06-06 09:50:48 +00:00
#------------------------------------------------------------------------------
sub Pushalot_Callback ($)
#------------------------------------------------------------------------------
{
my ( $ params , $ err , $ data ) = @ _ ;
my $ hash = $ params - > { hash } ;
if ( $ err ne "" )
{
$ returnObject = {
Success = > false ,
Status = > 500 ,
Description = > "Request could not be completed: " . $ err
} ;
Pushalot_Parse_Result ( $ hash , $ params - > { message } , encode_json $ returnObject ) ;
}
elsif ( $ data ne "" )
{
Pushalot_Parse_Result ( $ hash , $ params - > { message } , $ data ) ;
}
return undef ;
}
#------------------------------------------------------------------------------
2015-06-05 19:22:47 +00:00
sub Pushalot_Parse_Result ($$$)
2015-06-06 09:50:48 +00:00
#------------------------------------------------------------------------------
2015-06-05 19:22:47 +00:00
{
my ( $ hash , $ message , $ result ) = @ _ ;
my $ returnObject = decode_json $ result ;
readingsBeginUpdate ( $ hash ) ;
readingsBulkUpdate ( $ hash , "last-message-raw" , $ message ) ;
readingsBulkUpdate ( $ hash , "last-result-raw" , $ result ) ;
readingsBulkUpdate ( $ hash , "last-success" , $ returnObject - > { "Success" } ) ;
readingsBulkUpdate ( $ hash , "last-status" , $ returnObject - > { "Status" } ) ;
readingsBulkUpdate ( $ hash , "last-description" , $ returnObject - > { "Description" } ) ;
readingsEndUpdate ( $ hash , 1 ) ;
}
1 ;
###############################################################################
= pod
= begin html
< a name = "Pushalot" > </a>
<h3> Pushalot </h3>
<ul>
Pushalot is a service to receive instant push notifications on your
Windows Phone device from a variety of sources . <br>
You need an account to use this module . <br>
For further information about the service see < a href = "https://pushalot.com" target = "_blank" > pushalot . com </a> . <br>
<br>
Discuss the module < a href = "http://forum.fhem.de/index.php/topic,37775.0.html" target = "_blank" > here </a> . <br>
<br>
<br>
< a name = "PushalotDefine" > </a>
<b> Define </b>
<ul>
<code> define & lt ; name & gt ; Pushalot & lt ; token & gt ; [ & lt ; source & gt ; ] </code> <br>
<br>
<table>
<colgroup>
< col style = "width: 100px" ; " > </col>
<col> </col>
</colgroup>
<tr>
<td> & lt ; token & gt ; </td>
<td> The token that identifies a pushalot - account . You need to create if no account yet . </td>
</tr>
<tr>
<td> & lt ; source & gt ; </td>
<td> The source defines what will be shown in the 'from' - field of the message ( the sender ) . </td>
</tr>
</table>
<br>
Example:
<ul>
<code> define PushNotification Pushalot 123234 FHEM </code>
</ul>
</ul>
<br>
< a name = "PushalotSet" > </a>
<b> Set </b>
<ul>
2015-06-06 09:50:48 +00:00
<code> set & lt ; Pushalot_device & gt ; "<message>" [ "<title>" ] [ "<image>" ] [ "<link>" ] [ "<link_title>" ] [ "<important>" ] [ "<silent>" ] </code>
2015-06-05 19:22:47 +00:00
<br>
<br>
<table>
<colgroup>
< col style = "width: 100px" ; " > </col>
<col> </col>
</colgroup>
<tr>
<td> & lt ; message & gt ; </td>
<td> The message body that should appear in the message . </td>
</tr>
<tr>
<td> & lt ; title & gt ; </td>
<td> The title of the message . </td>
</tr>
2015-06-06 09:50:48 +00:00
<tr>
<td> & lt ; image & gt ; </td>
<td> An optional image URL that is shown in the message . </td>
</tr>
2015-06-05 19:22:47 +00:00
<tr>
<td> & lt ; link & gt ; </td>
<td> An optional link that should be appended to the message body . </td>
</tr>
<tr>
<td> & lt ; link_title & gt ; </td>
<td> An optional link title . If no title is set , the URL is shown as title in the message . </td>
</tr>
<tr>
<td> & lt ; important & gt ; </td>
<td> True | False: True if the message should be marked as 'important' , otherwise False ( Default ) </td>
</tr>
<tr>
<td> & lt ; silent & gt ; </td>
<td> True | False: True if the message should be delivered silently ( no notify sound is played ) , otherwise False ( Default ) </td>
</tr>
2016-02-02 19:23:13 +00:00
<tr>
<td> & lt ; time_to_live & gt ; </td>
<td> The time in minutes after which the message is automatically purged </td>
</tr>
2015-06-05 19:22:47 +00:00
</table>
<br>
Examples:
<ul>
<code> set PushNotification message "This is my message." </code> <br>
<code> set PushNotification message "This is my message." "With Title" </code> <br>
2015-06-06 09:50:48 +00:00
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" </code> <br>
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" </code> <br>
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" </code> <br>
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" True </code> <br>
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" True False </code> <br>
2016-02-02 19:23:13 +00:00
<code> set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" True False 5 </code> <br>
2015-06-05 19:22:47 +00:00
</ul>
<br>
</ul>
<br>
<b> Get </b> <ul> N /A</ ul > <br>
< a name = "PushalotAttr" > </a>
<b> Attributes </b> <ul> N /A</ ul > <br>
<ul>
</ul>
<br>
< a name = "PushalotEvents" > </a>
<b> Generated events: </b>
<ul>
N / A
</ul>
</ul>
= end html
= begin html_DE
< a name = "Pushalot" > </a>
<h3> Pushalot </h3>
<ul>
Pusalot ist ein Dienst , um Benachrichtigungen von einer vielzahl
von Quellen auf ein Windows Phone Device zu empfangen . <br>
Du brauchst einen Account um dieses Modul zu verwenden . <br>
Für weitere Informationen über den Dienst besuche < a href = "https://pushalot.com" target = "_blank" > pushalot . com </a> . <br>
<br>
Diskutiere das Modul < a href = "http://forum.fhem.de/index.php/topic,37775.0.html" target = "_blank" > hier </a> . <br>
<br>
<br>
< a name = "PushalotDefine" > </a>
<b> Define </b>
<ul>
<code> define & lt ; name & gt ; Pushalot & lt ; token & gt ; [ & lt ; source & gt ; ] </code> <br>
<br>
<table>
<colgroup>
< col style = "width: 100px" ; " > </col>
<col> </col>
</colgroup>
<tr>
<td> & lt ; token & gt ; </td>
<td> Der Token der den pushalot - Account identifiziert . Um diesen zu bekommen , muss ein Account erstellt werden . </td>
</tr>
<tr>
<td> & lt ; source & gt ; </td>
<td> Definiert den Absender , der in der Nachricht angezeigt werden soll . </td>
</tr>
</table>
<br>
Beispiel:
<ul>
<code> define Pushalot PushNotification 123234 FHEM </code>
</ul>
</ul>
<br>
< a name = "PushalotSet" > </a>
<b> Set </b>
<ul>
2015-06-06 09:50:48 +00:00
<code> set & lt ; Pushalot_device & gt ; "<message>" [ "<title>" ] [ "<image>" ] [ "<link>" ] [ "<link_title>" ] [ "<important>" ] [ "<silent>" ] </code>
2015-06-05 19:22:47 +00:00
<br>
<br>
<table>
<colgroup>
< col style = "width: 100px" ; " > </col>
<col> </col>
</colgroup>
<tr>
<td> & lt ; message & gt ; </td>
<td> Der Nachrichten - Text . </td>
</tr>
<tr>
<td> & lt ; title & gt ; </td>
<td> Der Titel der Nachricht . </td>
</tr>
2015-06-06 09:50:48 +00:00
<tr>
<td> & lt ; image & gt ; </td>
<td> Optionale Bild - URL die in der Nachricht angezeigt werden soll . </td>
</tr>
2015-06-05 19:22:47 +00:00
<tr>
<td> & lt ; link & gt ; </td>
<td> Ein optionaler Link der an die Nachricht angehängt werden soll . </td>
</tr>
<tr>
<td> & lt ; link_title & gt ; </td>
<td> Optionaler Link Titel . Wenn kein Titel angegeben wird , ist dieser die URL . </td>
</tr>
<tr>
<td> & lt ; important & gt ; </td>
<td> True | False: True wenn die Nachricht als 'wichtig' markiert werden soll , sonst False ( Default ) </td>
</tr>
<tr>
<td> & lt ; silent & gt ; </td>
<td> True | False: True wenn die Nachricht 'still' ausgeliefert werden soll ( kein Benachrichtigungssound wird abgespielt ) , ansonsten False ( Default ) </td>
</tr>
2016-02-02 19:23:13 +00:00
<tr>
<td> & lt ; time_to_live & gt ; </td>
<td> Zeit in Minuten nach der die Nachricht automatisch entfernt wird . Achtung: Der Pushalot Service prüft zu löschende Nachrichten alle 5 Minuten </td>
</tr>
2015-06-05 19:22:47 +00:00
</table>
<br>
Beispiele:
<ul>
<code> set PushNotification message "Das ist meine Nachricht." </code> <br>
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" </code> <br>
2015-06-06 09:50:48 +00:00
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" </code> <br>
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" </code> <br>
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Titel" </code> <br>
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Titel" True </code> <br>
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" True False </code> <br>
2016-02-02 19:23:13 +00:00
<code> set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" True False 5 </code> <br>
2015-06-05 19:22:47 +00:00
</ul>
<br>
Notes:
<ul>
</ul>
</ul>
<br>
<b> Get </b> <ul> N /A</ ul > <br>
< a name = "PushalotAttr" > </a>
<b> Attribute </b> <ul> N /A</ ul > <br>
<ul>
</ul>
<br>
< a name = "PushalotEvents" > </a>
<b> Generierte events: </b>
<ul>
N / A
</ul>
</ul>
= end html_DE
= cut