项目作者: flint-bot

项目描述 :
Cisco Spark API for NodeJS (deprecated in favor of https://github.com/webex/webex-bot-node-framework)
高级语言: JavaScript
项目地址: git://github.com/flint-bot/sparky.git
创建时间: 2016-03-28T04:27:26Z
项目社区:https://github.com/flint-bot/sparky

开源协议:MIT License

下载


node-sparky

NPM

Cisco Spark API for Node JS

This is a Cisco Spark API Library for Node JS. This project aims to simplify interaction with the Spark API while transparently handling more complex operations such as pagination, webhook creation, and webhook authentication. If you have a question, feature request, or have found a bug, please open an issue.

Quick Start

  1. const Spark = require('node-sparky');
  2. const spark = new Spark({ token: '<token>' });
  3. spark.roomsGet(10)
  4. .then(rooms => rooms.forEach(room => console.log(room.title)))
  5. .catch(err => console.error(err));

Features

  • Rate limiting headers
    inspected to adjust request rates based on Cisco Spark API. These are
    automatically re-queued and sent after the retry-after timer expires.
  • File processor for retrieving attachments from room.
  • Returns promises that comply with A+ standards..
  • Handles pagination transparently. (Receive unlimited records)
  • Support for authenticated HMAC-SHA1 webhooks

Using node-sparky as a Node JS Package

This module can be installed via NPM:

  1. npm install node-sparky --save

Using node-sparky webhook event parser in an Express App

  1. const Spark = require('node-sparky');
  2. const express = require('express');
  3. const bodyParser = require('body-parser');
  4. const when = require('when');
  5. const spark = new Spark({
  6. token: '<my token>',
  7. webhookSecret: 'somesecr3t',
  8. });
  9. const port = parseInt(process.env.PORT || '3000', 10);
  10. // add events
  11. spark.on('messages-created', msg => console.log(`${msg.personEmail} said: ${msg.text}`));
  12. const app = express();
  13. app.use(bodyParser.json());
  14. // add route for path that is listening for web hooks
  15. app.post('/webhook', spark.webhookListen());
  16. // start express server
  17. app.listen(port, function() {
  18. // get exisiting webhooks
  19. spark.webhooksGet()
  20. // remove all existing webhooks
  21. .then(webhooks => when.map(webhooks, webhook => spark.webhookRemove(webhook.id)))
  22. // create spark webhook directed back to the externally accessible
  23. // express route defined above.
  24. .then(() => spark.webhookAdd({
  25. name: 'my webhook',
  26. targetUrl: 'https://example.com/webhook',
  27. resource: 'all',
  28. event: 'all',
  29. });
  30. console.log(`Listening on port ${port}`);
  31. });

Using node-sparky in the Browser

You can use node-sparky on the client side browser as well. Simply include
<script src="browser/node-sparky.js"></script> in your page and you can use
node-sparky just as you can with with node-js.

  1. <head>
  2. <title>test</title>
  3. <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  4. <script src="browser/node-sparky.js"></script>
  5. </head>
  6. <body>
  7. <h1>Test</h1>
  8. <div id="messageId"></div>
  9. <script>
  10. $(document).ready(function() {
  11. var spark = new Sparky({
  12. token: '<my token>'
  13. });
  14. var message = {
  15. roomId: '<room id>',
  16. text: 'Hello world!'
  17. };
  18. spark.messageSend(message)
  19. .then(function(res) {
  20. $('#messageId').html(res.id);
  21. })
  22. .catch(function(err) {
  23. console.log(err);
  24. });
  25. });
  26. </script>
  27. </body>
  28. </html>

Note: The above is a simple example. It is not recommended to include the
token in anything client accessible. This would ideally be part of a broader
application that makes use of oauth2 to cross authenticate the user to Spark to
grab their token through a Spark integration should you use node-sparky in the
browser side JS.

Contributing

Build

The README.md and browser/node-sparky.* files are auto-generated from the
files in /lib and /docs. To regenerate these run:

  1. npm run build

Test

Tests require a user token and will not fully run using a bot token. It is
assumed that the user token has Org Admin permissions. If not, certain tests
WILL fail. The tests can be run via:

  1. git clone https://github.com/flint-bot/sparky
  2. cd sparky
  3. npm install
  4. SPARKY_API_TOKEN=someUserTokenHere npm test

Support this Project

Find this project useful? Help suppport the continued development by submitting issues, feature requests, or code. Alternatively, you can…

Buy me a Coffee!

Reference

Classes


Spark


Objects


File : object

File Object



Event : object

Event Object



License : object

License Object



Membership : object

Membership Object



Message : object

Message Object



Organization : object

Organization Object



Person : object

Person Object



Role : object

Role Object



Room : object

Room Object



Team : object

Team Object



TeamMembership : object

Team Membership Object



Webhook : object

Webhook Object



Validator : object

Spark Object Validation



Events


“memberships”

Webhook membership event



“messages”

Webhook messages event



“rooms”

Webhook rooms event



“memberships-created”

Webhook Memberships Created event



“memberships-updated”

Webhook Memberships Updated event



“memberships-deleted”

Webhook Memberships Deleted event



“messages-created”

Webhook Messages Created event



“messages-deleted”

Webhook Messages Deleted event



“rooms-created”

Webhook Rooms Created event



“rooms-updated”

Webhook Rooms Updated event



“request”

Webhook request event



Spark

Kind: global class
Properties

Name Type Description
options Object. Sparky options object

new Spark(options)

Creates a Spark API instance that is then attached to a Spark Account.

Param Type Description
options Object. Sparky options object

Example

  1. const Spark = require('node-sparky');
  2. const spark = new Spark({
  3. token: '<my token>',
  4. webhookSecret: 'somesecr3t',
  5. });
  6. spark.roomsGet(10)
  7. .then(rooms => rooms.forEach(room => console.log(room.title)))
  8. .catch(err => console.log(err);

spark.setToken(token) ⇒ Promise.String

Set/Reset API token used in a Sparky instance. Use this function when needing
to change an expired Token. Returns a fullfiled promise if token is valid,
else returns a rejected promise.

Kind: instance method of Spark
Returns: Promise.String - Token promise

Param Type Description
token String Spark API token

Example

  1. spark.setToken('Tm90aGluZyB0byBzZWUgaGVyZS4uLiBNb3ZlIGFsb25nLi4u')
  2. .then(token => console.log(token))
  3. .catch(err => console.error(err));

Spark.contentGet(contentId) ⇒ Promise.

Returns a File Object specified by Content ID or Content URL.

Kind: static method of Spark
Returns: Promise. - File object

Param Type Description
contentId String Spark Content ID or URL

Example

  1. spark.contentGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(file => console.log('File name: %s', file.name))
  3. .catch(err => console.error(err));

Spark.contentCreate(filePath, [timeout]) ⇒ Promise.

Create File Object from local file path.

Kind: static method of Spark
Returns: Promise. - File

Param Type Default Description
filePath String Path to file
[timeout] Integer 15000 Timeout in ms to read file (optional)

Example

  1. spark.contentCreate('/some/local/file.png')
  2. .then(file => console.log(file.name))
  3. .catch(err => console.error(err));

Spark.eventsGet([eventSearch], [max]) ⇒ Promise.Array.

List events in your organization. Several query parameters
are available to filter the response. Long result sets will be split
into pages. Requires admin permissions in organization.

Kind: static method of Spark
Returns: Promise.Array. - Events Collection

Param Type Description
[eventSearch] Object Spark Event Search Object
[max] Integer Number of records to return (optional)

Example

  1. spark.eventsGet({ resource: 'messages' }, 10)
  2. .then(events => events.forEach(event => console.log(event.data.text)))
  3. .catch(err => console.error(err));

Spark.eventGet(eventId) ⇒ Promise.

Return details of Spark Event by ID.

Kind: static method of Spark
Returns: Promise. - Spark Event object

Param Type Description
eventId String Spark Event ID

Example

  1. spark.eventGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(event => console.log(event.data.text))
  3. .catch(err => console.error(err));

Spark.licensesGet([orgId], [max]) ⇒ Promise.Array.

Returns all Spark Licenses for a given Organization ID. If
no organization ID argument is passed, licenses are returned for the
Organization that the authenticated account is in. If ‘max’ is not
specifed, returns all. Alternativly, you can pass a licenses object
instead of the orgId string.

Kind: static method of Spark
Returns: Promise.Array. - Licenses Collection

Param Type Description
[orgId] String The organization ID to query (optional)
[max] Integer Number of records to return (optional)

Example

  1. spark.licensesGet('Tm90aGluZyB0byBzZWUgaGVy', 10)
  2. .then(licenses => licenses.forEach(license => console.log(license.name)))
  3. .catch(err => console.error(err));

Example

  1. const licenseSearchObj = {
  2. orgId: 'Tm90aGluZyB0byBzZWUgaGVy',
  3. };
  4. spark.licensesGet(licenseSearchObj, 10)
  5. .then(licenses => licenses.forEach(license => console.log(license.name)))
  6. .catch(err => console.error(err));

Spark.licenseGet(licenseId) ⇒ Promise.

Returns a Spark License specified by License ID.

Kind: static method of Spark
Returns: Promise. - License

Param Type Description
licenseId String Spark License ID

Example

  1. spark.licenseGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(license => console.log(license.name))
  3. .catch(err => console.error(err));

Spark.membershipsGet([membershipSearch], [max]) ⇒ Promise.Array.

Returns all Spark Memberships that the authenticated account
is in. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Membership objects

Param Type Description
[membershipSearch] Object Spark Membership Search Object (optional)
[max] Integer Number of records to return

Example

  1. spark.membershipsGet({ roomId: 'Tm90aGluZyB0byBzZWUgaGVy' }, 10)
  2. .then(memberships => memberships.forEach(membership => console.log(membership.id)))
  3. .catch(err => console.error(err));

Spark.membershipGet(membershipId) ⇒ Promise.

Returns Spark Membership by ID.

Kind: static method of Spark
Returns: Promise. - Spark Membership object

Param Type Description
membershipId String Spark Membership ID

Example

  1. spark.membershipGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(membership => console.log(membership.id))
  3. .catch(err => console.error(err));

Spark.membershipAdd(roomId, personEmail, [isModerator]) ⇒ Promise.

Add new Spark Membership given Room ID, email address, and
moderator status. Alternativly, you can pass a membership object as the
only argument.

Kind: static method of Spark
Returns: Promise. - Spark Membership object

Param Type Description
roomId String Spark Room ID
personEmail String Email address of person to add
[isModerator] Boolean True if moderator

Example

  1. spark.membershipAdd('Tm90aGluZyB0byBzZWUgaGVy', 'aperson@company.com')
  2. .then(membership => console.log(membership.id))
  3. .catch(err => console.error(err));

Example

  1. const membershipObj = {
  2. personEmail: 'test@test.com',
  3. roomId: 'Tm90aGluZyB0byBzZWUgaGVy',
  4. isModerator: true,
  5. };
  6. spark.membershipAdd(membershipObj)
  7. .then(membership => console.log(membership.id))
  8. .catch(err => console.error(err));

Spark.membershipUpdate(membership) ⇒ Promise.

Update a Membership.

Kind: static method of Spark
Returns: Promise. - Spark Membership object

Param Type Description
membership Object. Spark Membership object

Example

  1. spark.membershipGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then((membership) => {
  3. membership.isModerator = true;
  4. return spark.membershipUpdate(membership);
  5. )
  6. .then(membership => console.log(membership.isModerator))
  7. .catch(err => console.error(err));

Spark.membershipRemove(membershipId) ⇒ Promise

Remove Spark Membership by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
membershipId String Spark Membership ID

Example

  1. spark.membershipRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Membership removed.'))
  3. .catch(err => console.error(err));

Spark.messagesGet(messageSearch, [max]) ⇒ Promise.Array.

Returns Spark Message Objects. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Message objects

Param Type Description
messageSearch Object Spark Message Search Object
[max] Integer Number of records to return (optional)

Example

  1. spark.messagesGet({roomId: 'Tm90aGluZyB0byBzZWUgaGVy'}, 100)
  2. .then(messages => messages.forEach(message => console.log(message.text)))
  3. .catch(err => console.error(err));

Spark.messageGet(messageId) ⇒ Promise.

Return details of Spark Message by ID.

Kind: static method of Spark
Returns: Promise. - Spark Message object

Param Type Description
messageId String Spark Message ID

Example

  1. spark.messageGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(message => console.log(message.text))
  3. .catch(err => console.error(err));

Spark.messageSend(message, [file]) ⇒ Promise.

Send Spark Message.

Kind: static method of Spark
Returns: Promise. - Spark Message object

Param Type Description
message Object. Spark Message Add Object
[file] Object. File Object to add to message (optional)

Example

  1. const newMessage = {
  2. roomId: 'Tm90aGluZyB0byBzZWUgaGVy',
  3. text: 'Hello World'
  4. };
  5. spark.contentCreate('/some/file/with.ext')
  6. .then(file => spark.messageSend(newMessage, file))
  7. .then(message => console.log(message.id))
  8. .catch(err => console.error(err));

Spark.messageRemove(messageId) ⇒ Promise

Remove Spark Message by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
messageId String Spark Message ID

Example

  1. spark.messageRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Message removed.'))
  3. .catch(err => console.error(err));

Spark.organizationsGet([max]) ⇒ Promise.Array.

Return all Spark Organizations that the authenticated
account is in. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Organization objects

Param Type Description
[max] Integer Number of records to return (optional)

Example

  1. spark.organizationsGet(10)
  2. .then(organizations => organizations.forEach(organization => console.log(organization.id)))
  3. .catch(err => console.error(err));

Spark.organizationGet(orgId) ⇒ Promise.

Return Spark Organization specified by License ID.

Kind: static method of Spark
Returns: Promise. - Spark Organization object

Param Type Description
orgId String Spark Organization ID

Example

  1. spark.organizationGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(organization => console.log(organization.id))
  3. .catch(err => console.error(err));

Spark.peopleGet([personSearch], [max]) ⇒ Promise.Array.

Returns Spark Person Objects. If no arguments are passed and
if the authenticated account is part of an Organization and if
authenticated account is assigned the Role of Organization Admin, returns
all Spark Person objects from the Organizations that the user is in.
Otherwise, the PersonSearch object should contain the key “id”,
“displayName”, or “email” to query. If ‘max’ is not specifed, returns all
matched Person Objects.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Person objects

Param Type Description
[personSearch] Object Spark Person Search Object (optional)
[max] Integer Number of records to return (optional)

Example

  1. spark.peopleGet({ displayName: 'John' }, 10)
  2. .then(people => people.forEach(person => console.log(person.displayName)))
  3. .catch(err => console.error(err));

Spark.personGet(personId) ⇒ Promise.

Returns a Spark Person Object specified by Person ID.

Kind: static method of Spark
Returns: Promise. - Spark Person object

Param Type Description
personId String Spark Person ID

Example

  1. spark.personGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(person => console.log(person.displayName))
  3. .catch(err => console.error(err));

Spark.personMe() ⇒ Promise.

Return the Spark Person Object of the authenticated account.

Kind: static method of Spark
Returns: Promise. - Spark Person object
Example

  1. spark.personMe()
  2. .then(person => console.log(person.displayName))
  3. .catch(err => console.error(err));

Spark.personAdd(person) ⇒ Promise.

Add new Person.

Kind: static method of Spark
Returns: Promise. - Spark Person object

Param Type Description
person Object. Spark Person object

Example

  1. let newPerson = {
  2. emails: ['aperson@company.com'],
  3. displayName: 'Any Person',
  4. firstName: 'Any',
  5. lastName: 'Person',
  6. avatar: 'http://lorempixel.com/400/400/',
  7. orgId: 'Tm90aGluZyB0byBzZWUgaGVy',
  8. roles: ['Tm90aGluZyB0byBzZWUgaGVy'],
  9. licenses: ['Tm90aGluZyB0byBzZWUgaGVy']
  10. };
  11. spark.personAdd(newPerson)
  12. .then(person => console.log(person.displayName))
  13. .catch(err => console.error(err));

Spark.personUpdate(person) ⇒ Promise.

Update a Person.

Kind: static method of Spark
Returns: Promise. - Spark Person object

Param Type Description
person Object. Spark Person object

Example

  1. spark.personGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then((person) => {
  3. person.displayName = 'Another Person';
  4. return spark.personUpdate(person);
  5. })
  6. .then(person => console.log(person.displayName))
  7. .catch(err => console.error(err));

Spark.personRemove(personId) ⇒ Promise

Remove Spark Person by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
personId String Spark Person ID

Example

  1. spark.personRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Person removed.'))
  3. .catch(err => console.error(err));

Spark.rolesGet([max]) ⇒ Promise.Array.

Returns all Spark Roles that the authenticated account is
in. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Role object

Param Type Description
[max] Integer Number of records to return (optional)

Example

  1. spark.rolesGet(10)
  2. .then(roles => roles.forEach(role => console.log(role.name)))
  3. .catch(err => console.error(err));

Spark.roleGet(roleId) ⇒ Promise.

Returns details for a Spark Role pecified by Role ID.

Kind: static method of Spark
Returns: Promise. - Spark Role object

Param Type Description
roleId String Spark Role ID

Example

  1. spark.roleGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(role => console.log(role.name))
  3. .catch(err => console.error(err));

Spark.roomsGet([roomSearch], [max]) ⇒ Promise.Array.

Returns Spark Room Objects. If roomSearch argument is not
passed, returns all Spark Rooms that the authenticated account is in.
If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Room objects

Param Type Description
[roomSearch] Object. Spark Person Search Object (optional)
[max] Integer Number of records to return (optional)

Example

  1. spark.roomsGet({ type: 'group' }, 10)
  2. .then(rooms => rooms.forEach(room => console.log(room.title)))
  3. .catch(err => console.error(err));

Spark.roomGet(roomId) ⇒ Promise.

Returns a Spark Room Object specified by Room ID.

Kind: static method of Spark
Returns: Promise. - Spark Room object

Param Type Description
roomId String Spark Room ID

Example

  1. spark.roomGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(room => console.log(room.title))
  3. .catch(err => console.error(err));

Spark.roomAdd(title, [teamId]) ⇒ Promise.

Add new Spark Room.

Kind: static method of Spark
Returns: Promise. - Spark Room object

Param Type Description
title String Title for a new Room
[teamId] String Team ID (optional)

Example

  1. spark.roomAdd('myroom')
  2. .then(room => console.log(room.title))
  3. .catch(err => console.error(err));

Spark.roomUpdate(room) ⇒ Promise.

Update a Spark Room.

Kind: static method of Spark
Returns: Promise. - Spark Room object

Param Type Description
room Object. Spark Room object

Example

  1. spark.roomGet(Tm90aGluZyB0byBzZWUgaGVy)
  2. .then((room) => {
  3. room.title = 'Another Title';
  4. return spark.roomUpdate(room);
  5. )
  6. .then(room => console.log(room.title))
  7. .catch(err => console.error(err));

Spark.roomRemove(roomId) ⇒ Promise

Remove Spark Room by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
roomId String Spark Room ID

Example

  1. spark.roomRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Room removed.'))
  3. .catch(err => console.error(err));

Spark.teamsGet([max]) ⇒ Promise.Array.

Return all Spark Teams that the authenticated account is in.
If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Teams Collection

Param Type Description
[max] Integer Number of records to return (optional)

Example

  1. spark.teamsGet(10)
  2. .then(teams => teams.forEach(team => console.log(team.name)))
  3. .catch(err => console.error(err));

Spark.teamGet(teamId) ⇒ Promise.

Returns a Spark Team Object specified by Team ID.

Kind: static method of Spark
Returns: Promise. - Team

Param Type Description
teamId String Spark Team ID

Example

  1. spark.teamGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(team => console.log(team.name))
  3. .catch(err => console.error(err));

Spark.teamAdd(name) ⇒ Promise.

Add new Spark Team.

Kind: static method of Spark
Returns: Promise. - Team

Param Type Description
name String Name for new Team

Example

  1. spark.teamAdd('myteam')
  2. .then(team => console.log(team.name))
  3. .catch(err => console.error(err));

Spark.teamUpdate(team) ⇒ Promise.

Update a Team.

Kind: static method of Spark
Returns: Promise. - Team

Param Type Description
team Object. Spark Team Object

Example

  1. spark.teamGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then((team) => {
  3. team.name = 'Another Team';
  4. return spark.teamUpdate(team);
  5. })
  6. .then(team => console.log(team.name))
  7. .catch(err => console.error(err));

Spark.teamRemove(teamId) ⇒ Promise

Remove Spark Team by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
teamId String Spark Team ID

Example

  1. spark.teamRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Team removed.'))
  3. .catch(err => console.error(err));

Spark.teamMembershipsGet(teamId, [max]) ⇒ Promise.Array.

Return all Spark Team Memberships for a specific Team that
the authenticated account is in. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark TeamMembership objects

Param Type Description
teamId String Spark Team Memebership ID
[max] Integer Number of records to return

Example

  1. spark.teamMembershipsGet('Tm90aGluZyB0byBzZWUgaGVy', 100)
  2. .then(tms => tms.forEach(tm => console.log(tm.personEmail)))
  3. .catch(err => console.error(err));

Spark.teamMembershipGet(membershipId) ⇒ Promise.

Return Spark Team Membership specified by Membership ID.

Kind: static method of Spark
Returns: Promise. - Spark TeamMembership object

Param Type Description
membershipId String Spark Membership ID

Example

  1. spark.membershipGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(tm => console.log(tm.personEmail))
  3. .catch(err => console.error(err));

Spark.teamMembershipAdd(teamId, personEmail, isModerator) ⇒ Promise.

Add new Spark Team Membership.

Kind: static method of Spark
Returns: Promise. - Spark TeamMembership object

Param Type Description
teamId String Spark Team Memebership ID
personEmail String Email address of person to add
isModerator Boolean Boolean value to add as moderator

Example

  1. spark.teamMembershipAdd('Tm90aGluZyB0byBzZWUgaGVy', 'aperson@company.com')
  2. .then(tm => console.log(tm.personEmail))
  3. .catch(err => console.error(err));

Example

  1. const teamMembershipObj = {
  2. personEmail: 'test@test.com',
  3. teamId: 'Tm90aGluZyB0byBzZWUgaGVy',
  4. isModerator: true,
  5. };
  6. spark.teamMembershipAdd(teamMembershipObj)
  7. .then(tm => console.log(tm.personEmail))
  8. .catch(err => console.error(err));

Spark.teamMembershipUpdate(teamMembership) ⇒ Promise.

Update a Team Membership.

Kind: static method of Spark
Returns: Promise. - Spark TeamMembership object

Param Type Description
teamMembership object. Spark TeamMembership object

Example

  1. spark.teamMembershipGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then((tm) => {
  3. tm.isModerator = true;
  4. return spark.teamMembershipUpdate(tm);
  5. )
  6. .then(tm => console.log(tm.isModerator))
  7. .catch(err => console.error(err));

Spark.teamMembershipRemove(membershipId) ⇒ Promise

Remove Spark Team Membership by ID..

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
membershipId String Spark Team Membership ID

Example

  1. spark.teamMembershipRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Team Membership removed.'))
  3. .catch(err => console.error(err));

Spark.webhooksGet([webhookSearch], [max]) ⇒ Promise.Array.

Returns all webhooks for authenticated account with optional
search criteria to filter results. If ‘max’ is not specifed, returns all.

Kind: static method of Spark
Returns: Promise.Array. - Array of Spark Webhook objects

Param Type Description
[webhookSearch] Object Webhook Search object
[max] Integer Number of records to return

Example

  1. spark.webhooksGet(10)
  2. .then(webhooks => webhooks.forEach(webhook => console.log(webhook.name)))
  3. .catch(err => console.error(err));

Example

  1. spark.webhooksGet({ name: 'My Awesome Webhook' }, 10)
  2. .then(webhooks => webhooks.forEach(webhook => console.log(webhook.name)))
  3. .catch(err => console.error(err));

Spark.webhookGet(webhookId) ⇒ Promise.

Returns details of Spark Webhook Object specified by Webhook ID.

Kind: static method of Spark
Returns: Promise. - Spark Webhook object

Param Type Description
webhookId String Spark Webhook ID

Example

  1. spark.webhookGet('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(webhook => console.log(webhook.name))
  3. .catch(err => console.error(err));

Spark.webhookAdd(webhookObj) ⇒ Promise.

Add new Webhook.

Kind: static method of Spark
Returns: Promise. - Spark Webhook object

Param Type Description
webhookObj Object. Spark Webhook object

Example

  1. const newWebhook = {
  2. name: 'my webhook',
  3. targetUrl: 'https://example.com/webhook',
  4. resource: 'memberships',
  5. event: 'created',
  6. filter: 'roomId=Tm90aGluZyB0byBzZWUgaGVy'
  7. };
  8. spark.webhookAdd(newWebhook)
  9. .then(webhook => console.log(webhook.name))
  10. .catch(err => console.error(err));

Spark.webhookUpdate(webhookObj) ⇒ Promise.

Update a Webhook.

Kind: static method of Spark
Returns: Promise. - Spark Webhook Object

Param Type Description
webhookObj Object. Spark Webhook Object

Example

  1. spark.webhookGet(Tm90aGluZyB0byBzZWUgaGVy)
  2. .then((webhook) => {
  3. webhook.name = 'Another Webhook';
  4. return spark.webhookUpdate(webhook);
  5. })
  6. .then(webhook => console.log(webhook.name))
  7. .catch(err => console.error(err));

Spark.webhookRemove(webhookId) ⇒ Promise

Remove Spark Webhook by ID.

Kind: static method of Spark
Returns: Promise - Fulfilled promise

Param Type Description
webhookId String Spark Webhook ID.

Example

  1. spark.webhookRemove('Tm90aGluZyB0byBzZWUgaGVy')
  2. .then(() => console.log('Webhook removed.'))
  3. .catch(err => console.error(err));

Spark.webhookAuth(secret, signature, payload) ⇒ Promise.String | Object

Authenticate X-Spark-Signature HMAC-SHA1 Hash.

Kind: static method of Spark
Returns: Promise.String | Object - payload

Param Type Description
secret String Value of secret used when creating webhook
signature String Value of “X-Spark-Signature” from header
payload String \ Object This can either be the json object or a string representation of the webhook’s body json payload

Example

  1. const sig = req.headers['x-spark-signature'];
  2. const secret = 'mySecret';
  3. spark.webhookAuth(secret, sig, req.body)
  4. .then(() => console.log('Webhook is valid');
  5. .catch(err => console.error(err));

Spark.webhookListen() ⇒ webhookHandler

Process request from connect, express, or resitify routes.
Returns function that accepts req, res, and next arguments.

Kind: static method of Spark
Returns: webhookHandler - function
Example

  1. const Spark = require('node-sparky');
  2. const express = require('express');
  3. const bodyParser = require('body-parser');
  4. const when = require('when');
  5. const spark = new Spark({
  6. token: '<my token>',
  7. webhookSecret: 'somesecr3t',
  8. });
  9. const port = parseInt(process.env.PORT || '3000', 10);
  10. // add events
  11. spark.on('messages-created', msg => console.log(`${msg.personEmail} said: ${msg.text}`));
  12. const app = express();
  13. app.use(bodyParser.json());
  14. // add route for path that is listening for web hooks
  15. app.post('/webhook', spark.webhookListen());
  16. // start express server
  17. app.listen(port, function() {
  18. // get exisiting webhooks
  19. spark.webhooksGet()
  20. // remove all existing webhooks
  21. .then(webhooks => when.map(webhooks, webhook => spark.webhookRemove(webhook.id)))
  22. // create spark webhook directed back to the externally accessible
  23. // express route defined above.
  24. .then(() => spark.webhookAdd({
  25. name: 'my webhook',
  26. targetUrl: 'https://example.com/webhook',
  27. resource: 'all',
  28. event: 'all',
  29. });
  30. console.log(`Listening on port ${port}`);
  31. });

webhookListen~webhookHandler(req, [res], [next]) ⇒ Null

Function returned by spark.webhookListen()

Kind: inner method of webhookListen
Returns: Null - null value

Param Type Description
req Object request object
[res] Object response object
[next] function next function

File : object

File Object

Kind: global namespace
Properties

Name Type Description
name String File name
ext String File extension
type String Header [content-type] for file
binary Buffer File contents as binary
base64 String File contents as base64 encoded string

Event : object

Event Object

Kind: global namespace
Properties

Name Type Description
id String Event ID
resource String Event resource
type String Event type
actorId String Person ID that triggered event
orgId String Organzation ID that event occurred in
appId String Application ID
created String Date Event created (ISO 8601)
data Object Event data object

License : object

License Object

Kind: global namespace
Properties

Name Type Description
id String License ID
name String License name
totalUnits Integer Total units of license available
consumedUnits Integer Number of license units consumed

Membership : object

Membership Object

Kind: global namespace
Properties

Name Type Description
id String Membership ID
roomId String Room ID
personId String Person ID
personEmail String Person Email
isModerator Boolean Membership is a moderator
isMonitor Boolean Membership is a monitor
created String Date Membership created (ISO 8601)

Message : object

Message Object

Kind: global namespace
Properties

Name Type Description
id String Message ID
roomId String Room ID
roomType String Room Type
toPersonId String Person ID
toPersonEmail String Person Email
text String Message text
markdown String Message markdown
files Array. Array of File URLs
personId String Person ID
personEmail String Person Email
created String Date Message created (ISO 8601)
mentionedPeople Array.String Person IDs of those mentioned in Message

Organization : object

Organization Object

Kind: global namespace
Properties

Name Type Description
id String Organization ID
displayName String Organization name
created String Date Organization created (ISO 8601)

Person : object

Person Object

Kind: global namespace
Properties

Name Type Description
id String Person ID
emails Array.String Array of email addresses
displayName String Display name
firstName String First name
lastName String Last name
avatar String Avatar URL
orgId String Organization ID
roles Array.String Array of assigned Role IDs
licenses Array.String Array of assigned License IDs
created String Date created (ISO 8601)

Role : object

Role Object

Kind: global namespace
Properties

Name Type Description
id String Role ID
name String Role name

Room : object

Room Object

Kind: global namespace
Properties

Name Type Description
id String Room ID
title String Room Title
type String Room Type
isLocked Boolean Room Moderated/Locked
teamId String Team ID
lastActivity String Last Activity in Room (ISO 8601)
creatorId String person ID of Room creator (ISO 8601)
created String Room Created (ISO 8601)

Team : object

Team Object

Kind: global namespace
Properties

Name Type Description
id String Message ID
name String Team name
created String Date Team created (ISO 8601)

TeamMembership : object

Team Membership Object

Kind: global namespace
Properties

Name Type Description
id String Membership ID
teamId String Team ID
personId String Person ID
personEmail String Person Email
isModerator Boolean Membership is a moderator
created String Date Membership created (ISO 8601)

Webhook : object

Webhook Object

Kind: global namespace
Properties

Name Type Description
id String Webhook ID
name String Webhook name
targetUrl String Webhook target URL
resource String Webhook resource
event String Webhook event
filter String Webhook filter
created String Date Webhook created (ISO 8601)

Validator : object

Spark Object Validation

Kind: global namespace

Validator.isFile(filePath) ⇒ Promise.String

Validate filePath resolves to existing file. Returns fulfilled Promise with
filePath if valid, else returns rejected Promise if not valid.

Kind: static method of Validator
Returns: Promise.String - Absolute path to file

Param Type Description
filePath String Absolute path to file

Validator.isDir(dirPath) ⇒ Promise.String

Validate filePath resolves to existing dir. Returns fulfilled Promise with
dirPath if valid, else returns rejected Promise if not valid.

Kind: static method of Validator
Returns: Promise.String - Absolute path to a directory

Param Type Description
dirPath String Absolute path to a directory

Validator.isToken(token) ⇒ Promise.String

Validate Spark Token is valid by sending request to API to determine if
authorized. Returns fulfilled Promise with token if valid, else returns rejected
Promise if not valid.

Kind: static method of Validator
Returns: Promise.String - Cisco Spark Token

Param Type Description
token String Cisco Spark Token

Validator.isEmail(email) ⇒ Boolean

Validate String is Email.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
email String Email address string

Validator.isEmails(emails) ⇒ Boolean

Validate Emails in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
emails Array Array of Email address string

Validator.isUrl(url) ⇒ Boolean

Validate String is URL.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
url String URL String

Validator.isFilePath(path) ⇒ Boolean

Validate String is File path and not a URL/URI.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
path String String to test

Validator.isOptions(options) ⇒ Boolean

Validate Options object

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
options Object. Validate that object passed includes all valid options for sparky constructor

Validator.isFile(file) ⇒ Boolean

Validate File object

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
file Object. Validate that object passed includes all valid options required in a file object

Validator.isEvent(event) ⇒ Boolean

Validate Spark Event Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
event Event Event object

Validator.isEvents(events) ⇒ Boolean

Validate Spark Event Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
events Array Array of Event objects

Validator.isEventSearch(searchObj) ⇒ Boolean

Validate Spark Event Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj EventSearch EventSearch object

Validator.isLicense(license) ⇒ Boolean

Validate Spark License Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
license License License object

Validator.isLicenses(licenses) ⇒ Boolean

Validate Spark License Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
licenses Array Array of License objects

Validator.isLicenseSearch(searchObj) ⇒ Boolean

Validate Spark License Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj LicenseSearch LicenseSearch object

Validator.isMembership(membership) ⇒ Boolean

Validate Spark Membership Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
membership Membership Membership object

Validator.isMemberships(memberships) ⇒ Boolean

Validate Spark Membership Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
memberships Array Array of Membership objects

Validator.isMembershipSearch(searchObj) ⇒ Boolean

Validate Spark Membership Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj MembershipSearch MembershipSearch object

Validator.isMessage(message) ⇒ Boolean

Validate Spark Message Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
message Message Message object

Validator.isMessages(messages) ⇒ Boolean

Validate Spark Message Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
messages Array Array of Message objects

Validator.isMessageSearch(searchObj) ⇒ Boolean

Validate Spark Message Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj MessageSearch MessageSearch object

Validator.isOrganization(organization) ⇒ Boolean

Validate Spark Organization Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
organization Organization Organization object

Validator.isOrganizations(organizations) ⇒ Boolean

Validate Spark Organizations Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
organizations Array Array of Organization objects

Validator.isPerson(person) ⇒ Boolean

Validate Spark Person Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
person Person Person object

Validator.isPeople(people) ⇒ Boolean

Validate Spark Person Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
people Array Array of Person objects

Validator.isPersonSearch(searchObj) ⇒ Boolean

Validate Spark Person Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj PersonSearch Person Search object

Validator.isRole(role) ⇒ Boolean

Validate Spark Role Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
role Role Role object

Validator.isRoles(roles) ⇒ Boolean

Validate Spark Role Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
roles Array Array of Role objects

Validator.isRoom(room) ⇒ Boolean

Validate Spark Room Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
room Room Room Object

Validator.isRooms(rooms) ⇒ Boolean

Validate Spark Room Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
rooms Array Array of Room objects

Validator.isRoomSearch(searchObj) ⇒ Boolean

Validate Spark Room Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj RoomSearch RoomSearch object

Validator.isTeam(team) ⇒ Boolean

Validate Spark Team Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
team Team Team object

Validator.isTeams(teams) ⇒ Boolean

Validate Spark Team Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
teams Array Array of Team objects

Validator.isTeamMembership(teamMembership) ⇒ Boolean

Validate Spark Team Membership Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
teamMembership TeamMembership TeamMembership object

Validator.isTeamMemberships(teamMemberships) ⇒ Boolean

Validate Spark Team Membership Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
teamMemberships Array Array of TeamMembership objects

Validator.isTeamMembershipSearch(searchObj) ⇒ Boolean

Validate Spark Team Memebership Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj TeamMembershipSearch TeamMembership object

Validator.isWebhook(webhook) ⇒ Boolean

Validate Spark Webhook Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
webhook Webhook Webhook object

Validator.isWebhooks(webhooks) ⇒ Boolean

Validate Spark Webhook Objects in Array.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
webhooks Array Array of Webhook objects

Validator.isWebhookSearch(searchObj) ⇒ Boolean

Validate Spark Webhook Search Object.

Kind: static method of Validator
Returns: Boolean - result

Param Type Description
searchObj WebhookSearch TeamMembership object

“memberships”

Webhook membership event

Kind: event emitted
Properties

Name Type Description
event String Triggered event (created, updated, deleted)
membership Object. Membership Object found in Webhook
reqBody Object. Full Webhook Body Object

“messages”

Webhook messages event

Kind: event emitted
Properties

Name Type Description
event String Triggered event (created, deleted)
message Object. Message Object found in Webhook
reqBody Object. Full Webhook Body Object

“rooms”

Webhook rooms event

Kind: event emitted
Properties

Name Type Description
event String Triggered event (created, updated)
room Object. Room Object found in Webhook
reqBody Object. Full Webhook Body Object

“memberships-created”

Webhook Memberships Created event

Kind: event emitted
Properties

Name Type Description
membership Object. Membership Object found in Webhook
reqBody Object. Full Webhook Body Object

“memberships-updated”

Webhook Memberships Updated event

Kind: event emitted
Properties

Name Type Description
membership Object. Membership Object found in Webhook
reqBody Object. Full Webhook Body Object

“memberships-deleted”

Webhook Memberships Deleted event

Kind: event emitted
Properties

Name Type Description
membership Object. Membership Object found in Webhook
reqBody Object. Full Webhook Body Object

“messages-created”

Webhook Messages Created event

Kind: event emitted
Properties

Name Type Description
message Object. Message Object found in Webhook
reqBody Object. Full Webhook Body Object

“messages-deleted”

Webhook Messages Deleted event

Kind: event emitted
Properties

Name Type Description
message Object. Message Object found in Webhook
reqBody Object. Full Webhook Body Object

“rooms-created”

Webhook Rooms Created event

Kind: event emitted
Properties

Name Type Description
message Object. Room Object found in Webhook
reqBody Object. Full Webhook Body Object

“rooms-updated”

Webhook Rooms Updated event

Kind: event emitted
Properties

Name Type Description
message Object. Room Object found in Webhook
reqBody Object. Full Webhook Body Object

“request”

Webhook request event

Kind: event emitted
Properties

Name Type Description
reqBody Object. Full Webhook Body Object

License

The MIT License (MIT)

Copyright (c) 2016-2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.