70_PushNotifier.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ###############################################
  2. #$Id: 70_PushNotifier.pm 11040 2016-03-10 14:42:46Z xusader $
  3. #
  4. # regex part by pirmanji
  5. #
  6. # download client-app http://pushnotifier.de/apps/
  7. # create account http://pushnotifier.de/login/
  8. #
  9. # register your app:
  10. # http://pushnotifier.de/settings/api
  11. #
  12. # Define example for all devices:
  13. # define yourname PushNotifier apiToken appname user password .*
  14. #
  15. # Define example for device group:
  16. # define yourname PushNotifier apiToken appname user password iPhone.*
  17. #
  18. # Define example for specific device:
  19. # define yourname PushNotifier apiToken appname user password iPhone5
  20. #
  21. # notify example:
  22. # define LampON notify Lamp:on set yourDefineName message Your message!
  23. #
  24. # notify with two lines:
  25. # define LampON notify Lamp:on set yourDefineName message Your message!_Second Line message
  26. #
  27. package main;
  28. use LWP::UserAgent;
  29. use Try::Tiny;
  30. sub
  31. PushNotifier_Initialize($)
  32. {
  33. my ($hash) = @_;
  34. $hash->{DefFn} = "PushNotifier_Define";
  35. $hash->{SetFn} = "PushNotifier_Set";
  36. }
  37. #####################################
  38. sub
  39. PushNotifier_Define($$)
  40. {
  41. my ($hash, $def) = @_;
  42. my @args = split("[ \t]+", $def);
  43. my ($name, $type, $apiToken, $app, $user, $passwd, $deviceID) = @args;
  44. if (! eval { qr/$deviceID/ }) {
  45. return "$deviceID is not a valid regex for <deviceID>";
  46. }
  47. if (!eval { require Try::Tiny }) {
  48. return "Perl module Try::Tiny not installed but is needed by this module. Please install it first (e.g. \"cpan -i Try::Tiny\")";
  49. }
  50. $hash->{STATE} = 'Initialized';
  51. if(defined($apiToken) && defined($app)&& defined($user)&& defined($passwd)&& defined($deviceID)) {
  52. $hash->{apiToken} = $apiToken;
  53. $hash->{app} = $app;
  54. $hash->{user} = $user;
  55. $hash->{passwd} = $passwd;
  56. $hash->{deviceID} = $deviceID;
  57. my $responseAT = LWP::UserAgent->new()->post("http://a.pushnotifier.de/1/login",
  58. ['apiToken' => $apiToken,
  59. 'username' => $user,
  60. 'password' => $passwd]);
  61. my $strg_chkAT = $responseAT->as_string;
  62. $strg_chkAT =~ m{"appToken":"([\w]+)};
  63. my $appToken = $1;
  64. $hash->{appToken} = $appToken;
  65. my $responseID = LWP::UserAgent->new()->post("http://a.pushnotifier.de/1/getDevices",
  66. ['apiToken' => $apiToken,
  67. 'appToken' => $appToken]);
  68. my $strg_chkID = $responseID->as_string;
  69. (my $devIDs = $strg_chkID) =~ s/.*\{"status":.*,"devices":\[(.*)\]\}/$1/s;
  70. $devIDs =~ s/[-"{}_]//g;
  71. $hash->{devices} = $devIDs;
  72. return undef;
  73. }
  74. }
  75. #####################################
  76. sub
  77. PushNotifier_Set($@)
  78. {
  79. my ($hash, $name, $cmd, @args) = @_;
  80. my %sets = ('message' => 1);
  81. if(!defined($sets{$cmd})) {
  82. return "Unknown argument ". $cmd . ", choose one of " . join(" ", sort keys %sets);
  83. }
  84. return PushNotifier_Send_Message($hash, @args);
  85. }
  86. #####################################
  87. sub
  88. PushNotifier_Send_Message
  89. {
  90. my $hash = shift;
  91. my $msg = join(" ", @_);
  92. $msg =~ s/\_/\n/g;
  93. my $result="";
  94. my $mc=0;
  95. try {
  96. while ($hash->{devices} =~ /title:(.*?),id:(\d+),model:(.*?)(?=,title:|$)/g) {
  97. my ($nd_title, $nd_id, $nd_model) = ("$1", "$2", "$3");
  98. # Log3 (undef, 3, "PushNotifier: Send Message $msg to device title: $nd_title, id: $nd_id, model: $nd_model");
  99. if ( $nd_id =~ m/$hash->{deviceID}/ || $nd_title =~ m/$hash->{deviceID}/ || $nd_model =~ m/$hash->{deviceID}/ ) {
  100. my $response = LWP::UserAgent->new()->post('http://a.pushnotifier.de/1/sendToDevice',
  101. ['apiToken' => $hash->{apiToken},
  102. 'appToken' => $hash->{appToken},
  103. 'app' => $hash->{app},
  104. 'deviceID' => $nd_id,
  105. 'type' => 'MESSAGE',
  106. 'content' => "$msg"]);
  107. my $error_chk = $response->as_string;
  108. $mc++;
  109. if($error_chk =~ m/"status":"ok"/) {
  110. $result.="OK! Message sent to $nd_title (id: $nd_id)\n\n$msg\n\n";
  111. }
  112. else
  113. {
  114. $result.="ERROR sending message to $nd_title (id: $nd_id)\n\nResponse:\n$error_chk\n\n";
  115. }
  116. }
  117. }
  118. };
  119. if ( !$mc ) {
  120. $result.="Regex ".$hash->{deviceID}." seems not to fit on any of your devices.";
  121. }
  122. return;
  123. }
  124. 1;
  125. ###############################################################################
  126. =pod
  127. =begin html
  128. <a name="PushNotifier"></a>
  129. <h3>PushNotifier</h3>
  130. <ul>
  131. PushNotifier is a service to receive instant push notifications on your
  132. phone or tablet from a variety of sources.<br>
  133. You need an account to use this module.<br>
  134. For further information about the service see <a href="http://www.fhemwiki.de/wiki/PushNotifier">FhemWiki PushNotifier</a>.<br>
  135. <br>
  136. Discuss the module <a href="http://forum.fhem.de/index.php/topic,25440.0.html">here</a>.<br>
  137. <br>
  138. <br>
  139. <a name="PushNotifierDefine"></a>
  140. <b>Define</b>
  141. <ul>
  142. <code>define &lt;name&gt; PushNotifier &lt;apiToken&gt; &lt;appName&gt; &lt;user&gt; &lt;password&gt; &lt;deviceID&gt;</code><br>
  143. <br>
  144. You have to create an account to get the apiToken.<br>
  145. And you have to create an application to get the appToken.<br>
  146. <br>
  147. Example:
  148. <ul>
  149. <code>define PushNotifier1 PushNotifier 01234 appname user password 012</code>
  150. </ul>
  151. </ul>
  152. <br>
  153. <a name="PushNotifierSet"></a>
  154. <b>Set</b>
  155. <ul>
  156. <code>set &lt;PushNotifier_device&gt; message</code>
  157. <br>
  158. <br>
  159. Examples:
  160. <ul>
  161. <code>set PushNotifier1 message This is a text.</code><br>
  162. </ul>
  163. Linebreak:
  164. <ul>
  165. <code>set PushNotifier1 message This is a text._New Line.</code><br>
  166. </ul>
  167. </ul>
  168. <br>
  169. <a name="PushNotifierEvents"></a>
  170. <b>Generated events:</b>
  171. <ul>
  172. N/A
  173. </ul>
  174. </ul>
  175. =end html
  176. =begin html_DE
  177. <a name="PushNotifier"></a>
  178. <h3>PushNotifier</h3>
  179. <ul>
  180. PushNotifier ist ein Dienst, um Benachrichtigungen von einer vielzahl
  181. von Quellen auf Deinem Smartphone oder Tablet zu empfangen.<br>
  182. Du brauchst einen Account um dieses Modul zu verwenden.<br>
  183. F��r weitere Informationen besuche <a href="http://www.fhemwiki.de/wiki/PushNotifier">FhemWiki PushNotifier</a>.<br>
  184. <br>
  185. Diskutiere das Modul <a href="http://forum.fhem.de/index.php/topic,25440.0.html">hier</a>.<br>
  186. <br>
  187. <br>
  188. <a name="PushNotifierDefine"></a>
  189. <b>Define</b>
  190. <ul>
  191. <code>define &lt;name&gt; PushNotifier &lt;apiToken&gt; &lt;appName&gt; &lt;user&gt; &lt;password&gt; &lt;deviceID&gt;</code><br>
  192. <br>
  193. Du musst einen Account erstellen, um das apiToken zu bekommen.<br>
  194. Und du musst eine Anwendung erstellen, um einen appToken zu bekommen.<br>
  195. <br>
  196. Beispiel:
  197. <ul>
  198. <code>define PushNotifier1 PushNotifier 01234 appname user password 012</code>
  199. </ul>
  200. </ul>
  201. <br>
  202. <a name="PushNotifierSet"></a>
  203. <b>Set</b>
  204. <ul>
  205. <code>set &lt;PushNotifier_device&gt; message </code>
  206. <br>
  207. <br>
  208. Beispiele:
  209. <ul>
  210. <code>set PushNotifier1 message Dies ist ein Text.</code><br>
  211. </ul>
  212. Zeilenumbruch:
  213. <ul>
  214. <code>set PushNotifier1 message Dies ist ein Text._Neue Zeile.</code><br>
  215. </ul>
  216. </ul>
  217. <br>
  218. <b>Get</b> <ul>N/A</ul><br>
  219. <br>
  220. <a name="PushNotifierEvents"></a>
  221. <b>Generated events:</b>
  222. <ul>
  223. N/A
  224. </ul>
  225. </ul>
  226. =end html_DE
  227. =cut