Extending functionality with scripts

Source

Let's open another app folder and look through the files as we go along.

Here you'll see examples of panels and scripts.

Now install the app for yourself. Download the app source code and unzip it locally.

Open the command line to install the app following the same steps as before.

Now you will have a script configured at the following link:

https://{host_address}/api/v1/{company_id}/apps/day4/scripts/Present.

Quickly check to make sure only the header is returned, and look at the code to check what is returned.

Open the main.js file

Now adjust the code and change the body. Open day4_scripts > main.js in an editor and edit line 2:

let body = "";
  body += "<html><body><h1>{change me to something different}</h1>";
  body += "<img src = 'https://kscnewmancenter.files.wordpress.com/2012/12/aimgt104.jpg?w=400&amp;h=399'/>";
  body += "</body></html>";

Now reinstall the app by running hourglass-cli.

You won't need set all of the params again as they have been stored locally in the .bbugrc file

Next, test passing in some params to alter the returned data.

All data passed in on the URL is in fact passed in to the script in the 'data' variable.

Change the code to expect some inbound data by adding the following lines:

const count = parseInt(data.birds);
 
let body = "";
body +="<html><body><h1>Day 4 Present</h1>";
for (let i =0; i < count; i++)
body += "<img src = 'https://usercontent2.hubstatic.com/4233451.jpg'/>";
body += "</body></html>";

Now re-install and run https://{host_address}/api/v1/{company_id}/apps/day4/scripts/Present?birds=4.

You will see that we can adjust the result.

Now add a new script.

Open the manifest.json file

First add the new handler in day4_scripts > manifest.json file, add the following lines so that the scripts say:

{
"name":"Present",
"description":"tell me what I'm getting on day 4",
"type":"PublicScript",
"actions": ["Get"],
"log_type": "None",
"enabled": true,
"handler": "calling_bird_http"
},{
"name":"UpdateClient",
"description":"Called when a client is updated",
"type":"Clients",
"actions": ["after_update"],
"log_type": "Tail",
"enabled": true,
"handler": "update_client"
}

This is a new type of script. Rather than a 'PublicScript'. This is now of type "Clients", with an action of "after_update".

This tells it to call the script after an existing client has been updated.

It's also calls a new handler that you need to define: "update_client".

Go to the main.js file for scripts and add that:

exports.update_client = function(data, callback){
const client = data.client;
console.log("Got a client update for:", client.first_name, " ", client.last_name)
callback(null, {});
}

Here all you are doing is logging that change was made.

The client object you are given is in fact a full BBModel.Admin.Client object as documented in the Core SDK.

With with the hourglass-cli tool, you can continually monitor the log outputs when you leave it running by starting it with: hourglass-cli tail

Re-install the new app and then go to Studio.

Edit an existing client by changing their name and you should see the log locally write you the change.

Challenge

Create another script that monitors the "after_create" action and test that it is called when you create a new Client.

Click here to see the final version of these apps if you are having problems.