Skip to content

mkontani/nature-proxy

Repository files navigation

nature-proxy

DockerCI

🔊 Simple API tool as nature proxy. nature is a IoT device as remote controller, and IFTTT is a trigger and action service with IoT device.

This proxy addresses following problems:

  • IFTTT free plan has became define only 3 custom actions.
  • IFTTT trigger cannot request multiple times all at once.
  • resolve CORS errs (requests by web browser) for nature apis.
  • schedule(cron) support.

IFTTT action is like below:

image

Google Assistant has ingredient util on IFTTT (speaking phrase can be used on next webhook), so GoogleHome is especially suitable.

Setup

Generate nature request payload

See details: https://developer.nature.global

# get props example
$ curl -X GET "https://api.nature.global/1/appliances" \
  -H "accept: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq | tee remo-appliance.json

Env setting

This api is restricted by apikey on request body, so you should deploy on tls.

Set APIKEY with any random value on .env.

ACCESS_TOKEN is need for requesting nature-api. see https://home.nature.global/home

# mandatory
APIKEY=xxxx
ACCESS_TOKEN=xxxx

# option
LOG_LEVEL=info (defult: `warn`)
PORT=9999 (default: `8000`)
CONF_PATH=/path/to/conf (default: `./mappings.json`)

# option (for preflight)
ACCESS_CONTROL_ALLOW_ORIGIN=https://example.com (default: *)
ACCESS_CONTROL_ALLOW_HEADERS=X-TOKEN (default: *)
ACCESS_CONTROL_ALLOW_METHODS=POST (default: *)
ACCESS_CONTROL_ALLOW_CREDENTIALS=true (default: unset)

# option (for use standalone ssl.)
USETLS=true
KEY_PATH=./server.key
CERT_PATH=./server.cert
PORT=443
TIME_ZONE=Asia/Tokyo

Config setting

We should define mappings for type and id.

Set mappings.json like below:

{
  "rules": [
    {
      "type": "appliance:LIGHT",
      "id": "turn-light",
      "appliance_id": "xxxx-xxxx-xxxx-xxxx-xxxxx",
      "words": [
        "照明",
        "つけて"
      ],
      "payload": "button=onoff"
    },
    {
      "type": "signal",
      "id": "turn-planetarium",
      "signal_id": "xxxx-xxxx-xxxx-xxxx-xxxxx",
      "words": [
        "プラネタリウム",
        "電源"
      ]
    }
  ],
  "schedules": [
    {
      "cronTime": "0 0 0 * * *",
      "timezone": "Asia/Tokyo",
      "ruleId": "turn-light",
      "repeat": 1
    }
  ]
}

In rules section, if all words are contained in request phrase, its correspond payload will be use.

In schedules section, you can define schedules for rules correspond to ruleId. This section is Optional.

IFTTT Action Setting

IF

Use Say a phrase with both a number and a text ingredient on Google Assistant.

Set params for What do you want to say? like below:

IR $ for # times (IR $ を # 回)

Then, if you speak ok google, IR Room light 3 times, Room light is set to $ and 3 is set to #.

These params can be used in next then-action by using ingredient.

THEN

Use Make a web request on webhook.

Set params like below:

URL: https://<your apihost>/v1/api/nature
Method: POST
Content-type: application/json
Body: {"apikey": "<your defined value>", "phrase": " {{TextField}}", "repeat": {{NumberField}} }

{{TextField}} and {{NumberField}} are corresponded to Room light and 3 respectively on above example.

Run

# local
$ npm ci
$ npm run serve

################################ 

# Run with docker
$ docker run -d -p 127.0.0.1:8000:8000 --restart always \
    -v $PWD/mappings.json:/app/mappings.json \
    -v $PWD/.env:/app/.env \
    ghcr.io/mkontani/nature-proxy:latest

Build

# docker usecase
$ docker build -t nature-proxy .

## proxy case
$ docker run -d -p 127.0.0.1:8000:8000 --restart always nature-proxy

## standalone case (set env USETLS=true)
$ docker run -d -p 443:443 --restart always nature-proxy

Manual Request

Request with id

ᐅ curl -XPOST -H 'content-type: application/json' \
    -d '{"apikey": "xxxx", "id": "turn-light", "repeat": 1}' \
    https://api.nicopun.com/v1/api/nature

Request with phrase

ᐅ curl -XPOST -H 'content-type: application/json' \
    -d '{"apikey": "xxxx", "phrase": "照明 を 明るく して", "repeat": 2}' \
    https://api.nicopun.com/v1/api/nature

About

Nature-remo proxy API Server.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors