Webhook intermediate tutorial

I made an webhook tutorial that goes into more depth on defining them in JSON, using Request Bin for debugging, and using Mustache templates on requests and responses.

It’s up on Github here:

There are a lot of pictures which would have been a pain to upload directly into a post, plus you can submit corrections and suggestions using issues and pull requests there easily.

8 Likes

Thanks for the clear explanation with examples.

3 Likes

Very nicely done.

You deserve a raise and a promotion.

1 Like

I updated my webhook tutorial with information on using the body feature to create much more complex JSON data, including true numbers, variable-length arrays and other things.

Also included is the link to my handy mustache tester web page. You paste in your JSON data and your mustache template, and it calculates the output. It can also validate JSON, and do the annoying conversion to JSON string needed to use the body feature.

4 Likes

Thanks for the really helpful tutorial and tester! I’m having trouble getting my response template to work. When I don’t use a response template, my photon receives the webhook response data, but in up to 10 chunks that are difficult to parse. I only want a little bit of the data, so I created a template, but now my photon gets no data from the subscription to the webhook.
Here is the template that I’m using

{{#.}} {{name}}~{{state}} {{/.}}

Here is the JSON that I get back from the webhook without a template:

[
{
    "id": "33084",
    "name": "A-7",
    "type": "Ultimaker 2 Extended",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        24.6,
        25.3
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "33330",
    "name": "A-6",
    "type": "Ultimaker 2 Extended",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        24.3,
        26.9
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34246",
    "name": "A-8",
    "type": "Ultimaker 2 Extended",
    "workgroups": [
        24
    ],
    "state": "connecting",
    "temps": [
        36.9,
        26.18
    ],
    "target_temps": [
        70,
        0
    ],
    "percent": 0
},
{
    "id": "34274",
    "name": "A-9",
    "type": "Ultimaker 2 Extended",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        28.8,
        28.7
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34275",
    "name": "A-11",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        26.5,
        27.8
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34276",
    "name": "B-2",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.1,
        229.8
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 97
},
{
    "id": "34277",
    "name": "B-3",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        70,
        239.7
    ],
    "target_temps": [
        70,
        240
    ],
    "percent": 0
},
{
    "id": "34278",
    "name": "B-4",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        70.9,
        237.2
    ],
    "target_temps": [
        70,
        237
    ],
    "percent": 20
},
{
    "id": "34281",
    "name": "C-2",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        70.2,
        227.8
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 90
},
{
    "id": "34282",
    "name": "C-3",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        22.2,
        24.6
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34283",
    "name": "C-4",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        71.4,
        239.3
    ],
    "target_temps": [
        70,
        240
    ],
    "percent": 6
},
{
    "id": "34284",
    "name": "C-5 ",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.8,
        237.2
    ],
    "target_temps": [
        70,
        237
    ],
    "percent": 11
},
{
    "id": "34285",
    "name": "A-5",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        26.5,
        78.1
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34286",
    "name": "A-14",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        89.7,
        249.5
    ],
    "target_temps": [
        90,
        250
    ],
    "percent": 0
},
{
    "id": "34287",
    "name": "A-13",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.7,
        230.5
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 56
},
{
    "id": "34288",
    "name": "A-12",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        49.6,
        39.6
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "34313",
    "name": "A-10",
    "type": "Ultimaker 2 Extended",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.8,
        237
    ],
    "target_temps": [
        70,
        237
    ],
    "percent": 72
},
{
    "id": "37198",
    "name": "B-10",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.7,
        230.7
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 87
},
{
    "id": "37199",
    "name": "B-9",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.7,
        229
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 48
},
{
    "id": "37200",
    "name": "A-4",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        17.5,
        35.6
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
},
{
    "id": "37201",
    "name": "A-3",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        72.2,
        239.2
    ],
    "target_temps": [
        70,
        240
    ],
    "percent": 14
},
{
    "id": "37202",
    "name": "A-1",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        71.7,
        235.8
    ],
    "target_temps": [
        70,
        237
    ],
    "percent": 71
},
{
    "id": "37203",
    "name": "B-1",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.8,
        230.2
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 81
},
{
    "id": "37204",
    "name": "B-5",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        70.6,
        229.7
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 12
},
{
    "id": "37205",
    "name": "A-2",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "offline",
    "temps": [
        63.9,
        129.5
    ],
    "target_temps": [
        0,
        260
    ],
    "percent": 0
},
{
    "id": "37211",
    "name": "B-13",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        70.2,
        230
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 12
},
{
    "id": "37212",
    "name": "B-12",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.7,
        230
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 53
},
{
    "id": "37220",
    "name": "B-11",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.1,
        229.7
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 51
},
{
    "id": "37222",
    "name": "C-11",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        68.8,
        229.2
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 13
},
{
    "id": "37223",
    "name": "C-12",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        68.3,
        231
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 18
},
{
    "id": "37225",
    "name": "C-14",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        71.1,
        230.3
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 78
},
{
    "id": "37262",
    "name": "B-8",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        68.4,
        229.5
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 3
},
{
    "id": "37303",
    "name": "B-7",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.5,
        229.8
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 52
},
{
    "id": "37304",
    "name": "B-6",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.4,
        230.5
    ],
    "target_temps": [
        70,
        230
    ],
    "percent": 89
},
{
    "id": "38742",
    "name": "C-15",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "printing",
    "temps": [
        69.1,
        240.5
    ],
    "target_temps": [
        70,
        240
    ],
    "percent": 76
},
{
    "id": "39875",
    "name": "B-14",
    "type": "Ultimaker 2",
    "workgroups": [
        24
    ],
    "state": "idle",
    "temps": [
        50.9,
        37.2
    ],
    "target_temps": [
        0,
        0
    ],
    "percent": 0
}

]

Where am I going wrong here? This template returns exactly what I want in the mustache template.

I added a new feature to the Mustache Tester. Now, when you Show formatted JSON data, it’s clickable. Just click on a row and it will generate the mustache template to access that piece of data!

If you have an unformatted blob of JSON data (top box), this greatly simplifies figuring out the right selector to use in your webhook.

4 Likes