PHP: Send notifications

From Domoticz
Jump to navigation Jump to search

Purpose

Easily send notifications of various types
The complete code is available at https://github.com/Egregius/PHP-Custom-Floorplan-for-Domoticz

Dependencies

Best combined with PHP: Store switch states and values in variables

Telegram Bot

$telegrambot='113492115:AADZ-xCRhO-RBfXqICiJs8q9A_3XIr9irxI';
$telegramchatid=56975243;

function telegram($msg) {
  global $telegrambot,$telegramchatid;
  $url='https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data=array('chat_id'=>$telegramchatid,'text'=>$msg);
  $options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),);
  $context=stream_context_create($options);
  $result=file_get_contents($url,false,$context);
  return $result;
}
//Example:
telegram('This is a test message sent to Telegram bot at'.time());

Clickatell SMS

$smsuser='username';//Username at Clickatell
$smspassword='Xyz1234';//Password at Clickatell
$smsapi=1234567;//API code at Clickatell
$smstofrom=32479123456;//GSM Number used as sender/receiver of text messages
function sms($msg) {
  file_get_contents('http://api.clickatell.com/http/sendmsg?user='.$smsuser.'&password='.$smspassword.'&api_id='.$smsapi.'&to='.$smstofrom.'&text='.urlencode($msg).'&from='.$smstofrom.'');
}
//Example:
sms('This is a test SMS');

iOS

Messages sent to iOS devices using the 'Find my iPhone' service off Apple. These type of messages always make sound, even when phone is on silent or do not disturb mode.
This function needs the findmyiphone.php file https://github.com/Egregius/PHP-Custom-Floorplan-for-Domoticz/blob/master/secure/findmyiphone.php
Appledevice ID can be retrieved using findmyiphonedevices.php https://github.com/Egregius/PHP-Custom-Floorplan-for-Domoticz/blob/master/secure/findmyiphonedevices.php

$appleid='[email protected]';//AppleID email address
$applepass='Xyz_1234';//AppleID password
$appledevice='939FIdWFh845REeBs1nZk0sD/ZHxYptTlD4zoKvGC2VYH806kSRqROHYRNSUzmWV';//Apple device ID. 

function ios($msg) {
  global $appleid,$applepass,$appledevice;
  include ("findmyiphone.php");
  $fmi=new FindMyiPhone($appleid,$applepass);
  $fmi->playSound($appledevice,$msg);
  sleep(2);
}
//Example:
ios('This is a test message');

Pushover

See the pushover FAQ here: https://pushover.net/faq

function pushover($msg) {
    $url = "https://api.pushover.net/1/messages.json";
    $data = array(
       "token" => "[ Your Pushover Application Token ]",
       "user" => "[ Your Pushover User Token ]",
       "message" => $msg,
    );
    $options = array(
       'http' => array(
           'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
           'method'  => 'POST',
           'content' => http_build_query($data),
        )
    );

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
}

Pushsafer

See the Pushsafer HowTo here: https://www.pushsafer.com/en/howto

Pushsafer API description: https://www.pushsafer.com/en/pushapi

function pushsafer($m,$t,$s,$v,$i,$d,$u,$ut,$p,$l,$pr,$re,$ex,$a) {
    $url = 'https://www.pushsafer.com/api';
    $data = array(
    't' => urldecode($t),
    'm' => urldecode($m),
    's' => $s,
    'v' => $v,
    'i' => $i,
    'd' => $d,
    'u' => urldecode($u),
    'ut' => urldecode($ut),
    'l' => $l,
    'pr' => $pr,
    're' => $re,
    'ex' => $ex,
    'a' => $a,
    'p' => $p,
    'k' => 'XXXXXXXXXXXXXXXXXXXX' // private or alias key
    );
    $options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
}

//Example
pushsafer('Message','Title','5','3','23','56','https://www.pushsafer.com','Open Pushsafer','','0','2','60','600','1');