Skip to content

Commit

Permalink
Merge pull request #20 from messagebird/add-contacts-and-groups
Browse files Browse the repository at this point in the history
Add contacts and groups
  • Loading branch information
epels authored Sep 28, 2018
2 parents 137cb82 + 03a6b8c commit 5bd619e
Show file tree
Hide file tree
Showing 4 changed files with 879 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "10"
- "9"
- "8"
- "7"
- "6"
- "5"
Expand All @@ -10,3 +13,7 @@ node_js:
- "iojs"
env:
- MB_ACCESSKEY="test_iQpAp0KCs5GCsMpDhIx2leuNB"
matrix:
allow_failures:
- node_js: "0.8"
- node_js: "iojs"
301 changes: 299 additions & 2 deletions lib/messagebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = function (accessKey, timeout) {
}
};

if (options.method === 'POST' || options.method === 'PUT') {
if (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') {
body = JSON.stringify(params);
options.headers['Content-Type'] = 'application/json';
options.headers['Content-Length'] = Buffer.byteLength(body, 'utf8');
Expand Down Expand Up @@ -120,7 +120,7 @@ module.exports = function (accessKey, timeout) {
.toString()
.trim();

if (method === 'DELETE' && response.statusCode === 204) {
if (response.statusCode === 204) {
doCallback(null, true);
return;
}
Expand Down Expand Up @@ -395,6 +395,303 @@ module.exports = function (accessKey, timeout) {
httpRequest('POST', '/lookup/' + phoneNumber + '/hlr', params, callback);
}
}
},

contacts: {

/**
* Create a new contact. Params is optional.
*
* @param {String} phoneNumber
* @param {Object} params
* @param {Function} callback
* @return void
*/
create: function (phoneNumber, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}

params.msisdn = phoneNumber;

httpRequest('POST', '/contacts', params, callback);
},

/**
* Deletes an existing contact. The callback is invoked with an error if
* applicable, but the data will never contain anything meaningful as the
* API returns an empty response for successful deletes.
*
* @param {String} id
* @param {Function} callback
* @return void
*/
delete: function (id, callback) {
httpRequest('DELETE', '/contacts/' + id, callback);
},

/**
* Lists existing contacts. Pagination is optional. If a limit is set, an
* offset is also required.
*
* @param {Number} limit
* @param {Number} offset
* @param {Function} callback
* @return void
*/
list: function (limit, offset, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
limit: limit,
offset: offset
};
} else {
callback = limit;
}

httpRequest('GET', '/contacts', params, callback);
},

/**
* View an existing contact.
*
* @param {String} id
* @param {Function} callback
* @return void
*/
read: function (id, callback) {
httpRequest('GET', '/contacts/' + id, callback);
},

/**
* Updates an existing contact. Params is optional.
*
* @param {String} id
* @param {String} name
* @param {Object} params
* @param {Function} callback
* @return void
*/
update: function (id, params, callback) {
httpRequest('PATCH', '/contacts/' + id, params, callback);
},

/**
* Lists the groups a contact is part of.
*
* @param {String} contactId
* @param {Number} limit
* @param {Number} offset
* @param {Function} callback
* @return void
*/
listGroups: function (contactId, limit, offset, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
limit: limit,
offset: offset
};
} else {
callback = limit;
}

httpRequest('GET', '/contacts/' + contactId + '/groups', params, callback);
},

/**
* Lists the messages for a contact.
*
* @param {String} contactId
* @param {Number} limit
* @param {Number} offset
* @param {Function} callback
* @return void
*/
listMessages: function (contactId, limit, offset, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
limit: limit,
offset: offset
};
} else {
callback = limit;
}

httpRequest('GET', '/contacts/' + contactId + '/messages', params, callback);
}

},

groups: {

/**
* Creates a new group. Params is optional.
*
* @param {String} name
* @param {Object} params
* @param {Function} callback
* @return void
*/
create: function (name, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}

params.name = name;

httpRequest('POST', '/groups', params, callback);
},

/**
* Deletes an existing group. The callback is invoked with an error if
* applicable, but the data will never contain anything meaningful as the
* API returns an empty response for successful deletes.
*
* @param {String} id
* @param {Function} callback
* @return void
*/
delete: function (id, callback) {
httpRequest('DELETE', '/groups/' + id, callback);
},

/**
* Lists existing groups. Pagination is optional. If a limit is set, an
* offset is also required.
*
* @param {Number} limit
* @param {Number} offset
* @param {Function} callback
* @return void
*/
list: function (limit, offset, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
limit: limit,
offset: offset
};
} else {
callback = limit;
}

httpRequest('GET', '/groups', params, callback);
},

/**
* View an existing group.
*
* @param {String} id
* @param {Function} callback
* @return void
*/
read: function (id, callback) {
httpRequest('GET', '/groups/' + id, callback);
},

/**
* Updates an existing contact. Parmas is optional.
*
* @param {String} id
* @param {String} name
* @param {Object} params
* @param {Function} callback
* @return void
*/
update: function (id, name, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}

params.name = name;

httpRequest('PATCH', '/groups/' + id, params, callback);
},

/**
* Adds anywhere from 1 to 50 contacts to a group.
*
* @param {String} groupId
* @param {String[]} contactIds
* @param {Function} callback
* @return void
*/
addContacts: function (groupId, contactIds, callback) {
// We need to make a PUT request with a body formatted like:
// `ids[]=contact-id&ids[]=other-contact-id`. The httpRequest method
// encodes all request bodies to JSON though.
//
// Instead, we'll send a GET request and pass a _method=PUT parameter
// that will ask the API to handle our request as a PUT. We can then
// provide the contact IDs in the query string.
var query = this.getAddContactsQueryString(contactIds);

httpRequest('GET', '/groups/' + groupId + '?' + query, null, callback);
},

getAddContactsQueryString: function (contactIds) {
// Map the contact IDs to the
// `_method=PUT&ids[]=contact-id&ids[]=other-contact-id` format. See
// docs in addContacts and:
// * https://developers.messagebird.com/docs/alternatives
// * https://developers.messagebird.com/docs/groups#add-contact-to-group
var params = [];

params.push('_method=PUT');
for (var i = 0; i < contactIds.length; i++) {
params.push('ids[]=' + contactIds[i]);
}

return params.join('&');
},

/**
* Lists the contacts that are part of a group.
*
* @param {String} groupId
* @param {Number} limit
* @param {Number} offset
* @param {Function} callback
* @return void
*/
listContacts: function (groupId, limit, offset, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
limit: limit,
offset: offset
};
} else {
callback = limit;
}

httpRequest('GET', '/groups/' + groupId + '/contacts', params, callback);
},

/**
* Removes a single contact from a group.
*
* @param {String} groupId
* @param {String} contactId
* @param {Function} callback
* @return void
*/
removeContact: function (groupId, contactId, callback) {
httpRequest('DELETE', '/groups/' + groupId + '/contacts/' + contactId, callback);
}

}

};
};
Loading

0 comments on commit 5bd619e

Please sign in to comment.