Switch Webhooks
Switch Webhooks
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
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
Re: Switch Webhooks
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.
Re: Switch Webhooks
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
Re: Switch Webhooks
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?
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?
Re: Switch Webhooks
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
Re: Switch Webhooks
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?
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?
Re: Switch Webhooks
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);
?>
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
Re: Switch Webhooks
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.
I dont have a Webserver running for this atm.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 can only show an Example WebHook from Integromat.
WebHook URL: https://hook.integromat.com/lti6akuqvj6 ... k8jl1hu9zh
Same as QR Code: 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.
Thanks for the snippet! I thought I could find a easier solution for this.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);
?>
Benjamin
Re: Switch Webhooks
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.
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
Re: Switch Webhooks
Okay, I will give this a try. Thanks a lot.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.
Benjamin
Re: Switch Webhooks
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.
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.
Re: Switch Webhooks
Okay got it. Thanks!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.
Since the Topic of this post is Switch WebHooks in general
![Wink ;)](./images/smilies/icon_e_wink.gif)
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");
}
So how can I continue with the second WebHook do I need a second WebHook Triggered function?
Benjamin
Re: Switch Webhooks
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.
See the path as the address to one webhook receiver. It must be unique on your Switch server.
Re: Switch Webhooks
Okay, I have the Webserver up and running with the following javascript.
When I open the url http://SwitchServerIP?id=666 the script catches the id 666 and calls the WebHook. Then my SwitchScript is running.
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?
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)}));
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");
}
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