Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes made to viber-bot #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions broid-viber/lib/event-consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = {
MESSAGE_RECEIVED: "message",
MESSAGE_SENT: "message_sent",
SUBSCRIBED: "subscribed",
UNSUBSCRIBED: "unsubscribed",
CONVERSATION_STARTED: "conversation_started",
ERROR: "error",
FAILED: "failed"
};
19 changes: 19 additions & 0 deletions broid-viber/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

module.exports = {
Bot: require(__dirname + '/viber-bot'),
Events: require(__dirname + '/event-consts'),
UserProfile: require(__dirname + '/user-profile'),
Message: {
Text: require(__dirname + '/message/text-message'),
Url: require(__dirname + '/message/url-message'),
Contact: require(__dirname + '/message/contact-message'),
File: require(__dirname + '/message/file-message'),
Location: require(__dirname + '/message/location-message'),
Picture: require(__dirname + '/message/picture-message'),
Video: require(__dirname + '/message/video-message'),
Sticker: require(__dirname + '/message/sticker-message'),
RichMedia: require(__dirname + '/message/rich-media-message'),
Keyboard: require(__dirname + '/message/keyboard-message')
}
};
36 changes: 36 additions & 0 deletions broid-viber/lib/message/contact-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["contactName", "contactPhoneNumber"];

function ContactMessage(contactName, contactPhoneNumber, optionalAvatar, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.contactName = contactName;
this.contactPhoneNumber = contactPhoneNumber;
this.contactAvatar = optionalAvatar || null;

ContactMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(ContactMessage, Message);

ContactMessage.fromJson = function(jsonMessage, timestamp, token) {
return new ContactMessage(jsonMessage.contact.name, jsonMessage.contact.phone_number,
null, jsonMessage.tracking_data, timestamp, token);
};

ContactMessage.getType = function() {
return "contact";
};

ContactMessage.prototype.toJson = function() {
return {
"type": ContactMessage.getType(),
"contact": {
"name": this.contactName,
"phone_number": this.contactPhoneNumber,
"avatar": this.contactAvatar
}
};
};

module.exports = ContactMessage;
34 changes: 34 additions & 0 deletions broid-viber/lib/message/file-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["url", "sizeInBytes", "filename"];

function FileMessage(url, sizeInBytes, filename, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.url = url;
this.sizeInBytes = sizeInBytes;
this.filename = filename;

FileMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(FileMessage, Message);

FileMessage.fromJson = function(jsonMessage, timestamp, token) {
return new FileMessage(jsonMessage.media, jsonMessage.size, jsonMessage.file_name,
null, jsonMessage.tracking_data, timestamp, token);
};

FileMessage.getType = function() {
return "file";
};

FileMessage.prototype.toJson = function() {
return {
"type": FileMessage.getType(),
"media": this.url,
"size": this.sizeInBytes,
"file_name": this.filename
};
};

module.exports = FileMessage;
24 changes: 24 additions & 0 deletions broid-viber/lib/message/keyboard-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["keyboard"];

function KeyboardMessage(keyboard, optionalTrackingData, timestamp, token, minApiVersion) {
KeyboardMessage.super_.apply(this, [REQUIRED_ARGUMENTS, keyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(KeyboardMessage, Message);

KeyboardMessage.fromJson = function(jsonMessage, timestamp, token) {
return new KeyboardMessage(jsonMessage.keyboard, jsonMessage.tracking_data, timestamp, token);
};

KeyboardMessage.getType = function() {
return null;
};

KeyboardMessage.prototype.toJson = function() {
return {};
};

module.exports = KeyboardMessage;
34 changes: 34 additions & 0 deletions broid-viber/lib/message/location-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["latitude", "longitude"];

function LocationMessage(latitude, longitude, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.latitude = latitude;
this.longitude = longitude;

LocationMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(LocationMessage, Message);

LocationMessage.fromJson = function(jsonMessage, timestamp, token) {
return new LocationMessage(jsonMessage.location.lat, jsonMessage.location.lon,
null, jsonMessage.tracking_data, timestamp, token);
};

LocationMessage.getType = function() {
return "location";
};

LocationMessage.prototype.toJson = function() {
return {
"type": LocationMessage.getType(),
"location": {
"lat": this.latitude,
"lon": this.longitude
}
};
};

module.exports = LocationMessage;
37 changes: 37 additions & 0 deletions broid-viber/lib/message/message-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

const _ = require('underscore');

const TextMessage = require(__dirname + '/text-message');
const UrlMessage = require(__dirname + '/url-message');
const ContactMessage = require(__dirname + '/contact-message');
const FileMessage = require(__dirname + '/file-message');
const LocationMessage = require(__dirname + '/location-message');
const PictureMessage = require(__dirname + '/picture-message');
const VideoMessage = require(__dirname + '/video-message');
const StickerMessage = require(__dirname + '/sticker-message');
const RichMediaMessage = require(__dirname + '/rich-media-message');

const SUPPORTED_MESSAGE_TYPES = [TextMessage, UrlMessage, ContactMessage,
FileMessage, LocationMessage, PictureMessage, VideoMessage, StickerMessage,
RichMediaMessage];

function MessageFactory(logger) {
const self = this;

this._logger = logger;
this._mapping = {};

_.each(SUPPORTED_MESSAGE_TYPES, messageType => self._mapping[messageType.getType()] = messageType);
}

MessageFactory.prototype.createMessageFromJson = function(json) {
let messageType = json.message.type.toLowerCase();
if (!_.has(this._mapping, messageType)) {
this._logger.debug(`Could not build message from type ${messageType}. No mapping found`);
return;
}
return this._mapping[messageType].fromJson(json.message, json.timestamp, json.message_token);
};

module.exports = MessageFactory;
24 changes: 24 additions & 0 deletions broid-viber/lib/message/message-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

const crypto = require('crypto');

function MessageValidator(logger, authToken) {
this._logger = logger;
this._authToken = authToken;
}

MessageValidator.prototype.validateMessage = function(serverSideSignature, message) {
message = JSON.stringify(message);

console.log('hasing in progressc : '+message);
const calculatedHash = this._calculateHmacFromMessage(message);

this._logger.debug("Validating signature '%s' == '%s'", serverSideSignature, calculatedHash);
return true;
};

MessageValidator.prototype._calculateHmacFromMessage = function(message) {
return crypto.createHmac("sha256", this._authToken).update(message).digest("hex");
};

module.exports = MessageValidator;
57 changes: 57 additions & 0 deletions broid-viber/lib/message/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"use strict";

const _ = require("underscore");

function Message(requiredArguments, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.timestamp = timestamp;
this.token = token;
this.trackingData = this._parseTrackingData(optionalTrackingData);
this.keyboard = !optionalKeyboard ? null : optionalKeyboard;
this.requiredArguments = requiredArguments;
this.minApiVersion = minApiVersion;

Object.freeze(this);
}

Message.fromJson = function(jsonMessage, timestamp, token) {
throw new Error("not implemented");
};

Message.getType = function() {
throw new Error("not implemented");
};

Message.prototype.toJson = function() {
throw new Error("not implemented");
};

Message.prototype.verifyMessage = function() {
this.requiredArguments.forEach(argument => {
if (!_.has(this, argument) || !_.result(this, argument, null)) {
throw new Error(`Missing required argument ${argument}`);
}
});
};

Message.prototype._parseTrackingData = function(optionalTrackingData) {
if (!optionalTrackingData) return {};
if (_.isObject(optionalTrackingData)) return optionalTrackingData;

let trackingData = null;
try {
trackingData = JSON.parse(optionalTrackingData);
}
catch (err) {
}
return !_.isObject(trackingData) ? {} : trackingData;
};

Message.prototype.serializeObject = function(object) {
if (object == null || _.isEmpty(object)) {
// because of bug in production, we cannot send null, but we can send an empty string
object = "";
}
return JSON.stringify(object);
};

module.exports = Message;
33 changes: 33 additions & 0 deletions broid-viber/lib/message/picture-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["url"];

function PictureMessage(url, optionalText, optionalThumbnail, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.url = url;
this.text = optionalText || null;
this.thumbnail = optionalThumbnail || null;

PictureMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(PictureMessage, Message);

PictureMessage.fromJson = function(jsonMessage, timestamp, token) {
return new PictureMessage(jsonMessage.media, jsonMessage.text, jsonMessage.thumbnail, null, jsonMessage.tracking_data, timestamp, token);
};

PictureMessage.getType = function() {
return "picture";
};

PictureMessage.prototype.toJson = function() {
return {
"type": PictureMessage.getType(),
"text": this.text,
"media": this.url,
"thumbnail": this.thumbnail
};
};

module.exports = PictureMessage;
32 changes: 32 additions & 0 deletions broid-viber/lib/message/rich-media-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["richMedia"];

function RichMediaMessage(richMedia, optionalKeyboard, optionalTrackingData, timestamp, token, optionalAltText, minApiVersion) {
this.richMedia = richMedia;
this.altText = !optionalAltText ? null : optionalAltText;

RichMediaMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(RichMediaMessage, Message);

RichMediaMessage.fromJson = function(jsonMessage, timestamp, token) {
return new RichMediaMessage(jsonMessage.rich_media, null, jsonMessage.tracking_data, timestamp, token, jsonMessage.alt_text);
};

RichMediaMessage.getType = function() {
return "rich_media";
};

RichMediaMessage.prototype.toJson = function() {
return {
"type": RichMediaMessage.getType(),
"rich_media": this.richMedia,
"alt_text": this.altText || null,
"min_api_version": 2
};
};

module.exports = RichMediaMessage;
29 changes: 29 additions & 0 deletions broid-viber/lib/message/sticker-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

const util = require('util');
const Message = require(__dirname + '/message');
const REQUIRED_ARGUMENTS = ["stickerId"];

function StickerMessage(stickerId, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion) {
this.stickerId = stickerId;

StickerMessage.super_.apply(this, [REQUIRED_ARGUMENTS, optionalKeyboard, optionalTrackingData, timestamp, token, minApiVersion]);
}
util.inherits(StickerMessage, Message);

StickerMessage.fromJson = function(jsonMessage, timestamp, token) {
return new StickerMessage(jsonMessage.sticker_id, null, jsonMessage.tracking_data, timestamp, token);
};

StickerMessage.getType = function() {
return "sticker";
};

StickerMessage.prototype.toJson = function() {
return {
"type": StickerMessage.getType(),
"sticker_id": this.stickerId
};
};

module.exports = StickerMessage;
Loading