Skip to content

Commit

Permalink
Merge pull request #31 from odrzutowiec/add-conversations
Browse files Browse the repository at this point in the history
fix conversation api paths + add examples
  • Loading branch information
epels authored Jan 23, 2019
2 parents 9aac892 + b641aea commit 9f53ffa
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 15 deletions.
144 changes: 144 additions & 0 deletions examples/conversations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@

var messagebird = require('messagebird')('<YOUR_ACCESS_KEY>');

// start a conversation
messagebird.conversations.start({
'to': '31612345678',
'channelId': '619747f69cf940a98fb443140ce9aed2' ,
'type': 'text',
'content': { 'text': 'Hello!' }
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// list conversations
messagebird.conversations.list(20, 0, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// get conversation
messagebird.conversations.read('46d0cef122574e8da60d9fb9e5de5eb9', function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// update conversation status
messagebird.conversations.update('46d0cef122574e8da60d9fb9e5de5eb9', {
'status': 'archived'
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// reply to conversation
messagebird.conversations.reply('649ca3dc307544e1b56ac1ca55d572d1', {
'type': 'text',
'content': {
'text': 'Hello!'
}
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// get messages in conversation
messagebird.conversations.listMessages('46d0cef122574e8da60d9fb9e5de5eb9', 20, 0, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// get message
messagebird.conversations.readMessage('93e00b69b1094befbd3fe71290469f1a', function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// create webhook
messagebird.conversations.webhooks.create({
'events': [
'message.created',
'message.updated'
],
'channelId': '853eeb5348e541a595da93b48c61a1ae',
'url': 'https://example.com/webhook'
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// list webhooks
messagebird.conversations.webhooks.list(100, 0, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// get webhook
messagebird.conversations.webhooks.read('451e6b72799e4415b2aab425f582f65e', function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// update webhook
messagebird.conversations.webhooks.update('451e6b72799e4415b2aab425f582f65e', {
'status': 'disabled',
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// delete webook
messagebird.conversations.webhooks.delete('451e6b72799e4415b2aab425f582f65e', function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

// send hsm message
messagebird.conversations.reply('6710713c4605456f894b93580e8b5791', {
'type': 'hsm',
'content': {
'hsm': {
'namespace': '5ba2d0b7_f2c6_433b_a66e_57b009ceb6ff',
'templateName': 'order_update',
'language': {
'policy': 'deterministic',
'code': 'en'
},
'params': [
{ 'default': 'Bob' },
{ 'default': 'tomorrow!' }
]
}
}
}, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});

12 changes: 6 additions & 6 deletions lib/messagebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'GET',
path: '/v1/conversations/messages/' + id
path: '/v1/messages/' + id
}, callback);
},

Expand All @@ -643,7 +643,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'POST',
path: '/v1/conversations/webhooks',
path: '/v1/webhooks',
params: params,
}, callback);
},
Expand All @@ -659,7 +659,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'GET',
path: '/v1/conversations/webhooks/' + id,
path: '/v1/webhooks/' + id,
}, callback);
},

Expand All @@ -675,7 +675,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'PATCH',
path: '/v1/conversations/webhooks/' + id,
path: '/v1/webhooks/' + id,
params: params
}, callback);
},
Expand Down Expand Up @@ -703,7 +703,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'GET',
path: '/v1/conversations/webhooks',
path: '/v1/webhooks',
params: params
}, callback);
},
Expand All @@ -719,7 +719,7 @@ module.exports = function (accessKey, timeout) {
httpRequest({
hostname: 'conversations.messagebird.com',
method: 'DELETE',
path: '/v1/conversations/webhooks/' + id,
path: '/v1/webhooks/' + id,
}, callback);
},
}
Expand Down
14 changes: 7 additions & 7 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ queue.push(function () {

queue.push(function () {
nock('https://conversations.messagebird.com')
.get('/v1/conversations/messages/message-id')
.get('/v1/messages/message-id')
.reply(200, {
id: 'message-id',
conversationId: 'conversation-id',
Expand Down Expand Up @@ -739,7 +739,7 @@ queue.push(function () {
};

nock('https://conversations.messagebird.com')
.post('/v1/conversations/webhooks', params)
.post('/v1/webhooks', params)
.reply(200, {
id: 'webhook-id',
url: 'https://example.com/webhook',
Expand All @@ -764,7 +764,7 @@ queue.push(function () {

queue.push(function () {
nock('https://conversations.messagebird.com')
.get('/v1/conversations/webhooks/webhook-id')
.get('/v1/webhooks/webhook-id')
.reply(200, {
id: 'webhook-id',
url: 'https://example.com/webhook',
Expand All @@ -790,7 +790,7 @@ queue.push(function () {
var params = { 'status': 'disabled' };

nock('https://conversations.messagebird.com')
.patch('/v1/conversations/webhooks/webhook-id', params)
.patch('/v1/webhooks/webhook-id', params)
.reply(200, {
id: 'webhook-id',
url: 'https://example.com/webhook',
Expand All @@ -814,7 +814,7 @@ queue.push(function () {

queue.push(function () {
nock('https://conversations.messagebird.com')
.get('/v1/conversations/webhooks')
.get('/v1/webhooks')
.query({
limit: 0,
offset: 30
Expand Down Expand Up @@ -847,7 +847,7 @@ queue.push(function () {

queue.push(function () {
nock('https://conversations.messagebird.com')
.get('/v1/conversations/webhooks')
.get('/v1/webhooks')
.reply(200, {
offset: 0,
limit: 20,
Expand Down Expand Up @@ -876,7 +876,7 @@ queue.push(function () {

queue.push(function () {
nock('https://conversations.messagebird.com')
.delete('/v1/conversations/webhooks/webhook-id')
.delete('/v1/webhooks/webhook-id')
.reply(204, '');

messagebird.conversations.webhooks.delete('webhook-id', function (err) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "messagebird",
"version": "2.3.0",
"version": "2.3.1",
"description": "A node.js wrapper for the MessageBird REST API",
"main": "lib/messagebird.js",
"engines": {
Expand Down

0 comments on commit 9f53ffa

Please sign in to comment.