Creating a webhook with Runkit

Try to run obniz in response to an external request

runkit has an endpoint feature to accept URLs from outside.
You can use it as a webhook, so it can be used more widely!
HTTPS is supported, so it can be connected to various services

Let's write a program to turn the LEDs on and off according to the URL

// Runkit Endpoint Example

const Obniz = require("obniz");
const express = require("@runkit/runkit/express-endpoint/1.0.0");
const app = express(exports);

const yourObnizId = "OBNIZ\_ID\_HERE";  // write your obniz id

app.get("/", async (req, res) => {

    let obniz = new Obniz(yourObnizId);

    let connected = await obniz.connectWait({timeout:10});

    if(connected){
         let led = obniz.wired("LED", {anode:0, cathode:1});
         let changed = "";
         if(req.query.led == "on"){
            led.on();
            changed = "on";
         }else{
            led.off();
            changed = "off";
         }

         obniz.resetOnDisconnect(false);
         obniz.close();

         res.send("LED Changed to "+changed);
    }

});

It is implemented using Express, the famous Node.js framework.
In the lower right corner of this page, you can see the URL instead of the run button.
Let's press this button.

If you're writing a program on runkit, press endpoint

If the message "LED Changed to off" is displayed, it's a success.
If you get an error like below, please make sure that the obniz id is correct and that obniz is turned on

{
  "error": "timeout",
  "message": "The server did not return a response quickly enough."
}

If "LED Changed to off" is displayed successfully, now let's make the LED light up be possible
Add "?led=on" to the endpoint after the URL that is sent when you click the endpoint.

https://untitled-kgxjycirmufe.runkit.shだった場合はhttps://untitled-kgxjycirmufe.runkit.sh?led=onです.

Let's jump to your URL

If the message "LED Changed to on" appears and the LED comes on, you have succeeded!

If you want to turn off the LEDs, try changing it to "led=off".
For example, it looks like this
https://untitled-kgxjycirmufe.runkit.sh?led=off

Did you turn off the LEDs? Congratulations!
Now you can control the LEDs from anywhere in the world!

Publish and add the URL of your choice.

I just created an endpoint, but the URL was a random string of URLs.
https://untitled-kgxjycirmufe.runkit.sh

This is hard to remember, so change it to your favorite URL

First, write the code on runkit and name the notebook.
Put your favorite name in "untitled-notebook".
This time it's "obniz-led-onoff".

Click on the "publish" button to open the publish configuration screen.

After the release, the following URL will be available

https://runkit.com/[username]/[notebook name]/[version]

With a user name of 9wick and a notebook named "obniz-led-onoff" In the case of

https://runkit.com/9wick/obniz-led-onoff/1.0.0