Skip to content

Commit

Permalink
Added getUserDetails, fixed sticker message and documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
Idan Harel committed Nov 22, 2016
1 parent 36341e5 commit b1926ab
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ Dependencies License (MIT)
[underscore](https://www.npmjs.com/package/underscore)
[body-parser](https://www.npmjs.com/package/body-parser)
[express](https://www.npmjs.com/package/express)
[needle](https://www.npmjs.com/package/needle)
[needle](https://www.npmjs.com/package/needle)
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ An event emitter, emitting events [described here](#onEvent).

* [ViberBot](#ViberBot)
* [new ViberBot()](#newViberBot)
* [.getProfile()](#getProfile)`promise.JSON`
* [.getBotProfile()](#getBotProfile)`promise.JSON`
* [.getUserDetails(userProfile)](#getUserDetails)`promise.JSON`
* [.setWebhook(url)](#setWebhook)`promise.JSON`
* [.sendMessage(userProfile, messages, [optionalTrackingData])](#sendMessage)`promise.ARRAY`
* [.on(handler)](#onEvent)
Expand Down Expand Up @@ -148,11 +149,23 @@ bot.on(BotEvents.SUBSCRIBED, response =>
response.send(`Thanks for subscribing, ${response.userProfile.name}`));
```

<a name="getProfile"></a>
### bot.getProfile()
<a name="getBotProfile"></a>
### bot.getBotProfile()
Returns a `promise.JSON` ([With the following JSON](https://developers.viber.com/customer/en/portal/articles/2541122-get-account-info?b_id=15145)). **Example**
```js
bot.getProfile().then(response => console.log(`Public Account Named: ${response.name}`));
bot.getBotProfile().then(response => console.log(`Public Account Named: ${response.name}`));
```

<a name="getUserDetails"></a>
### bot.getUserDetails(userProfile)
| Param | Type | Description |
| --- | --- | --- |
| userProfile | [`UserProfile`](#UserProfile) | `UserProfile` object |

Returns a `promise.JSON`. **Example**
```js
bot.onSubscribe(response => bot.getUserDetails(response.userProfile)
.then(userDetails => console.log(userDetails)));
```

<a name="setWebhook"></a>
Expand Down Expand Up @@ -243,7 +256,8 @@ bot.onError(err => logger.error(err));
bot.onConversationStarted((userProfile, onFinish) =>
onFinish(new TextMessage(`Hi, ${userProfile.name}! Nice to meet you.`)));

bot.onConversationStarted((userProfile, onFinish) => onFinish(new TextMessage(`Thanks`), { saidThanks: true }));
bot.onConversationStarted((userProfile, onFinish) =>
onFinish(new TextMessage(`Thanks`), { saidThanks: true }));
```

<a name="onSubscribe"></a>
Expand Down
6 changes: 0 additions & 6 deletions lib/message/message-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ function MessageFactory(logger) {

MessageFactory.prototype.createMessageFromJson = function(json) {
let messageType = json.message.type.toLowerCase();

// Sticker messages are received as picture messages for the time being until the API will support sticker IDs.
if (messageType == StickerMessage.getType()) {
messageType = PictureMessage.getType();
}

if (!_.has(this._mapping, messageType)) {
this._logger.debug(`Could not build message from type ${messageType}. No mapping found`);
return;
Expand Down
6 changes: 5 additions & 1 deletion lib/viber-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ function ViberBot(logger, configuration) {
}
util.inherits(ViberBot, EventEmitter);

ViberBot.prototype.getProfile = function() {
ViberBot.prototype.getBotProfile = function() {
return this._client.getAccountInfo();
};

ViberBot.prototype.getUserDetails = function(userProfile) {
return this._client.getUserDetails(userProfile.id).then(response => Promise.resolve(response.user));
};

ViberBot.prototype.setWebhook = function(url) {
return this._client.setWebhook(url);
};
Expand Down
23 changes: 23 additions & 0 deletions lib/viber-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ const util = require('util');

const SUCCESSFUL_REQUEST_STATUS = 0;
const VIBER_AUTH_TOKEN_HEADER = "X-Viber-Auth-Token";
const MAX_GET_ONLINE_IDS = 100;
const API_ENDPOINTS = {
"setWebhook": "/set_webhook",
"getAccountInfo": "/get_account_info",
"getUserDetails": "/get_user_details",
"getOnlineStatus": "/get_online",
"sendMessage": "/send_message"
};

Expand All @@ -28,6 +31,10 @@ ViberClient.prototype.setWebhook = function(url) {
};

ViberClient.prototype.sendMessage = function(toViberUserId, messageType, messageData, optionalTrackingData, optionalKeyboard) {
if (!toViberUserId || !messageType || !messageData) {
throw new Error(`Invalid arguments passed to sendMessage. to, type and message are required`);
}

const request = {
"receiver": toViberUserId,
"sender": {
Expand All @@ -47,6 +54,22 @@ ViberClient.prototype.getAccountInfo = function() {
return this._sendRequest("getAccountInfo", {});
};

ViberClient.prototype.getUserDetails = function(viberUserId) {
if (!viberUserId) throw new Error(`Missing user id`);
return this._sendRequest("getUserDetails", { "id": viberUserId });
};

ViberClient.prototype.getOnlineStatus = function(viberUserIds) {
viberUserIds = _.isArray(viberUserIds) ? viberUserIds : [viberUserIds];

if (_.isEmpty(viberUserIds)) throw new Error(`Empty or no user ids passed to getOnlineStatus`);
if (_.size(viberUserIds) > MAX_GET_ONLINE_IDS) {
throw new Error(`Can only check up to ${MAX_GET_ONLINE_IDS} ids per request`);
}

return this._sendRequest("getOnlineStatus", { "ids": viberUserIds });
};

ViberClient.prototype._sendRequest = function(endpoint, data) {
if (!_.has(API_ENDPOINTS, endpoint)) {
throw new Error(`could not find endpoint ${endpoint}`);
Expand Down
6 changes: 3 additions & 3 deletions test/message/message-factory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ exports.messageFactory = {
"name": "Idan Harel",
"avatar": "https://share.viber.com/download_photo?dlid=66adab37fe13aa4a731249d8a10a2fb8e5c339765661876e3d963083369a6a06%26fltp=jpg%26imsz=0000"
},
"message": { "type": "sticker", "media": "http://viber.com" }
"message": { "type": "sticker", "sticker_id": 5536 }
};

const message = new MessageFactory().createMessageFromJson(stickerMessage);

test.ok(message instanceof PictureMessage, "message should be of type PictureMessage");
test.deepEqual(message.toJson(), { type: 'picture', text: null, media: 'http://viber.com', thumbnail: null });
test.ok(message instanceof StickerMessage, "message should be of type StickerMessage");
test.deepEqual(message.toJson(), { type: 'sticker', sticker_id: 5536 });
test.done();
}
};

0 comments on commit b1926ab

Please sign in to comment.