Switch Webhooks

Post Reply
User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Switch Webhooks

Post by bkromer » Fri Sep 25, 2020 1:59 pm

Hey everyone,

I am reading the documentation trying to figure out how WebHooks work in Switch.
The HTTP-Listener is activated and the configured port is open and points to the switch server.
When I create a flow with a WebHook Flow Element with protocol HTTP and Method POST and sending a JSON to the URL, it works perfectly.

However, when I create a QR-Code with that URL in it and I open it with a smartphone or tablet nothing happens on the switch side.
Is it somehow possible to achieve this with Switch? What else do I need for this?

Thanks in Advance
Benjamin Kromer

Padawan
Advanced member
Posts: 299
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Switch Webhooks

Post by Padawan » Sun Sep 27, 2020 8:56 am

When you open a url in a browser you will do a Get. If you want to do a post from a browser you will need to create a form.

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Mon Sep 28, 2020 11:40 am

Okay and since there is no way to listen for a GET request in Switch it does not work. Maybe there is a way to pack stuff in a URL do make the browser do a post on this webhook?!
Benjamin Kromer

Padawan
Advanced member
Posts: 299
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Switch Webhooks

Post by Padawan » Mon Sep 28, 2020 12:07 pm

I'm afraid it won't be that simple.

The easiest way to do a POST from a browser is to have a form.

If you want to do an automatic post when the page loads, then I think you'll need to have a javascript in a website which does the post for you.

What are you trying to achieve?

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Mon Sep 28, 2020 12:12 pm

I want a QR-Code that triggers the webhook. When I create a Webhook in Integromat this works. And it loads a website where it spits out the Status code. I want the people in the production to scan the QR-Code when they finished the job.
Benjamin Kromer

jan_suhr
Advanced member
Posts: 420
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Switch Webhooks

Post by jan_suhr » Mon Sep 28, 2020 12:28 pm

Where is the QR-code? Is it on printed paper.

How are they scanning it? Phone or scanner attached to computer? In any case the scanning software is the one who sends the POST, right?
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

laurentd
Member
Posts: 41
Joined: Wed Mar 13, 2019 2:06 pm

Re: Switch Webhooks

Post by laurentd » Mon Sep 28, 2020 12:35 pm

Modify and save this code as php file on your web site.
Your QR code should refer to the php file's url.

<?php
$ch = curl_init();
$data='{"jobnumber":123456, "filename":"GES.pdf", "number of copies":150}';
curl_setopt($ch, CURLOPT_URL, "http://localhost:51080/Switch/ReleaseJob");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);
?>
Laurent De Wilde, Solution Architect @ Enfocus

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Mon Sep 28, 2020 1:39 pm

jan_suhr wrote:
Mon Sep 28, 2020 12:28 pm
Where is the QR-code? Is it on printed paper.

How are they scanning it? Phone or scanner attached to computer? In any case the scanning software is the one who sends the POST, right?
Yes its printed on paper. And to scan it could be really any device within our IP-Range but we use IPads scanning with the builtin camera.

Padawan wrote:
Mon Sep 28, 2020 12:07 pm
I'm afraid it won't be that simple.

The easiest way to do a POST from a browser is to have a form.

If you want to do an automatic post when the page loads, then I think you'll need to have a javascript in a website which does the post for you.

What are you trying to achieve?
I dont have a Webserver running for this atm.

I can only show an Example WebHook from Integromat.
WebHook URL: https://hook.integromat.com/lti6akuqvj6 ... k8jl1hu9zh
Same as QR Code:
qr.png
qr.png (512 Bytes) Viewed 3025 times
If u scan this with an IPhone or IPad you will be asked to open the website and if u click okay an empty website is loaded or just one line saying "Accepted" or in your case "Access denied from this IP."
When Accepted the WebHook starts a Flow in Integromat. I want the same for Switch.
laurentd wrote:
Mon Sep 28, 2020 12:35 pm
Modify and save this code as php file on your web site.
Your QR code should refer to the php file's url.

<?php
$ch = curl_init();
$data='{"jobnumber":123456, "filename":"GES.pdf", "number of copies":150}';
curl_setopt($ch, CURLOPT_URL, "http://localhost:51080/Switch/ReleaseJob");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);
?>
Thanks for the snippet! I thought I could find a easier solution for this.
Benjamin Kromer

laurentd
Member
Posts: 41
Joined: Wed Mar 13, 2019 2:06 pm

Re: Switch Webhooks

Post by laurentd » Mon Sep 28, 2020 1:51 pm

In Switch you need a webhook element with this Path property: /Switch/ReleaseJob (or anything else of course if you edit the code)
If the php file is hosted on the Switch server, the url in the code is "http://localhost:51080/Switch/ReleaseJob", otherwise replace "localhost" with IP address.
An yes indeed, you need to host this on a web server. Can be a simple MAMP or WAMP or XAMPP.
Laurent De Wilde, Solution Architect @ Enfocus

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Mon Sep 28, 2020 1:54 pm

laurentd wrote:
Mon Sep 28, 2020 1:51 pm
In Switch you need a webhook element with this Path property: /Switch/ReleaseJob (or anything else of course if you edit the code)
If the php file is hosted on the Switch server, the url in the code is "http://localhost:51080/Switch/ReleaseJob", otherwise replace "localhost" with IP address.
An yes indeed, you need to host this on a web server. Can be a simple MAMP or WAMP or XAMPP.
Okay, I will give this a try. Thanks a lot.
Benjamin Kromer

jan_suhr
Advanced member
Posts: 420
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Switch Webhooks

Post by jan_suhr » Mon Sep 28, 2020 1:57 pm

Your URL needs to include the data like what is suggested before.
https://yoururl?jobnumber=123456&filena ... Copies=150

That will then be picked up on the webserver and from there you can send it to Switch.
So you QR-code should include the URL with variables.

There is a lot of info on how to do it if you Google.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Mon Sep 28, 2020 4:20 pm

jan_suhr wrote:
Mon Sep 28, 2020 1:57 pm
Your URL needs to include the data like what is suggested before.
https://yoururl?jobnumber=123456&filena ... Copies=150

That will then be picked up on the webserver and from there you can send it to Switch.
So you QR-code should include the URL with variables.

There is a lot of info on how to do it if you Google.
Okay got it. Thanks!

Since the Topic of this post is Switch WebHooks in general ;) I have some further questions.

A different scenario now. I "POST" the JSON {"id":"MB42"} to my WebHook "http://myIP:51080/jobs" in Switch.
Is there a way of dynamically creating new webhooks with the id as path like in this case "http://myIP:51080/jobs/MB42"?

Example code (most of it straight from the docs):

Code: Select all

function webhookTriggered(s : Switch, r : WebhookRequest) {
    if (r == null) { // Not a real request, but first initialization call at the moment flow is activated -> subscribe
        try {
            s.webhookSubscribe(WebhookRequest.POST, "/job","HTTP"  );
        }
        catch(e) {
            s.log(3, "Failed to subscribe to the request: %1", String( e ));
            return;
        }
       s.log(1, "Subscribed to HTTP and HTTPS requests for the path '/job'" );
        return;
    }
	s.log(1, "Received notification from %1", r.remoteAddress);
	var newJob = s.createNewJob();
	var id = JSON.parse(r.getBody().toString("UTF-8"))['id'];
	id = String( id );
	[b]// AT THIS POINT I WANT TO SUBSCRIBE A NEW WEBHOOK WITH THE ID IN ITS PATH LIKE /job/42... but how?[/b]
	try {
            s.webhookSubscribe(WebhookRequest.POST, "/job/"+id,"HTTP"  );
        }
        catch(e) {
            s.log(3, "Failed to subscribe to the request: %1", String( e ));
            return;
        }
       s.log(1, "Subscribed to HTTP and HTTPS requests for the path '/job/"+id+"'" );
        return;
	//
   newJob.sendToSingle(r.content, "createWebhookWithID.json");
}
The messages from my test script.(read from bottom to the top)
Bildschirmfoto 2020-09-28 um 16.09.36.png
Bildschirmfoto 2020-09-28 um 16.09.36.png (61.37 KiB) Viewed 3000 times
The first path "/job" is subscribed and the second one with "/job/MB42" but if I now try to POST another json on that new webhook ("/job/MB42) nothing happens.

So how can I continue with the second WebHook do I need a second WebHook Triggered function?
Benjamin Kromer

jan_suhr
Advanced member
Posts: 420
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Switch Webhooks

Post by jan_suhr » Mon Sep 28, 2020 4:38 pm

The thing with the property paths in the Webhook is a bit confusing.

See the path as the address to one webhook receiver. It must be unique on your Switch server.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Switch Webhooks

Post by bkromer » Tue Sep 29, 2020 10:09 am

Okay, I have the Webserver up and running with the following javascript.

Code: Select all

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
var id =  getQueryVariable("id");
http = new XMLHttpRequest();
http.open("POST","http://switchwebhook.bechtel-druck.de:51080/job");
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
http.send(JSON.stringify({"id":String(id)}));
When I open the url http://SwitchServerIP?id=666 the script catches the id 666 and calls the WebHook. Then my SwitchScript is running.

Code: Select all

function webhookTriggered(s : Switch, r : WebhookRequest) {
    if (r == null) { // Not a real request, but first initialization call at the moment flow is activated -> subscribe
        try {
            s.webhookSubscribe(WebhookRequest.POST, "/job","HTTP"  );
        }
        catch(e) {
            s.log(3, "Failed to subscribe to the request: %1", String( e ));
            return;
        }
       s.log(1, "Subscribed to HTTP and HTTPS requests for the path '/job'" );
        return;
    }
	s.log(1, "Received notification from %1", r.remoteAddress);
	var newJob = s.createNewJob();
	var id = JSON.parse(r.getBody().toString("UTF-8"))['id'];
	id = String( id );
	// AT THIS POINT I WANT TO SUBSCRIBE A NEW WEBHOOK WITH THE ID IN ITS PATH LIKE /job/42... but how?
	try {
            s.webhookSubscribe(WebhookRequest.POST, "/job/"+id,"HTTP"  );
        }
        catch(e) {
            s.log(3, "Failed to subscribe to the request: %1", String( e ));
            return;
        }
       s.log(1, "Subscribed to HTTP and HTTPS requests for the path '/job/"+id+"'" );
        return;
	s.log(1, "Received notification from %1", r.remoteAddress);
	var newJob = s.createNewJob();
	newJob.sendToSingle(r.content, "createWebhookWithID.json");
	//
   newJob.sendToSingle(r.content, "createWebhookWithID.json");
}
It subscribes WebHooks for every id I send to it. But if I call on the WebHook with the id like "/job/666" it tries to find the id in the json and crashes.
I know that I need to seperate the first and the following WebHooks but how? Maybe I need to make a sendToData() after the inital WebHook request pushing the id to a privateData variable and on the second sscript subsribe to that WebHook?
Benjamin Kromer

Post Reply