New Rest API - handlePing and other handle functions

Hello,

I have an question, about new API , I think that I don’t understand new model Smart App, totally. I created SmartApp on base of documentation but if I should create all handle functions by myself (despite of exsisting API) ? And if response is yes, in what way I should create handlePIng function, because I can’t see information on this subject in documentation.

indent preformatted text by 4 spaces
> Import the necessary libraries/declare the necessary objects:
// Basic Server:
//framework
var express = require("express");
// Body-Parser
var bodyParser = require("body-parser");
// Request
var request = require('request');
// Http
var http = require('http');
//baseUrl
var baseUrl = "https;//api.smartthing.com";

// Https
var https = require('https');
// Files
var fs = require('fs');

// Http-signature
var httpSignature = require('http-signature');

//Key and cert created by openssl
var options = {
key: fs.readFileSync('data/agent2-key.pem'),
cert: fs.readFileSync('data/agent2-cert.cert')
};


//create a service
var app = express();
app.use(bodyParser.json());

//app.use(myParser.urlencoded({extended : true}));

app.post('/smartthings', function (req, response) {
// We don't yet have the public key during PING (when the app is created),
// so no need to verify the signature. All other requests are verified.
if (req.body && req.body.lifecycle === "PING" || signatureIsVerified(req)) {
handleRequest(req, response);
} else {

response.status(401).send("Forbidden");
}
});



function signatureIsVerified(req) {
 try {
  let parsed = httpSignature.parseRequest(req);
  if (!httpSignature.verifySignature(parsed, publicKey)) {
  console.log('forbidden - failed verifySignature');
  return false;
  }
 } catch (error) {
   console.error(error);
  return false;
 }
 return true;
 }

function handleRequest(req, response, error) {
// handle all lifecycles from SmartThings
let evt = req.body;
let lifecycle = evt.lifecycle;
let res = null;

switch(lifecycle) {
    case 'PING':
     res = handlePing(evt.pingData.challenge);
     resp.json({statusCode: 200, pingData: {challenge:res}});
     break;
   case 'CONFIGURE':
     res = handleConfig(evt.configurationData);
     resp.json({statusCode: 200, configurationData: res});
   break;
   case 'INSTALL':
      handleInstall(evt.installData.installedApp, evt.installData.authToken);
      resp.json({statusCode: 200, installData: {}});
    break;
   case 'UPDATE':
      handleUpdate(evt.updateData.installedApp, evt.authToken);
      resp.json({statusCode: 200, updateData: {}});
    break;
   // handle other lifecycles...
   case 'EVENT':
     //???????? handleEvent write
     handleEvent(evt.eventData.installedApp, evt.authToken);
     resp.json({statusCode: 200, eventData:{}});
    break;
   case 'OAUTH_CALLBACK':
     //???????? handleEvent write   
     handleoAuthCallback(evt.oAuthCallbackData.installedAppId , evt.oAuthCallbackData.urlPath);
     resp.json({statusCode: 200, oAuthCallbackData:{}});
   break;
   case 'UNINSTALL':
       handleUninstall(evt.uninstallData.installedApp, evt.uninstallData.authToken);
      resp.json({statusCode: 200, uninstallData:{}});
   default:
   console.log(`lifecycle ${lifecycle} not supported`);
  } 

}

function initializing(){
    console.log("Https server works");
}
//Start the server and make it listen for connections on port 8080 
 //http.createServer(app).listen(3000 , listening);
 https.createServer(options, initializing).listen(8080);

Best regards

The purpose of PING is to verify that your SmartApp exists and is running during app registration. So, your SmartApp will receive a payload as documented here and the app should respond with the challenge code sent.

An example would look like:

    // PING happens during app creation. Purpose is to verify app
    // is alive and is who it says it is.
    case 'PING': {
      let chal = evt.pingData.challenge;
      response.json({statusCode: 200, pingData: {challenge: chal}});
      break;
    }

2 Likes

Ok, Thank you for your answer, so in the case of ping event there is no handlePing function, but I understand that these functinos are neccessary for all other lifecycle events:

-configure,
-install,
-update,
-uninstall,
-event
-oauth callback ?