aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--.npmignore4
-rw-r--r--CHANGELOG.md5
-rw-r--r--package.json2
-rw-r--r--src/lib/core.js185
5 files changed, 194 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index ae84ba2..1444b04 100644
--- a/.gitignore
+++ b/.gitignore
@@ -58,4 +58,4 @@ typings/
.env
# Compiled files
-lib/ \ No newline at end of file
+/lib
diff --git a/.npmignore b/.npmignore
index f4a567e..18c1054 100644
--- a/.npmignore
+++ b/.npmignore
@@ -58,7 +58,7 @@ typings/
.env
# Source
-src/
+/src/
# Example of use
-example.js \ No newline at end of file
+/example.js
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8ec8eb..ada223c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,9 @@
# Description of releases
+## 0.0.2 (05-06-2017)
+
+Fix:
+ - Fixed exclusion files.
+
## 0.0.1 (05-06-2017)
Release. \ No newline at end of file
diff --git a/package.json b/package.json
index e0b9e69..31438c7 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "iii-for-vk",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Simple automatic reply to incoming messages.",
"homepage": "https://github.com/valentineus/iii-for-vk",
"main": "lib/core.js",
diff --git a/src/lib/core.js b/src/lib/core.js
new file mode 100644
index 0000000..976f458
--- /dev/null
+++ b/src/lib/core.js
@@ -0,0 +1,185 @@
+/**
+ * @project iii-for-vk
+ * @author Valentin Popov <info@valentineus.link>
+ * @license See LICENSE.md file included in this distribution.
+ */
+import urlParseLax from 'url-parse-lax';
+import queryString from 'querystring';
+import iiiClient from 'iii-client';
+import EventEmitter from 'events';
+import inherits from 'inherits';
+import https from 'https';
+import SDK from 'vksdk';
+
+inherits(Bot, EventEmitter);
+module.exports = Bot;
+
+/**
+ * Class representing a Bot.
+ * @class
+ */
+function Bot(opts) {
+ opts = opts || {};
+ var self = this;
+
+ self.uuid = opts.uuid;
+ self.token = opts.token;
+ self.appID = opts.appID;
+ self.appSecret = opts.appSecret;
+}
+
+/**
+ * Initial initialization of all systems and services.
+ * @param {requestCallback} callback - The callback that handles the response.
+ */
+Bot.prototype.init = function(callback) {
+ var self = this;
+
+ // Initialize the connection to the social network
+ self._vk = new SDK({
+ appId: self.appID,
+ appSecret: self.appSecret,
+ https: true,
+ secure: true
+ });
+
+ // Setting the user's token
+ self._vk.setToken(self.token);
+
+ // Starting services
+ self._eventLoop();
+ // Connecting filters
+ self._filterMessages();
+
+ // Connecting to the bot server
+ iiiClient.connect(self.uuid, (raw) => callback(raw.cuid));
+};
+
+/**
+ * Receive a message by its ID.
+ * @param {Number} id - The message ID.
+ * @param {requestCallback} callback - The callback that handles the response.
+ */
+Bot.prototype.getMessageByID = function(id, callback) {
+ id = id || false;
+ var self = this;
+
+ self._vk.request('messages.getById', {
+ message_ids: id,
+ preview_length: 0
+ }, (raw) => {
+ if (raw.error) throw new Error(raw.error.error_msg);
+ callback(raw.response.items.shift());
+ });
+};
+
+/**
+ * Simplifies the sending of a message to the user.
+ * The social network API is used.
+ * More information: https://vk.com/dev/messages.send
+ * @param {Object} options - Object with parameters.
+ * @param {Object} options.user_id - User ID.
+ * @param {Object} options.message - Message text.
+ * @param {requestCallback} callback - The callback that handles the response.
+ */
+Bot.prototype.sendMessageToVK = function(options) {
+ options = options || {};
+ var self = this;
+ self._vk.request('messages.send', options, () => {});
+};
+
+/**
+ * Simplifies sending a message to the bot.
+ * @param {Object} options - Object with parameters.
+ * @param {Object} options.cuid - Session identifier.
+ * @param {Object} options.text - Message text.
+ * @param {requestCallback} callback - The callback that handles the response.
+ */
+Bot.prototype.sendMessageToBot = function(options, callback) {
+ options = options || {};
+ iiiClient.send(options, (answer) => callback(answer));
+};
+
+/**
+ * The event startup service.
+ */
+Bot.prototype._eventLoop = function() {
+ var self = this;
+
+ self._getEvents();
+ self.on('events', (data) => {
+ self._getEvents(data.ts);
+ });
+};
+
+/**
+ * Filter events for incoming messages.
+ * @fires: Bot#messages
+ */
+Bot.prototype._filterMessages = function() {
+ var self = this;
+
+ self.on('events', function(data) {
+ data.updates.filter(function(item) {
+ if (item[0] == 4) self.emit('messages', item);
+ });
+ });
+};
+
+/**
+ * Obtaining the Long Poll server address.
+ * @param {requestCallback} callback - The callback that handles the response.
+ */
+Bot.prototype._getLongPollServer = function(callback) {
+ var self = this;
+
+ self._vk.request('messages.getLongPollServer', {
+ need_pts: false
+ }, (raw) => {
+ if (raw.error) throw new Error(raw.error.error_msg);
+ callback(raw.response);
+ });
+};
+
+/**
+ * Waiting and returning the event.
+ * @fires: Bot#events
+ * @param {String=} [ts] - The ID of the last event.
+ */
+Bot.prototype._getEvents = function(ts) {
+ var self = this;
+
+ // Server address request
+ self._getLongPollServer(function(raw) {
+ ts = ts || raw.ts;
+
+ // Analysis of the connection address
+ var url = urlParseLax(raw.server);
+
+ // Details: https://vk.com/dev/using_longpoll
+ var options = queryString.stringify({
+ act: 'a_check',
+ key: raw.key,
+ ts: ts,
+ wait: 25,
+ mode: 2,
+ version: 1
+ });
+
+ // Configuring the connection
+ var query = {
+ hostname: url.hostname,
+ path: url.pathname + '?' + options
+ };
+
+ https.get(query, function(response) {
+ var answer = {};
+ response.setEncoding('utf8');
+ response.on('data', (data) => answer = data);
+ response.on('end', () => {
+ answer = JSON.parse(answer);
+ self.emit('events', answer);
+ });
+ }).on('error', (error) => Error(error));
+ });
+};