intercom.io.js

Module dependencies.

4  var request = require('request'),
5      qs = require('qs'),
6      _ = require('lodash/dist/lodash.underscore'),
7      debug = require('debug')('intercom.io'),
8  
9  

Represents errors thrown by intercom, see IntercomError.js

9  
10      IntercomError = require('./IntercomError'),
11      url = require('url');
12  
13  
14  

Define some sane default options Right now we only have one endpoint for intercom data

14  
15  var defaultOptions = {
16    endpoint: 'https://api.intercom.io/v1/'
17  };
18  
19  
20  

Intercom constructor.

Params
appId String
  • your app Id
apiKey String
  • your api key
options Object
  • an options object
API
public
26  
27  function Intercom(appId, apiKey, options) {
28  
29  

Overload the contractor Parse out single option objects

29  
30    if (_.isObject(appId) && !_.isString(appId) && appId.apiKey && appId.appId) {
31      apiKey = appId.apiKey;
32      options = _.omit(appId, 'apiKey', 'appId');
33      appId = appId.appId;
34    }
35  
36  
37  

Preform some sane validation checks and throw errors We need the appId

37  
38    if (!appId) {
39      throw new IntercomError('Invalid App ID: ' + appId);
40    }
41  
42  
43  

The api key is also required

42  
43    if (!apiKey) {
44      throw new IntercomError('Invalid API Key: ' + apiKey);
45    }
46  
47  
48  

Copy over the relavant data

47  
48    this.appId = appId;
49    this.apiKey = apiKey;
50  
51  
52  

Extend the defaults

51  
52    this.options = _.defaults(options || {}, Intercom.defaultOptions);
53  
54  
55  

Contruct the endpoint with the correct auth from the appId and apiKey

54  
55    this.endpoint = Intercom.constructEndpoint(this.options.endpoint, appId, apiKey);
56  };
57  
58  
59  

Expose defaultOptions for the intercom library so that this is changable.

60  
61  Intercom.defaultOptions = defaultOptions;
62  
63  
64  

Helper function to Construct the endpoint

Params
endpoint String
  • the base intercom.io endpoint
appId String
  • your app Id
apiKey String
  • your api key
API
public
71  
72  Intercom.constructEndpoint = function(endpoint, appId, apiKey) {
73    var urlObj = url.parse(endpoint),
74        auth = appId + ':' + apiKey;
75  
76  
77  

Check if the endpoint already has auth built in

76  
77    if (urlObj.auth) {
78  
79  

Compare auths and warn developer if different (sanity check)

78  
79      if (urlObj.auth !== auth) {
80        console.warn('Auth provided in endpoint url is different from auth calculated. Assuming endpoint has correct authentication.')
81      }
82  
83      return endpoint;
84    }
85  
86    urlObj.auth = auth;
87  
88  
89  

Return the url

88  
89    return url.format(urlObj);
90  };
91  
92  
93  

Helper method to create an instance easily

Enables use like this:

var intercom = require('intercom.io').create("your_APP_ID", "your_API_key");

Params
appId String
  • your app Id
apiKey String
  • your api key
options Object
  • an options object
API
public
103  
104  Intercom.create = function(appId, apiKey, options) {
105    var intercom = new Intercom(appId, apiKey, options);
106    return intercom;
107  };
108  
109  
110  

The main method that makes all the requests to intercom. This method deals with the intercom api and can be used to make requests to the intercom api.

API
public
115  
116  Intercom.prototype.request = function(method, path, parameters, cb) {
117    debug('Requesting [%s] %s with data %o', method, path, parameters)
118  
119    var url = this.endpoint + path;
120    var requestOptions = {
121      method: method,
122      url: url
123    };
124  
125    if (method === 'GET' || method === 'DELETE') {
126      requestOptions.qs = parameters;
127    } else {
128  
129  

requestOptions.form = parameters;

128  
129      requestOptions.body = qs.stringify(parameters);
130      requestOptions.headers = {
131        'Content-Type': 'application/json'
132      };
133    }
134  
135    return request(requestOptions, function(err, res, data) {
136      var parsed;
137  
138      if (!err && data) {
139        debug('Recieved response %s', data);
140  
141        try {
142          parsed = JSON.parse(data);
143  
144          if (parsed && parsed.error && !err) {
145            err = new Error(data);
146          }
147        } catch (exception) {
148  
149        }
150      }
151  
152      return cb && cb(err, parsed || data);
153    });
154  };
155  
156  
157  

Helper method to create dates for intercom easily

Params
date NumberorDate
  • the date. Leave empty if you want the date set to now
API
public
162  
163  Intercom.prototype.date = function(date) {
164    if (date && _.isDate(date) && date.getTime) {
165      return Math.floor(date.getTime() / 1000);
166    }
167  
168    return Math.floor((new Date(date)) / 1000);
169  };
170  
171  
172  

Users

171  
172  Intercom.prototype.getUsers = function(options, cb) {
173    return this.request('GET', 'users', options, cb);
174  };
175  
176  Intercom.prototype.getUser = function(userObj, cb) {
177    return this.request('GET', 'users', userObj, cb);
178  };
179  
180  Intercom.prototype.createUser = function(userObj, cb) {
181    return this.request('POST', 'users', userObj, cb);
182  };
183  
184  Intercom.prototype.updateUser = function(userObj, cb) {
185    return this.request('PUT', 'users', userObj, cb);
186  };
187  
188  Intercom.prototype.deleteUser = function(userObj, cb) {
189    return this.request('DELETE', 'users', userObj, cb);
190  };
191  
192  
193  

Impressions

192  
193  Intercom.prototype.createImpression = function(userObj, cb) {
194    return this.request('POST', 'users/impressions', userObj, cb);
195  };
196  
197  
198  

Message Threads

197  
198  Intercom.prototype.getMessageThread = function(userObj, cb) {
199    return this.request('GET', 'users/message_threads', userObj, cb);
200  };
201  
202  Intercom.prototype.createMessageThread = function(userObj, cb) {
203    return this.request('POST', 'users/message_threads', userObj, cb);
204  };
205  
206  Intercom.prototype.replyMessageThread = function(userObj, cb) {
207    return this.request('PUT', 'users/message_threads', userObj, cb);
208  };
209  
210  
211  

Notes

210  
211  Intercom.prototype.createNote = function(userObj, cb) {
212    return this.request('POST', 'users/notes', userObj, cb);
213  };
214  
215  
216  

Tags

215  
216  Intercom.prototype.getTag = function(userObj, cb) {
217    return this.request('GET', 'tags', userObj, cb);
218  };
219  
220  Intercom.prototype.createTag = function(userObj, cb) {
221    return this.request('POST', 'tags', userObj, cb);
222  };
223  
224  Intercom.prototype.updateTag = function(userObj, cb) {
225    return this.request('PUT', 'tags', userObj, cb);
226  };
227  
228  
229  

Expose Intercom Library.

230  
231  module.exports = Intercom;
232