web hooks? MS Teams

Post Reply
ckuhlmann
Newbie
Posts: 12
Joined: Thu Apr 02, 2015 7:53 pm

web hooks? MS Teams

Post by ckuhlmann »

Hi all,

Has anyone worked with sending notices out to applications like Slack or MS Teams?

I was looking to use HTTP Request to post an error message to a group channel. But it seems you can only post a file, where I just want to post a simple json string.

example:
{
"@context": "http://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Switch could not Process Job",
"text": "AHHH SOMETHING HAPPENED"
}



If someone has done this I would love to see how it was achieved. I do have the scripting module but this seemed simple enough that I would assume the configurator could handle it.

Thanks
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: web hooks? MS Teams

Post by sander »

I couldn't get post with json to work either so I switched to cURL. Works great, worth a try! :)

Code: Select all

		// Other carriers
			jsonBody = '{' +
				'"company": "'+propCompany.left(35)+'",' + // Not required
				'"full_name": "'+propFullName.left(35)+'",' +
				'"post_code": "'+propPostcode+'",' +
				'"street": "'+propStreet+'",' +
				'"street_number": "'+propStreetNumber.left(8)+'",' +
				'"suffix": "'+propStreetNumberAdd.left(5)+'",' +
				'"city": "'+propCity+'",' +
				'"country": "'+propCountry+'",' +
				'"email_address": "'+propEmail+'",' + // Not required
				'"phone_number": "'+propPhoneNumber+'",' + // Not required
				'"instruction_on_label": "'+propInstruction+'",' +
				'"parcel_reference": "'+propParcelRef.left(27)+'",' + // Not required. Char limit is 35, however DPD labels get messed up when too long.
				'"shipping_carrier_id": "'+propShipCarID+'",' +
				'"shipping_carrier_option": "'+propShipCarOption+'",' +
				'"total_weight": "'+propTotalWeight+'",' +
				'"parcel_number_of_labels": "'+propNumOfLabels+'",' +
				'"delivery_date": "'+tomorrow+'",' +
				'}'

		if (debug) { s.log(3, "jsonBody: " + jsonBody); }
	
		json = s.createPathWithName(job.getNameProper()+'.json');
		File.write(json, jsonBody,'UTF-8');
		
		cmd = 'd:\\scripts\\cURL\\curl.exe -X POST --http1.0 -H "Content-Type:application/json" -H "api-key:' + propApiKey + '" --data-binary \"@' + json + '" ' + propURL;
		
		if (debug) { s.log(3, "Post json with cURL command: " + cmd); }

		Process.execute(cmd);
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: web hooks? MS Teams

Post by Padawan »

Afaik Enfocus assumed that the json would always be created using a script when they created HTTP Request, that's why you can't do it directly in HTTP Request.

Maybe you can convince the author of the next App to make a "Make-Json" App, that would allow it to be used in combination with HTTP Request
https://www.enfocus.com/en/appstore/product/make-xml

If you do go the scripting route, I do use the HTTP class for this kind of stuff. This is a modified version from the documentation which might be able to help you:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
  var theHTTP = new HTTP( ); 
  var payload = createFilePathWithContent( s, '{' + 
												   '"key": "value"' +
												   '}'
											  );
  
  theHTTP.setAttachedFile(payload);
  theHTTP.url = "http://jsonplaceholder.typicode.com/posts"; 

  theHTTP.post(); 

  job.log( 4, "Upload started", 100);
  while( !theHTTP.waitForFinished( 3 ) )
  {
  job.log( 5, "Uploading...", theHTTP.progress() );
  }
  job.log( 6, "Upload finished" ); 
  job.log( 1, "Server response: %1", theHTTP.getServerResponse().toString( "UTF-8" ) );
  if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 201 )
  {
      job.log( 1, "Upload completed successfully" );
  }
  else
  {
      job.fail( "Upload failed with the status code %1", theHTTP.statusCode );
      return;
  }
}


function createFilePathWithContent( s : Switch , content)
{
    var theFilePath = s.createPathWithName( "temp.txt" );   
    var theFile = new File( theFilePath );
    theFile.open( File.WriteOnly );
    theFile.write(content)
    theFile.close();   
    return Dir.convertSeparators(theFile.fullName);   
}
Edit: Now that I think a but further on it, you can even use this script expression in the "Attached file" property of HTTP request:

Code: Select all

function createFilePathWithContent( s : Switch , content)
{
    var theFilePath = s.createPathWithName( "temp.txt" );   
    var theFile = new File( theFilePath );
    theFile.open( File.WriteOnly );
    theFile.write(content)
    theFile.close();   
    return Dir.convertSeparators(theFile.fullName);   
}
createFilePathWithContent(s, '{"key": "value"}');
You could use Switch variables in there like this:
job.getVariableAsString("[Job.Name]", s);
ckuhlmann
Newbie
Posts: 12
Joined: Thu Apr 02, 2015 7:53 pm

Re: web hooks? MS Teams

Post by ckuhlmann »

Thank you Both,

I was able to make a script do what I needed, but I want to give the script expression a try. And your script is much more elegant than mine ;)
User avatar
vernonvd
Member
Posts: 38
Joined: Tue Oct 09, 2012 9:21 am
Location: Pretoria, South Africa

Re: web hooks? MS Teams

Post by vernonvd »

we use MS teams via Switch. i was able to achieve this without scripting and it works quite well. i use the make json App

sample post:
Attachments
Capture.JPG
Capture.JPG (27.42 KiB) Viewed 6943 times
Post Reply