Programming Call a php webpage from a IFTTT applet using Google Assistant

I wanted to be able to wake up PC on my Lan using voice commands. To achieve that, I did implement a solution using Google Assistant, IFTTT and a php web page hosted on my NAS.

Click to Read More

Regarding IFTTT, you must have an account. Then,

  • Create a new applet,
  • Choose the service "Google Assistant"
    • Choose a trigger, ex.: "Say a phrase with a text ingredient"
    • Type a phrase in "What do you want to say?" such as "Wake up my PC $" ($ is the symbol to be used for the "ingredient")
    • Type a phrase in "What do you want the Assistant to say in response?" such as "Ok, I will wake up $"
  • Choose next an action service "Webhooks" (to "make a web request")
    • Type the url of your web page: http://<public Ip of your NAS>/<some path>/action.php?do=wakeup&param={{TextField}} ({{TextField}} will take the value of the ingredient)
    • Chose the "method": GET
    • Chose the "content type": text/plain

Regarding the php web page (action.php), I had to do some fine tuning... So, I started with a page logging all information received from IFTTT:

</pre>
<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile
{
public function execute($targetFile)
{
$boby = json_decode(file_get_contents('php://input') , true);

$data = sprintf("<h2><u>IFTTT call at %s on %s</u></h2><h3>Method:</h3>", date('h:i:s') , date('d/m/Y'));
$data .= sprintf("<blockquote>%s (%s): %s </blockquote><h3>HTTP headers:</h3><ul>", $_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'], htmlentities($_SERVER['REQUEST_URI']));
foreach ($this->getHeaderList() as $name => $value)
{
$data .= "<li><b>" . $name . '</b>: ' . $value . "</li>";
}

$data .= "</ul><h3>Post:</h3><ul>";
foreach ($_POST as $key => $value) $data .= "<li>" . $key . '=' . $value . '</li>';

$bodyhtml = $this->jsonToTable($boby);
$data .= "</ul><h3>Request body:</h3><blockquote>$bodyhtml</blockquote>";

$action = trim(strtolower($_GET['action']));
$action = preg_replace('/^ *de +/', '', $action);
$action = preg_replace('/^ *d *\' +/', '', $action);

$param = trim(strtolower($_GET['param']));
$param = preg_replace('/^ *de +/', '', $param);
$param = preg_replace('/^ *d *\' +/', '', $param);

$data .= "<h3>Requested Action:</h3><blockquote>$action: $param</blockquote>";

similar_text(strtolower($param) , "zeus", $percent);
$data .= "Zeus: $percent %<br>";

similar_text(strtolower($param) , "chaos", $percent);
$data .= "Chaos: $percent %<br>";

file_put_contents($targetFile, $data . file_get_contents($targetFile) . "\n");

echo (nl2br($data));
}

private function getHeaderList()
{
$headerList = [];
foreach ($_SERVER as $name => $value)
{
if (preg_match('/^HTTP_/', $name))
{
// convert HTTP_HEADER_NAME to Header-Name
$name = strtr(substr($name, 5) , '_', ' ');
$name = ucwords(strtolower($name));
$name = strtr($name, ' ', '-');
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}

private function jsonToTable($data)
{
$table = "<table class='json-table' width='100%'>";
foreach ($data as $key => $value)
{
$table .= "<tr valign='top'>";
if (!is_numeric($key))
{
$table .= "
<td>
<strong>" . $key . ":</strong>
</td>
<td>
";
}
else
{
$table .= "
<td colspan='2'>
";
}
if (is_object($value) || is_array($value))
{
$table .= jsonToTable($value);
}
else
{
$table .= $value;
}
$table .= "
</td>
</tr>
";
}
$table .= "</table>";
return $table;
}
}

(new DumpHTTPRequestToFile)->execute('./action.html');

?>
<pre>

The log is stored in a html page "log.html".

Notes:

  • I notice that Google was not good at recognizing the name of my PC (IMO because I use French sentences). So I am using a php command "similar_text" to evaluate the "ingredient". But this is really far from effective.
  • I tried to use a "POST" method instead of "Get" and a Content Type "application/json", it works. But when I add a Body like {“token”:”mseries”,”command”: “<<<{{TextField}}>>>”, ”test”: “data”}, I don't see it received by my php page :(
  • If Google Assistant does not call correcly IFTTT, look at you voice command history to be sure it understood you order: https://myactivity.google.com/myactivity?restrict=vaa&utm_source=help
  • If Google Assistant understood correctly your command, but IFTTT didn't call your web page, look at your IFTTT activity: https://ifttt.com/activity

A log for a call using a GET method will look like:

IFTTT call at 11:32:29 on 25/09/2018

Method: GET (HTTP/1.1): /ifttt/action.php?action=wakeup&param=CARROS

HTTP headers:

X-Newrelic-Transaction: PxQGUlECCwFRBVEEVQAAAlQTGlUDChAHHEAJVA1cAgMKA1gEVFNVUVUHFU1EAQACBV1UBlUTag==
X-Newrelic-Id: VwAOU1RRGwAFUFZUAwQE
Content-Type: text/plain
Content-Length: 74
Connection: close
Host: olympe.letroye.be
X-Real-Port: 55748
X-Port: 80
X-Real-Ip: 54.164.110.125
X-Forwarded-By: 192.168.0.200

Post:

Request body:

Requested Action: wakeup: CARROS

Zeus: 20 %
Chaos: 72.727272727273 %

A log for a call using a POST method with application/json content-type will look like:

IFTTT call at 11:41:14 on 25/09/2018

Method: POST (HTTP/1.1): /ifttt/action.php?action=wakeup&param=Kaos

HTTP headers:

X-Newrelic-Id: VwAOU1RRGwAFUFZUAwQE
Content-Type: application/json
Content-Length: 77
Connection: close
Host: olympe.letroye.be
X-Real-Port: 59892
X-Port: 80
X-Real-Ip: 54.211.16.53
X-Forwarded-By: 192.168.0.200

Post:

Request body:

Requested Action: wakeup: kaos

Zeus: 25 %
Chaos: 66.666666666667 %

Tips Use a VPN between an Android mobile and a Synology NAS via Proximus network

I spent hours trying to figure out why I couldn't connect my Galaxy S7 onto my Synology using a PPTP VPN on the Proximus 3G/4G Network (Belgium) while it was working fine if connected via WiFi.

Finally, I gave up and configured a L2TP/IPSec VPN. This one worked immediately like a charm, both on WiFi and 3G/4G. I am quite sure this is due to Proximus configuring my profile to use CGN (Carrier Grade NAT).

Click to Read More

I was initially inspired by this post ("Using Synology DiskStation as a VPN server using built-in Android VPN client"). So;

  • I did first configure my Synology VPN Server to use PPTP as explained here (Official Knowledge base Synology)
  • Next, I did open my Synology Firewall (port 1723 for PPTP) via the Control Panel > Security > Firewall > Edit Rules > Edit an entry to Allow > Select Built-in applications > VPN Server (PPTP)

Open Firewall Synology for VPN

  • Then, I did configure my Modem to route the TCP port 1723 to my NAS via Access Control > Port Mapping

Configure Proximus Modem Route

Configure Proximus Modem Route

  • Finally, I configured a PPTP VPN on my Galaxy S7 via Settings > Connections > More connection settings > VPN > ADD VPN

It worked immediately while connecting my Galaxy S7 via WiFi. But if failed while connected via 3G/4G. To investigate, I did enable the firewall of my dd-wrt router (placed between my modem and my NAS) and configured it to send all its firewall messages to the syslog server of my Synology (the "Log Center"). I those logs, I found packets corresponding most probably to the VPN requests from my Galaxy S7 (They were only arriving when I was trying to establish a connection). Those were not coming from my Galaxy S7's IP. Instead, they were coming from another Proximus IP, with a protocol 47 (Generic Route Encapsulation)?! Could this be due to Proximus configuring my profile to use CGN (Carrier Grade NAT)? Unfortunately, I couldn't check and reconfigure this myself as the option is not available anymore via the MyProximus administration interface...

So, I decided to give up with this PPTP VPN and use a more secure but less performant VPN: L2TP/IPSEC with a PSK (IPsec pre-shared key). And miracle, after opening and routing the UDP ports 500, 1701 and 4500, it worked !

Synology Plex Media Server not matching/downloading metadata anymore

Since a few days, My Plex Media Server, hosted on my Synology, was not able to match movies anymore. And when matching movies manually, it was sometimes downloading the covers but never the metadata. Deleting the Service plugin solved the problem.

Click to Read More

I read that IPV6 could be the cause of such a problem, and that turning it off on the Synloloy was the solution. I tried that but without success.

Was solved my problem was to:

  1. Stop Plex via the DSM's Package Manager
  2. Log in my NAS as an administrator, in a SSH console, via Putty and enter root mode.
  3. Enter Plex's Plugin folder using a command like: cd /volume1/Plex/Library/Application\ Support/Plex\ Media\ Server/Plug-ins/
  4. Delete the service plugin:  rm -R Services.bundle
  5. Restart Plex via the DSM's Package Manager

SynologyTips Synology: "failed to install '...'. This package is not published by Synology Inc,

I got the following message when trying to install third parties packages on my Synology: "failed to install '...'. This package is not published by Synology Inc,

The solution is simply to Trust Any Publisher.

Click to Read More

Go into the "Package Center" > Settings > General tab and select "Any Publisher" in the section "Trust Level"

Trust Any Publisher

Trust Any Publisher

Synology Synology Package Creator for shell scripts and php pages

I did upgrade "Mods", my Windows Tool used to create dummy packages adding shortcuts on DSM's desktop to open web pages.

Now, "Mods" is able to package shell scripts and php webpages...

Mods

Mods

Click to Read More

It is available here on GitHub.

For example, you could package apps like Chris Hager's Most Simple Ajax Chat Ever

Simple Chat on DSM

Simple Chat on DSM

Or package a more advanced tool like php Server Monitor

Server Monitor

Server Monitor

Server Monitor Running

Server Monitor Running

This Tool is available here on GitHub.

SynologyTips Send Custom Notifications from scripts running on a Synology [new]

This is an update of a old post written for DSM 4 and 5: how to use the native Notification Mechanism of Synology to sent custom notifications. It's now tested on my DS1815+ with DSM 6.0.

Click to Read More

First, define the Notifications to be sent:

  1. Open a dos command prompt, and execute: telnet <YourSynoHostname>
    1. As an alternative, I am using the free telnet client "Putty" to open SSH Console.
  2. Login as administrator
  3. Once connected via telnet, enter the root mode with the command 'sudo -i', using here also the password of your administrator
  4. Execute next: cd /usr/syno/synoman/webman/texts/
  5. Execute: ls
  6. Now you see a list of folders for each language supported by your Synology. You must at least defined your custom notifications in the 'enu' version and next the version that match your language.
  7. Copy the file with the definition of the existing notifications in a shared folder to edit it easily: cp enu/mails /volume1/web/
    1. I presume here that the web station is enabled, otherwise, copy the file 'mails' in any other shared folder.
    2. As an alternative, you can use the Package "Config File Editor" to edit "/usr/syno/synoman/webman/texts/enu/mails"
  8. Edit now this file 'mails' (e.g.: with notepad++: \\<yourSynoHostname>\web\mails)
  9. Add your new custom "tags" at the end of the file with an adequate definition (see bellow for an example). You must specify a Title, a Subject, the text, and a Category with its priority. The category is used to group the Notifications in the DSM > Control Panel > Notification > Advanced Tab. For each Category, specify one of the possible values with a "Priority", separated by a coma. The priority is used to filter the notifications in the "Advanced" tab (Look at existing tags in the file for illustration):
    1. Possible values for "Category"
      1. Backup/Restore: group under 'Backup and Restoration' in the "Advanced" tab
      2. CMS
      3. External Storage
      4. HA: grouped under 'High-availability cluster'
      5. AHA: not displayed in the "Advanced" tab but related to External Storage
      6. Storage: grouped under 'Internal Storage'
      7. USB Copy: not displayed in the "Advanced" tab but related to External Storage
      8. Power System: grouped under 'Power supply'
      9. System
      10. PerfEvent: not displayed in the "Advanced" tab but related to System
    2. Possible values for "Priority"
      1. Important: displayed as Critical
      2. (no value): displayed as Informative
        Synology Notifications

        Synology Notifications

  10. Save your changes (E.g.: back to the telnet console, execute: cp /volume1/web/mails enu/mails)

Examples:

[MySqlBkpError] Category: Backup/Restore,Important
Title: Database backup failed
Subject: Database backup on %HOSTNAME% has failed

Dear User,

Database backup on %HOSTNAME% has failed.

[MySqlBkpOK] Category: Backup/Restore
Title: Database backup completed
Subject: Database backup on %HOSTNAME% has succeeded

Dear User,

Database backup on %HOSTNAME% has successfully completed.

Next, define how to sent those custom Notifications if and only if they are in a Category which does not appear in the "Advanced" tab of the "Control Panel" > "Notifications" pane > "Advanced" tab (Ex.: the categoryPerfEvent). It's indeed via that Tab that you should to enable  the desired delivery modes: "email", "sms", .... which is obviously only possible for Notifications in a Category which is displayed.

  1. Go back to the telnet console and execute: cp /usr/syno/etc/notification/notification_filter.settings /volume1/web/

    1. As an alternative, you can use the Package "Config File Editor" to edit "/usr/syno/etc/notification/notification_filter.settings"
  2. Edit the file 'notification_filter.settings' (E.g. with notepad++)
  3. At the end of that file, insert a configuration line for each custom tag, specifying how the notification must be sent (Look at the other line for illustrations). Possible delivery mode are:
    1. mail
    2. sms
    3. mobile
    4. cms: this is the native popup notification mechanism of DSM, enabled by default for all notifications
    5. msn: not supported anymore since 17th July 2014
    6. skype: not supported anymore since 17th July 2014
  4. Copy the file back to its original location: cp /volume1/web/notification_filter.settings /usr/syno/etc/

Examples:

MySqlBkpOK="mail"

MySqlBkpError="mail"

Now, reboot your Synology

It seems that in the past, I was able to apply my changes in those files without a reboot. But while testing this trick on DSM 6.x, I didn't find how to do it without a reboot.

Finally, modify your script to sent your notifications:

  1. Edit your script (E.g. with notepad++).
  2. Add where required the following command: /usr/syno/bin/synonotify <YourCustomTag>
  3. Save your changes.

In the example here after, I sent notifications depending on the success or failure of the previous command:

if [ $? -eq 0 ] then /usr/syno/bin/synonotify MySqlBkpOK
else /usr/syno/bin/synonotify MySqlBkpError
fi

Finally, check that the "Notifications" are enabled on your Synology and tick options like emails, SMS, ... for the new tags if required:

  1. In your DSM, go to “Control Panel” > “Notification” > “E-Mail”
  2. Tick the option “Enable e-mail notifications”.
  3. Complete all the fields in that tab. At least: SMTP server, SMTP port, SMTP authentication and Primary email.
  4. Send also a test mail from this tab to verify your settings.
  5. Go next to the "Advanced" tab
  6. Tick the "E-mail" option (and possibly others) for the new tags which appear now in the list.
    1. If the tags do not appear, close and reopen the control panel. restart your DSM!
    2. The tags appear with their Title under their Category
Notifications Configuration

Notifications Configuration

And here are the outcomes: a popup message and an email

Database Backup Email

Database Backup Email

Database Backup Custom Notification

Database Backup Custom Notification

Pay attention that the tags are case sensitive ! Also backup the changes you made in the file 'mails' and 'notification_category' as they will be overwritten for sure when you will update the DSM... Finally, delete the files 'mails' and 'notification_category' from your web folder...

Notice: the user EConceptApplications suggested on Synology Forum to use a script as here under to automatically reinsert the notifications

grep -q "MySqlBkpOK" /usr/syno/etc/notification/notification_filter.settings
if [ $? -ne 0 ] then

echo MySqlBkpOK="mail" >> /usr/syno/etc/notification/notification_filter.settings

fi

Notice: a reader, k13tas, suggested that it was much easier to reuse an existing notification tag and customize its text via Control Panel > Notification > Advanced. Indeed, those changes are not lost after a DSM update.

Notice: a reader, Rusmin, posted here that he found how to pass parameters to be used  in the notification message, via a hash json string. In his sample, one passes the value "value1" via a variable named %VAR1%. The value can obviously come from any variable of your shell script. Ex.: $PPID, $MyVariable, $$, etc...

Usage: /usr/syno/bin/synonotify tag_event [hash_json_string]

/usr/syno/bin/synonotify Tag_Name '{"[%VAR1%]": "value1", "[%VAR2%]": "value2"}'

As creating manually custom notifications is far from ideal, please vote for my feature request on Synology's forumhttp://forum.synology.com/enu/viewtopic.php?f=3&t=92727

Tips Cannot access port 80 on my NAS from Internet

From time to time, I can't access any web page anymore on my NAS' port 80 from the web. The solution is to reboot the my modem (BBOX2) or reset its internal firewall. It's never an issue with my NAS (Synology DS1815+)

Click to Read More

When this issue occurs, I can still access web pages on specific port like Plex  (e.g.: on port 32000) or my NAS' admin console on his own port (e.g.: on port 8080). Also the "Cloud Sync" service of my NAS is still  able to sync files with Google, DropBox and Hubic. Finally, JDownloader which is installed also installed on my NAS, is still able to download.

To reboot my modem takes time but solve the problem. Another option is to reset the firewall:

  • open a telnet sessin on the modem,
  • log as 'admin' with your password (I did change mine. The default is possibly BGCVDSL2 or your MAC Address)
  • execute: fw_restart
fw_restart BBOX2

fw_restart BBOX2

The problem comes from a NAT table full, most probably due to JDownloader or Cloud Sync. But I don't know how to check which one is the culprit.

Info about B-BOX-2 of Belgacom/Proximus: http://setuprouter.com/router/belgacom/b-box-2/

Tips Synolody DSM update: Insufficient free disk space to upgrade

Since I have migrated from my DS713+ to a DS1815+, I can't update seamlessly my DSM. Most of the time, my NAS pretends that I have "Not enough storage space". This problem is due to large log files filling my system partition.

Click to Read More

After the migration top my new NAS, I have reinstalled JDownloader. This one is using a script to run a java application via "nohup". Nohup's log is growing fast and takes a lot of storage, preventing my NAS to download the DSM updates.

I found it was the culprit via an 'iterative' procedure suggested by maxxfi on the forum of Synology:

Use your admin password. Once authenticated as root, type:

  • cd /
  • du -ks [a-u]*

That command will give you for each subdirectory whose name begins with a letter from a to u (so to avoid the volumeX) the total amount of disk space in use. Wait that it finishes and search for which directory shows the biggest numbers, and type:

  • cd <directory>
  • du -ks *

Repeat the analysis going deeper into the directory tree until you are able to pinpoint the culprit. Finally, use the appropriate "rm <filenames>" command to delete the largest folder/files.

In my case, the largest file was /root/nohup.out

But in the past , I also had to deleted /var/log or /var/log/messages and reboot prior updating the DSM.

In a Putty session, to know the free space in the system partition, type: df -k

Tips Expand storage by migrating disks from Synology DS713+ to DS1815+

The 2x3TB disks of my DS713+ were full. Being lazy, I decided to expand the storage by migrating the disk to a new DS1815+ with more bays (instead of doing a full backup and restore on larger disks). It was a piece of cake!

Click to Read More

  1. Do first a backup of the configuration of your current Synology via Control Panel > Update & Restore menu > Configuration Backup tab > Back up configuration button
  2. Move the disks to the new Synology, keeping the very same order (disk in bay n must go into the bay n of the new Synology).
  3. Add a few other disks in the new Synology and create new Volumes
  4. Boot the new Synology and wait for the beep.
  5. Start the Synology Assistant, find your new Synology and click "Connect" on it.
  6. Use the Migration mode, keeping all your files. NB.: you should download the latest DSM for your new Synology and use that local copy during the installation process instead of letting the installation process download that file itself.
  7. Restore the Configuration backup on the new Synology
  8. If you were using the web station and php, enable it. This settings was lost in my case: Control Panel > Web Services menu > Web Services tab > Enable Web Station option
  9. Repair the packages one by one. Wait for each repair to be successful before repairing the next package otherwise you could be in troubles. Pay attention also that the behaviour of the UI can be weird when auto-refreshing after a repair. If clicking repair on a package, you see that the package repaired is not the one you clicked on (but one that was already repaired), close and reopen the Package Center. All my packages where successfully repaired (sometimes I had to skip one and retry later, IMO due to the dependencies...). Here are the packages I had to repair
    1. Audio Station. everything kept as-is, including the user-defined radio
      1. if music does not appear, go to Control Panel > Media Indexing and re-index the media files.
    2. Download Station: didn't lose anything from its download queue
    3. Plex Media Server. I simply had to reenter my credentials which were expired
    4. DNS Server: all settings/zones still defined. Access to all services from intranet and internet working fine
    5. Maria DB: nothing lost. In particular my Worpress blog.
    6. Media Server: ok
    7. Video Station: all video and metadata accessible
    8. Photo Station: all albums still there and ok
    9. Python 3: I had to retry the repair but works fine
    10. Java Manager: ok (used to install Java 7)
    11. Surveillance Station: Licenses, settings, camera, recordings are all kept
    12. Time Backup: I had to retry the repair but backup tasks are ok, as wel as the timeline itself.
    13. Node.js
  10. Other Packages:
    1. My Custom packages were still there: iPKGui, AcpiOnLan, Mods (My Own Dsm Shorcuts), Unofficial Java Installer (Java 8)
    2. Custom Package Sources are still defined too
    3. Jdownloader was still installed and configured and I only had to restart it.
    4. Settings were lost for FileBot Node. After reconfiguring FileBot Node, it was not working (unable to access its onw port) I had to uninstall it and reinstall (including FileBot). Reconfigured settings were however not lost...
    5. WordPress was stopped. My blog was however well still accessible as I did reenable the Web Station
  11. Synology's DDNS Service
    1. DDNS Service being linked to the old Synology, I had to go to https://account.synology.com/fr-fr/overview and unlink my DDNS name
    2. Next, I went to  Control Panel > External Access and did Edit my provider (Synology) to re-enabled the DDNS and re-introduced my login/password.
    3. It took a few minutes before being again connected.
  12. I finally moved a few Shared Folders to the new volumes by editing them via Control Panel > Shared Folder menu > Edit. I simply changed the location, picking the new Volume as a target. Attention, during the move, the Control Panel is not accessible anymore. If you need to access it, simply open a new browser ;) (The only thing that cannot be done, is moving two Shared Folders simultaneously). Notice that the move can take ages as the new volume is probably still under validation (depends if you checked that option when you created the volume).

Et voilà.

Tips Netgear JGS524E's 802.3ad Link Aggregation And Synology

Long time ago, I wanted to configured a Bond on my Synology to benefit the LAG feature of my Switch JGS524E (Netgear ProSafe® Plus). Unfortunately, it didn't work. Being lazy, I never investigated further. It's now done: I wrote to Netgear and they confirmed that only Static LAG is supported on JGS524E. Dynamic LAG, required to use Link Aggregation with Synology, is not.

Click to Read More

Concretely, Synology says in documentation that it requires IEEE 802.3ad (Dynamic) Link Aggregation (LACP, 802.1AX). While Netgear told me that the Switch ProSafe® Plus only supports static LAG. And that's how I learned that static LAG is not LACP (Link Aggregation Control Protocol) :/

Conclusion I did configure my Synology to use Adaptive Load Balancing and I did disable LAG on my Netgear...