Cisco Spark Webhooks using Node.js

February 20, 2017

I’ve been trying to come up with a reason to play around with node.js. I’ve also been trying to make some time to play around with the Cisco Spark API. So I figured I would merry the two of them. My intention was to setup a webhook on a channel, then in my node application display the messages being posted in my room. Pretty simple example, but it touches a few different things.

First, you need to setup your webhook to point to your server.

Second, you need to have node installed, in this case I’m using Ubuntu 16.04.

Third, write some code. Check out my comments to follow along.

//will use these for the POST request

var express = require("express");
var myParser = require("body-parser");
var app = express();

//will use this for the GET request

var http = require(‘https’);

app.use(myParser.json());
app.post("/", function(request, response){

console.log(request.method);

//console the webhook name as well as the name of the author of the message
console.log(‘Webhook:’+request.body.name);
console.log(‘Email:’+request.body.data.personEmail);

//setup your GET request to find out the actual message posted by the author

var options = {
host: ‘api.ciscospark.com’,
path: ‘/v1/messages/’+request.body.data.id,
headers: {
  ‘User-Agent’: ‘request’,
  ‘Authorization’: ‘Bearer MyAuth’
}
}

//make the request

http.request(options, OnResponse).end();
});

//capture the request response

function OnResponse(response){
var data = ”;
response.on(‘data’, function(chunk){
  data += chunk;
});

response.on(‘end’, function(){
  data = JSON.parse(data);

//console out the actual text
  console.log(data.text);
});
}

app.listen(8080);

~david

tags: ,
posted in Cisco, Technology by dmacias

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org