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
			
			
									
						
										
						web hooks? MS Teams
Re: web hooks? MS Teams
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);Part of my playground:
- HP Indigo 10k, HP Indigo 7600's (full options), Highcon Euclid III, Zünd S3
- HP Production Pro 6.0.1, HP Production Center 2.5.1 beta, Apogee 9.1, Enfocus Switch 13u1 & PitStop Server 13u2.
Chat: open-automation @ gitter
			
						- HP Indigo 10k, HP Indigo 7600's (full options), Highcon Euclid III, Zünd S3
- HP Production Pro 6.0.1, HP Production Center 2.5.1 beta, Apogee 9.1, Enfocus Switch 13u1 & PitStop Server 13u2.
Chat: open-automation @ gitter
Re: web hooks? MS Teams
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:
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:
You could use Switch variables in there like this:
job.getVariableAsString("[Job.Name]", s);
			
			
									
						
										
						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);   
}
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"}');
job.getVariableAsString("[Job.Name]", s);
Re: web hooks? MS Teams
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
			
			
									
						
										
						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
