npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ciscospark-websocket-events

v1.2.0

Published

Cisco Spark Websocket Events

Downloads

20

Readme

Cisco Spark Websocket Events

Provides a simple way to get events through Cisco Spark's native websocket.

This module is useful when deploying a Cisco Spark bot behind a firewall with no way to get the traditional inbound webhooks back to the bot.

Installation

npm install ciscospark-websocket-events

Usage

This module can be used in two different ways. The first is by setting an event callback to handle the events directly in your code. The second is to define URL to the location you would like to post event data to.

The current events supported are:

  • Message Created
  • Membership Created
  • Room Updated

Here is an example event:

{
  "event": "created",
  "resource": "messages",
  "data": {
    "id": "Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzkyM2RiNDAtMTU4ZS0xMWU3LWI1OWItMjNiODI4NTFiY2Fh",
    "roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vOTAwYjZiNTEtNDc2ZC0zMjkzLThlMTAtYmI1MTVjN2RjNDQy",
    "roomType": "direct",
    "text": "Hello",
    "personId": "Y2lzY29zcGFyazovL3VzL1BFT1BMRS83M2YwNThiZS01MTRjLTQ5OTAtYTkyZi00MWNlY2M4NWFiMzc",
    "personEmail": "[email protected]",
    "html": "<p><strong>Hello</strong></p>",
    "created": "2017-03-30T21:17:04.628Z"
  }
}

Example 1: using the event handler callback

Run the sample from a terminal:

> cd ciscospark-websocket-events
> npm install
> cd tests
> SPARK_TOKEN=XXXXXXXXXXXXXXXXX node event-callback.js
...

Check the code:

   var SparkWebSocket = require('ciscospark-websocket-events');
   var accessToken = process.env.SPARK_TOKEN;

   sparkwebsocket = new SparkWebSocket(accessToken);
   sparkwebsocket.connect(function (err, res){
      if (!err) {
        sparkwebsocket.setEventCallback(function (event){
          console.log("New Event");
          console.log("---------");
          console.log(JSON.stringify(event, null, 2));
          
          // do something with the event
        });

      }
      else {
        console.log("Error starting up websocket: " + err);
      }
   }):

Example 2: forwarding the event using the webhook_url

   var SparkWebSocket = require('ciscospark-websocket-events');
   var accessToken = process.env.SPARK_TOKEN;
   var webHookUrl =  process.env.WEBHOOK_URL; // http://localhost:8080/mybot/incoming_event

   sparkwebsocket = new SparkWebSocket(accessToken);
   sparkwebsocket.connect(function(err, res){
      if (!err) {
         if (webHookUrl) {
            sparkwebsocket.setWebHookURL(webHookUrl);
         }
      }
      else {
         console.log("Error starting up websocket: " + err);
      }
   });

BotKit Example

Run the sample from a terminal:

> cd examples
> cd botkit
> npm install
> SPARK_TOKEN=XXXXXXXXXXXXXXXXX node bot.js
...

Check the BotKit code sample:

/// Setup the Cisco Spark Websocket

var SparkWebSocket = require('ciscospark-websocket-events')

var accessToken = process.env.SPARK_TOKEN
var PORT = process.env.PORT || 3000

var webHookUrl =  "http://localhost:"+PORT+"/ciscospark/receive"

sparkwebsocket = new SparkWebSocket(accessToken)
sparkwebsocket.connect(function(err,res){
   if (!err) {
         if(webHookUrl)
             sparkwebsocket.setWebHookURL(webHookUrl)
   }
   else {
        console.log("Error starting up websocket: "+err)
   }
})

//////// Bot Kit //////

var Botkit = require('botkit');

var controller = Botkit.sparkbot({
    debug: true,
    log: true,
    public_address: "https://localhost",
    ciscospark_access_token: process.env.SPARK_TOKEN
});


var bot = controller.spawn({
});

controller.setupWebserver(PORT, function(err, webserver) {

 //setup incoming webhook handler
  webserver.post('/ciscospark/receive', function(req, res) {
            res.sendStatus(200)
            controller.handleWebhookPayload(req, res, bot);
  });

});

controller.hears('hello', 'direct_message,direct_mention', function(bot, message) {
    bot.reply(message, 'Hi');
});

controller.on('direct_mention', function(bot, message) {
    bot.reply(message, 'You mentioned me and said, "' + message.text + '"');
});

controller.on('direct_message', function(bot, message) {
    bot.reply(message, 'I got your private message. You said, "' + message.text + '"');
});

Flint Example

Run the sample from a terminal:

> cd examples
> cd flint
> npm install
> SPARK_TOKEN=XXXXXXXXXXXXXXXXX node bot.js
...

Check the flint code sample:

// Spark Websocket Intialization
var SparkWebSocket = require('ciscospark-websocket-events')

var accessToken = process.env.SPARK_TOKEN
var PORT = process.env.PORT || 8080

var webHookUrl =  "http://localhost:"+PORT+"/flint"

sparkwebsocket = new SparkWebSocket(accessToken)
sparkwebsocket.connect(function(err,res){
      if (!err)
      {
        sparkwebsocket.setWebHookURL(webHookUrl)
      }
      else {
        console.log("Error starting up websocket: "+err)
      }

   })

// Flint Bot Initialization

var Flint = require('node-flint');
var webhook = require('node-flint/webhook');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());

// flint options
var config = {
  token: accessToken,
  port: PORT,
  removeWebhooksOnStart: true,
  maxConcurrent: 5,
  minTime: 50
};

// init flint
var flint = new Flint(config);
flint.start();

// say hello
flint.hears('/hello', function(bot, trigger) {
  flind.debug('Flind hears /hello');
  bot.say('I hear you, %s!', trigger.personDisplayName);
});

// add flint event listeners
flint.on('message', function(bot, trigger, id) {
  flint.debug('"%s" said "%s" in room "%s"', trigger.personEmail, trigger.text, trigger.roomTitle);
  if (trigger.text === '/hello') {
    bot.say('"%s" said keyword in room "%s".', trigger.personEmail, trigger.roomTitle);
  }
});

flint.on('initialized', function() {
  flint.debug('initialized %s rooms', flint.bots.length);
});

// define express path for incoming webhooks
app.post('/flint', webhook(flint));

// start express server
var server = app.listen(config.port, function () {
  flint.debug('Flint listening on port %s', config.port);
});

// gracefully shutdown (ctrl-c)
process.on('SIGINT', function() {
  flint.debug('stoppping...');
  server.close();
  flint.stop().then(function() {
    process.exit();
  });
});

Proxy Support

Cisco Spark Websockets now support web proxies!

To enable web proxy support set the following enviroment variables:

  • HTTP_PROXY=(to your web proxy URL)
  • NO_PROXY=localhost