<b style="color:red">This document is not up-to-date, please refer to [the Japanese document](https://docs.enebular.com/ja/).</b>

# Device status change notification via Webhook {#Webhook}

## Overview {#Overview}

Functionality is being provided to notify via Webhook when there is a change in the connection status of a device ([Device Status](./DeviceList.md#DeviceStatus)).Webhook are a mechanism to notify external services by HTTP protocol (including HTTPS).  

One Webhook notification can be set per connection. If the connection state of a device contained in a connection changes, a POST request will be sent to the notification URL. One request will be sent for each connection status change.  

By providing functionality to notify by sending messages via email, Slack, etc., it becomes possible to promptly know when a device in operation becomes unexpectedly disconnected from the network.  

Notification destinations can be handled in the following ways  

*	enebular's [Heroku Deploy](../Deploy/DeployFlow/Heroku/index.md) / [Lambda Deploy Function](../Deploy/DeployFlow/Lambda/index.md)  
In enebular, create a Node-RED flow to execute the desired process when the device’s connection status changes. Make that flow get triggered by the HTTP request. Deploy that flow to Heroku or Lambda, and by setting that URL as the Webhook’s notification destination, the flow will be executed if the device connection status changes.   

* [IFTTT](https://ifttt.com/)  
A web service to connect and operate multiple social services and platforms together. Triggers and actions to take when triggered can be specified. To use the device status change notification function, specify the Web Request as the trigger.   

This function is a paid function and can only be used in Enterprise Plan projects.  

## HowToUse {#HowToUse}

1. Open the Project that contains the connection for the desired device status change notification
1. Open the menu on the left and click Devices
1. Click on the connection for the desired device status change notification
1. Click on Setting on the upper right side of the Devices screen
1. Click on Webhook when the submenu is displayed
1. When the Webhooks dialog is displayed, click on Add Webhook (If a notification setting already exists, that setting will be displayed. To edit the contents, click on notification setting)
1. When the Create Webhook dialog appears, enter the settings for each item: Name, URL, Key Name (explanation of setting fields is [below](#ItemForSetting))
1. Clicking Test sends dummy data to the entered URL and the result is displayed
1. If the result is normal, the Save button will appear (if the result returns an error, please review the URL and click Test again)
1. Click the Save button to save the Webhook settings

## Notification settings fields {#ItemForSetting}

The Webhook notification settings items are given in the table below.  

| Field | Description |
| --- | --- |
| Name | Name given to the notification setting. |
| URL | Notification destination URL. |
| Key Name | Due to notification destination constraints, notification data may be assigned to a single JSON key. In such a case, specify the key name in this field. For example, for IFTTT, a key name will need to be specified for one of value1-value3. |

## Send to Webhook notification destination {#DataForNotification}

### Transmitted Data

This section describes the data sent to the URL specified in the notification settings. Depending on whether or not the Key Name is specified in the notification settings, the data sent will differ.  

#### When Key Name is not specified

The following information will be sent in JSON format.  

* projectName: Project name
* connectionName: Connection name
* deviceName: Device name
* trigger: Trigger (onConnect / onDisconnect)
    * onConnect: When status changes from disconnect to connect
    * onDisconnect: When status changes from connect to disconnect
* ts: Timestamp (UTC milliseconds)
* deviceLogUrl: enebular’s log screen URL

Example  

```
{
  "projectName": "temperature-today",
  "connectionName": "aws-us-west",
  "deviceName": "thermo-sensor-a",
  "trigger": "onDisconnect",
  "ts": 1615277649908,
  "deviceLogUrl": "https://enebular.com/app/project/44be37d9-e847-4cd9-ab05-0e65b5196f2f/logs/8173f588-d48a-431a-9c34-be7aed03bf19/webhook-aws-999"
}
```

#### When the Key Name is specified

In addition to the above data `When Key Name is not specified`, data is assigned to the specified Key Name.  

The following information is added in HTML format to the data assigned to the specified Key Name:  

* Project: Project name
* Connection: Connection name
* Device Name: Device name
* Trigger: Trigger (onConnect / onDisconnect)
* Timestamp: Timestamp
    * In <Day> <Month> <Year>, <Hour Minute> (<Time Zone>) format
    * Time zone is fixed to UTC
    * Ex: 9th Mar 2021, 08:14 am (UTC)
* Device Log URL: enebular’s log screen URL

Below is an example when anyKeyName is specified as Key Name (anything other than anyKeyName is the same as `When Key Name is not specified`).  

```
{
  "projectName": "temperature-today",
  "connectionName": "aws-us-west",
  "deviceName": "thermo-sensor-a",
  "trigger": "onDisconnect",
  "ts": 1615277649908,
  "deviceLogUrl": "https://enebular.com/app/project/44be37d9-e847-4cd9-ab05-0e65b5196f2f/logs/8173f588-d48a-431a-9c34-be7aed03bf19/webhook-aws-999",
  "anyKeyName": "<b>Project:</b> temperature-today<br><b>Connection:</b> aws-us-west<br><b>Device Name:</b> thermo-sensor-a<br><b>Trigger:<b/> onDisconnect<br><b>Timestamp:</b> 9th Mar 2021, 08:14 am (UTC)<br><b>Device Log URL:</b> https://enebular.com/app/project/44be37d9-e847-4cd9-ab05-0e65b5196f2f/logs/8173f588-d48a-431a-9c34-be7aed03bf19/webhook-aws-999"
}
```

### Request format

The following is the format for the request to the notification destination.  

* HTTP Method: POST
* Content-Type: application/json

### Notification retry processing

If the request to the notification destination fails, it will be retried after 5 seconds. Retry will only be done once.  

## Notes {#Notes}

* If the connection status of many devices changes at the same, the Webhook may not execute properly (depending on the service used, there may be a limit to the number of simultaneous executions, so please check the limit with the service)
* If the request sent to the notification URL results in a response instructing to redirect, it will be treated as an error
