70_Pushalot.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. ###############################################################################
  2. # $Id: 70_Pushalot.pm 10699 2016-02-02 19:23:13Z talkabout $
  3. #
  4. # A module to send notifications to Pushalot.
  5. #
  6. # written 2015 by Talkabout B <talk dot about at gmx dot de>
  7. #
  8. ###############################################################################
  9. #
  10. # Definition:
  11. # define <name> Pushalot <token> [<source>]
  12. #
  13. # Example:
  14. # define PushNotification Pushalot 123234 FHEM
  15. #
  16. #
  17. # You can send messages via the following command:
  18. # set <Pushalot_device> message "<message>" ["<title>"] ["<image>"] ["<link>"] ["<link_title>"] [<important>] [<silent>]
  19. #
  20. # Examples:
  21. # set PushNotification message "This is my message."
  22. # set PushNotification message "This is my message." "With Title"
  23. # set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png"
  24. # set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com""
  25. # set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title"
  26. # set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title" True False
  27. # set PushNotification message "This is my message." "With Title" "http://www.xyz/image.png" "http://www.xyz.com" "Link Title" True False 5
  28. #
  29. # Explantation:
  30. #
  31. # - The first parameter is the message to send
  32. # - The second parameter is an optional title for the message
  33. # - The third parameter is an optional image for the message
  34. # - The fourth parameter is an optional link for the message
  35. # - The fifth parameter is an optional link title for the message
  36. # - The sixth parameter defines whether the message should be marked as important
  37. # - The seventh parameter defines whether the message should be delivered silently
  38. # - 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
  39. # messages for purge every 5 minutes
  40. #
  41. # For further documentation
  42. # https://pushalot.com/api:
  43. package main;
  44. use HttpUtils;
  45. use utf8;
  46. use JSON;
  47. use URI::Escape;
  48. my %sets = (
  49. "message" => 1
  50. );
  51. #------------------------------------------------------------------------------
  52. sub Pushalot_Initialize($$)
  53. #------------------------------------------------------------------------------
  54. {
  55. my ($hash) = @_;
  56. $hash->{DefFn} = "Pushalot_Define";
  57. $hash->{SetFn} = "Pushalot_Set";
  58. $hash->{AttrList} = "disable:0,1";
  59. Log3 $hash, 3, "Pushalot initialized";
  60. return undef;
  61. }
  62. #------------------------------------------------------------------------------
  63. sub Pushalot_Define($$)
  64. #------------------------------------------------------------------------------
  65. {
  66. my ($hash, $def) = @_;
  67. my @args = split("[ \t]+", $def);
  68. if (int(@args) < 1)
  69. {
  70. return "Invalid number of arguments: define <name> Pushalot <token> [<source>]";
  71. }
  72. my ($name, $type, $token, $source) = @args;
  73. $hash->{STATE} = 'Initialized';
  74. $hash->{helper}{Url} = "https://pushalot.com/api/sendmessage";
  75. if(defined($token))
  76. {
  77. $hash->{Token} = $token;
  78. }
  79. else
  80. {
  81. return "Token and/or user missing.";
  82. }
  83. if(defined($source))
  84. {
  85. $hash->{Source} = $source;
  86. }
  87. else
  88. {
  89. $hash->{Source} = '';
  90. }
  91. Log3 $hash, 3, "Pushalot defined for token: " . $token;
  92. return undef;
  93. }
  94. #------------------------------------------------------------------------------
  95. sub Pushalot_Set($@)
  96. #------------------------------------------------------------------------------
  97. {
  98. my ($hash, $name, $cmd, @args) = @_;
  99. if (!defined($sets{$cmd}))
  100. {
  101. return "Unknown argument " . $cmd . ", choose one of " . join(" ", sort keys %sets);
  102. }
  103. if (@args < 1)
  104. {
  105. return "Argument \"message\" missing";
  106. }
  107. if (AttrVal($name, "disable", 0 ) == 1)
  108. {
  109. return "Device is disabled";
  110. }
  111. if ($cmd eq 'message')
  112. {
  113. return Pushalot_Send($hash, Pushalot_Build_Body($hash, @args));
  114. }
  115. }
  116. #------------------------------------------------------------------------------
  117. sub Pushalot_Build_Body($@)
  118. #------------------------------------------------------------------------------
  119. {
  120. my ($hash, @args) = @_;
  121. my $string = join(" ", @args);
  122. my @matches = ($string =~ /"[^"]*"| True| False| \d+/g);
  123. my ($message, $title, $image, $link, $linkTitle, $important, $silent, $timeToLive) = @matches;
  124. $message =~ s/^[\s"]+|[\s"]+$//g;
  125. $title =~ s/^[\s"]+|[\s"]+$//g;
  126. $image =~ s/^[\s"]+|[\s"]+$//g;
  127. $link =~ s/^[\s"]+|[\s"]+$//g;
  128. $linkTitle =~ s/^[\s"]+|[\s"]+$//g;
  129. $important =~ s/^[\s"]+|[\s"]+$//g;
  130. $silent =~ s/^[\s"]+|[\s"]+$//g;
  131. $timeToLive =~ s/^[\s"]+|[\s"]+$//g;
  132. if ($message eq "")
  133. {
  134. $message = $string;
  135. }
  136. return
  137. "AuthorizationToken="
  138. . $hash->{Token}
  139. . "&Source=" . $hash->{Source}
  140. . "&Body=" . uri_escape($message)
  141. . "&Title=" . uri_escape($title)
  142. . ($image ? "&Image=" . uri_escape($image) : "")
  143. . ($link ? "&Link=" . uri_escape($link) : "")
  144. . ($linkTitle ? "&LinkTitle=" . uri_escape($linkTitle) : "")
  145. . "&IsImportant=" . $important
  146. . "&IsSilent=" . $silent
  147. . "&TimeToLive=" . $timeToLive;
  148. }
  149. #------------------------------------------------------------------------------
  150. sub Pushalot_Send($$)
  151. #------------------------------------------------------------------------------
  152. {
  153. my ($hash,$body) = @_;
  154. my $params = {
  155. url => $hash->{helper}{Url},
  156. timeout => 10,
  157. hash => $hash,
  158. data => $body,
  159. message => $body,
  160. method => "POST",
  161. callback => \&Pushalot_Callback
  162. };
  163. HttpUtils_NonblockingGet($params);
  164. return undef;
  165. }
  166. #------------------------------------------------------------------------------
  167. sub Pushalot_Callback($)
  168. #------------------------------------------------------------------------------
  169. {
  170. my ($params, $err, $data) = @_;
  171. my $hash = $params->{hash};
  172. if($err ne "")
  173. {
  174. $returnObject = {
  175. Success => false,
  176. Status => 500,
  177. Description => "Request could not be completed: " . $err
  178. };
  179. Pushalot_Parse_Result($hash, $params->{message}, encode_json $returnObject);
  180. }
  181. elsif($data ne "")
  182. {
  183. Pushalot_Parse_Result($hash, $params->{message}, $data);
  184. }
  185. return undef;
  186. }
  187. #------------------------------------------------------------------------------
  188. sub Pushalot_Parse_Result($$$)
  189. #------------------------------------------------------------------------------
  190. {
  191. my ($hash, $message, $result) = @_;
  192. my $returnObject = decode_json $result;
  193. readingsBeginUpdate($hash);
  194. readingsBulkUpdate($hash, "last-message-raw", $message);
  195. readingsBulkUpdate($hash, "last-result-raw", $result);
  196. readingsBulkUpdate($hash, "last-success", $returnObject->{"Success"});
  197. readingsBulkUpdate($hash, "last-status", $returnObject->{"Status"});
  198. readingsBulkUpdate($hash, "last-description", $returnObject->{"Description"});
  199. readingsEndUpdate($hash, 1);
  200. }
  201. 1;
  202. ###############################################################################
  203. =pod
  204. =begin html
  205. <a name="Pushalot"></a>
  206. <h3>Pushalot</h3>
  207. <ul>
  208. Pushalot is a service to receive instant push notifications on your
  209. Windows Phone device from a variety of sources.<br>
  210. You need an account to use this module.<br>
  211. For further information about the service see <a href="https://pushalot.com" target="_blank">pushalot.com</a>.<br>
  212. <br>
  213. Discuss the module <a href="http://forum.fhem.de/index.php/topic,37775.0.html" target="_blank">here</a>.<br>
  214. <br>
  215. <br>
  216. <a name="PushalotDefine"></a>
  217. <b>Define</b>
  218. <ul>
  219. <code>define &lt;name&gt; Pushalot &lt;token&gt; [&lt;source&gt;]</code><br>
  220. <br>
  221. <table>
  222. <colgroup>
  223. <col style="width: 100px";"></col>
  224. <col></col>
  225. </colgroup>
  226. <tr>
  227. <td>&lt;token&gt;</td>
  228. <td>The token that identifies a pushalot-account. You need to create if no account yet.</td>
  229. </tr>
  230. <tr>
  231. <td>&lt;source&gt;</td>
  232. <td>The source defines what will be shown in the 'from'-field of the message (the sender).</td>
  233. </tr>
  234. </table>
  235. <br>
  236. Example:
  237. <ul>
  238. <code>define PushNotification Pushalot 123234 FHEM</code>
  239. </ul>
  240. </ul>
  241. <br>
  242. <a name="PushalotSet"></a>
  243. <b>Set</b>
  244. <ul>
  245. <code>set &lt;Pushalot_device&gt; "&lt;message&gt;" ["&lt;title&gt;"] ["&lt;image&gt;"] ["&lt;link&gt;"] ["&lt;link_title&gt;"] ["&lt;important&gt;"] ["&lt;silent&gt;"]</code>
  246. <br>
  247. <br>
  248. <table>
  249. <colgroup>
  250. <col style="width: 100px";"></col>
  251. <col></col>
  252. </colgroup>
  253. <tr>
  254. <td>&lt;message&gt;</td>
  255. <td>The message body that should appear in the message.</td>
  256. </tr>
  257. <tr>
  258. <td>&lt;title&gt;</td>
  259. <td>The title of the message.</td>
  260. </tr>
  261. <tr>
  262. <td>&lt;image&gt;</td>
  263. <td>An optional image URL that is shown in the message.</td>
  264. </tr>
  265. <tr>
  266. <td>&lt;link&gt;</td>
  267. <td>An optional link that should be appended to the message body.</td>
  268. </tr>
  269. <tr>
  270. <td>&lt;link_title&gt;</td>
  271. <td>An optional link title. If no title is set, the URL is shown as title in the message.</td>
  272. </tr>
  273. <tr>
  274. <td>&lt;important&gt;</td>
  275. <td>True|False: True if the message should be marked as 'important', otherwise False (Default)</td>
  276. </tr>
  277. <tr>
  278. <td>&lt;silent&gt;</td>
  279. <td>True|False: True if the message should be delivered silently (no notify sound is played), otherwise False (Default)</td>
  280. </tr>
  281. <tr>
  282. <td>&lt;time_to_live&gt;</td>
  283. <td>The time in minutes after which the message is automatically purged</td>
  284. </tr>
  285. </table>
  286. <br>
  287. Examples:
  288. <ul>
  289. <code>set PushNotification message "This is my message."</code><br>
  290. <code>set PushNotification message "This is my message." "With Title"</code><br>
  291. <code>set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png"</code><br>
  292. <code>set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com"</code><br>
  293. <code>set PushNotification message "This is my message." "With Title" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Title" </code><br>
  294. <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>
  295. <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>
  296. <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>
  297. </ul>
  298. <br>
  299. </ul>
  300. <br>
  301. <b>Get</b> <ul>N/A</ul><br>
  302. <a name="PushalotAttr"></a>
  303. <b>Attributes</b> <ul>N/A</ul><br>
  304. <ul>
  305. </ul>
  306. <br>
  307. <a name="PushalotEvents"></a>
  308. <b>Generated events:</b>
  309. <ul>
  310. N/A
  311. </ul>
  312. </ul>
  313. =end html
  314. =begin html_DE
  315. <a name="Pushalot"></a>
  316. <h3>Pushalot</h3>
  317. <ul>
  318. Pusalot ist ein Dienst, um Benachrichtigungen von einer vielzahl
  319. von Quellen auf ein Windows Phone Device zu empfangen.<br>
  320. Du brauchst einen Account um dieses Modul zu verwenden.<br>
  321. Für weitere Informationen über den Dienst besuche <a href="https://pushalot.com" target="_blank">pushalot.com</a>.<br>
  322. <br>
  323. Diskutiere das Modul <a href="http://forum.fhem.de/index.php/topic,37775.0.html" target="_blank">hier</a>.<br>
  324. <br>
  325. <br>
  326. <a name="PushalotDefine"></a>
  327. <b>Define</b>
  328. <ul>
  329. <code>define &lt;name&gt; Pushalot &lt;token&gt; [&lt;source&gt;]</code><br>
  330. <br>
  331. <table>
  332. <colgroup>
  333. <col style="width: 100px";"></col>
  334. <col></col>
  335. </colgroup>
  336. <tr>
  337. <td>&lt;token&gt;</td>
  338. <td>Der Token der den pushalot-Account identifiziert. Um diesen zu bekommen, muss ein Account erstellt werden.</td>
  339. </tr>
  340. <tr>
  341. <td>&lt;source&gt;</td>
  342. <td>Definiert den Absender, der in der Nachricht angezeigt werden soll.</td>
  343. </tr>
  344. </table>
  345. <br>
  346. Beispiel:
  347. <ul>
  348. <code>define Pushalot PushNotification 123234 FHEM</code>
  349. </ul>
  350. </ul>
  351. <br>
  352. <a name="PushalotSet"></a>
  353. <b>Set</b>
  354. <ul>
  355. <code>set &lt;Pushalot_device&gt; "&lt;message&gt;" ["&lt;title&gt;"] ["&lt;image&gt;"] ["&lt;link&gt;"] ["&lt;link_title&gt;"] ["&lt;important&gt;"] ["&lt;silent&gt;"]</code>
  356. <br>
  357. <br>
  358. <table>
  359. <colgroup>
  360. <col style="width: 100px";"></col>
  361. <col></col>
  362. </colgroup>
  363. <tr>
  364. <td>&lt;message&gt;</td>
  365. <td>Der Nachrichten-Text.</td>
  366. </tr>
  367. <tr>
  368. <td>&lt;title&gt;</td>
  369. <td>Der Titel der Nachricht.</td>
  370. </tr>
  371. <tr>
  372. <td>&lt;image&gt;</td>
  373. <td>Optionale Bild-URL die in der Nachricht angezeigt werden soll.</td>
  374. </tr>
  375. <tr>
  376. <td>&lt;link&gt;</td>
  377. <td>Ein optionaler Link der an die Nachricht angehängt werden soll.</td>
  378. </tr>
  379. <tr>
  380. <td>&lt;link_title&gt;</td>
  381. <td>Optionaler Link Titel. Wenn kein Titel angegeben wird, ist dieser die URL.</td>
  382. </tr>
  383. <tr>
  384. <td>&lt;important&gt;</td>
  385. <td>True|False: True wenn die Nachricht als 'wichtig' markiert werden soll, sonst False (Default)</td>
  386. </tr>
  387. <tr>
  388. <td>&lt;silent&gt;</td>
  389. <td>True|False: True wenn die Nachricht 'still' ausgeliefert werden soll (kein Benachrichtigungssound wird abgespielt), ansonsten False (Default)</td>
  390. </tr>
  391. <tr>
  392. <td>&lt;time_to_live&gt;</td>
  393. <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>
  394. </tr>
  395. </table>
  396. <br>
  397. Beispiele:
  398. <ul>
  399. <code>set PushNotification message "Das ist meine Nachricht."</code><br>
  400. <code>set PushNotification message "Das ist meine Nachricht." "Mit Titel"</code><br>
  401. <code>set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png"</code><br>
  402. <code>set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com"</code><br>
  403. <code>set PushNotification message "Das ist meine Nachricht." "Mit Titel" "http://www.xyz.com/image.png" "http://www.xyz.com" "Link Titel" </code><br>
  404. <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>
  405. <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>
  406. <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>
  407. </ul>
  408. <br>
  409. Notes:
  410. <ul>
  411. </ul>
  412. </ul>
  413. <br>
  414. <b>Get</b> <ul>N/A</ul><br>
  415. <a name="PushalotAttr"></a>
  416. <b>Attribute</b> <ul>N/A</ul><br>
  417. <ul>
  418. </ul>
  419. <br>
  420. <a name="PushalotEvents"></a>
  421. <b>Generierte events:</b>
  422. <ul>
  423. N/A
  424. </ul>
  425. </ul>
  426. =end html_DE
  427. =cut