vk-bot-api
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -37,8 +37,11 @@ 'use strict'; | ||
return rq(link, { json: true }).catch(function (err) { | ||
console.log(err);return _this2.getLink(); | ||
return rq(link, { json: true }).catch(function () { | ||
return _this2.getLink(); | ||
}).then(function (res) { | ||
if (!res.ts) return _this2.getLink(); | ||
if (res.failed) return _this2.getLink(); | ||
_this2.count++; | ||
link = link.replace(/ts=.*/, 'ts=' + res.ts); // ставим новое время | ||
if (res.failed && res.failed !== 1) return _this2.getLink(); | ||
if (res.updates.length < 1 || !res.updates) { | ||
@@ -45,0 +48,0 @@ return setTimeout(function () { |
152
lib/vk.js
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -86,3 +88,3 @@ | ||
var checkPrefix, getMsg, isChat, chatId, msgId, body, extra, handled, msg, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, r, result, _msg, _msg2, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _r, _result; | ||
var checkPrefix, getMsg, _update, msgId, chatId, body, extra, isChat, handled, msg, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, r, result, _msg, _msg2, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _r, _result; | ||
@@ -103,7 +105,10 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
isChat = update[3] > 2e9; | ||
chatId = update[3]; | ||
msgId = update[1]; | ||
body = update[6]; | ||
extra = update[7]; | ||
/* let chatId = update[3] | ||
let msgId = update[1] | ||
let body = update[6] | ||
let extra = update[7] */ | ||
_update = _slicedToArray(update, 8), msgId = _update[1], chatId = _update[3], body = _update[6], extra = _update[7]; | ||
isChat = chatId > 2e9; | ||
handled = false; | ||
@@ -118,3 +123,3 @@ | ||
))) { | ||
_context.next = 41; | ||
_context.next = 38; | ||
break; | ||
@@ -124,3 +129,3 @@ } | ||
if (!this.msgtype) { | ||
_context.next = 13; | ||
_context.next = 10; | ||
break; | ||
@@ -130,13 +135,13 @@ } | ||
_context.t0 = update; | ||
_context.next = 16; | ||
_context.next = 13; | ||
break; | ||
case 13: | ||
_context.next = 15; | ||
case 10: | ||
_context.next = 12; | ||
return getMsg(msgId); | ||
case 15: | ||
case 12: | ||
_context.t0 = _context.sent; | ||
case 16: | ||
case 13: | ||
msg = _context.t0; | ||
@@ -148,3 +153,3 @@ | ||
if (!body) { | ||
_context.next = 39; | ||
_context.next = 36; | ||
break; | ||
@@ -156,3 +161,3 @@ } | ||
_iteratorError = undefined; | ||
_context.prev = 22; | ||
_context.prev = 19; | ||
@@ -168,14 +173,14 @@ for (_iterator = this.textRegexpCallbacks[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
} | ||
_context.next = 30; | ||
_context.next = 27; | ||
break; | ||
case 26: | ||
_context.prev = 26; | ||
_context.t1 = _context['catch'](22); | ||
case 23: | ||
_context.prev = 23; | ||
_context.t1 = _context['catch'](19); | ||
_didIteratorError = true; | ||
_iteratorError = _context.t1; | ||
case 30: | ||
_context.prev = 30; | ||
_context.prev = 31; | ||
case 27: | ||
_context.prev = 27; | ||
_context.prev = 28; | ||
@@ -186,7 +191,7 @@ if (!_iteratorNormalCompletion && _iterator.return) { | ||
case 33: | ||
_context.prev = 33; | ||
case 30: | ||
_context.prev = 30; | ||
if (!_didIteratorError) { | ||
_context.next = 36; | ||
_context.next = 33; | ||
break; | ||
@@ -197,20 +202,20 @@ } | ||
case 36: | ||
return _context.finish(33); | ||
case 37: | ||
case 33: | ||
return _context.finish(30); | ||
case 38: | ||
case 34: | ||
return _context.finish(27); | ||
case 35: | ||
if (!handled) this.emit('unhandled', msg); | ||
case 39: | ||
_context.next = 84; | ||
case 36: | ||
_context.next = 81; | ||
break; | ||
case 41: | ||
case 38: | ||
if (!( // для спец.сообщений типа создания чата или изменения его названия | ||
isChat && (!this.chats.length || this.chats.includes(chatId)) && extra.source_act // если сообщение специальное | ||
)) { | ||
_context.next = 53; | ||
_context.next = 50; | ||
break; | ||
@@ -220,3 +225,3 @@ } | ||
if (!this.msgtype) { | ||
_context.next = 46; | ||
_context.next = 43; | ||
break; | ||
@@ -226,20 +231,20 @@ } | ||
_context.t2 = update; | ||
_context.next = 49; | ||
_context.next = 46; | ||
break; | ||
case 46: | ||
_context.next = 48; | ||
case 43: | ||
_context.next = 45; | ||
return getMsg(msgId); | ||
case 48: | ||
case 45: | ||
_context.t2 = _context.sent; | ||
case 49: | ||
case 46: | ||
_msg = _context.t2; | ||
this.emit(extra.source_act, _msg); | ||
_context.next = 84; | ||
_context.next = 81; | ||
break; | ||
case 53: | ||
case 50: | ||
if (!( // для пользоватей | ||
@@ -249,3 +254,3 @@ !isChat && (!this.chats.length || this.chats.includes(chatId)) && (this.prefixOnlyForGroups || checkPrefix(body, this.prefix)) | ||
)) { | ||
_context.next = 84; | ||
_context.next = 81; | ||
break; | ||
@@ -255,3 +260,3 @@ } | ||
if (!this.msgtype) { | ||
_context.next = 58; | ||
_context.next = 55; | ||
break; | ||
@@ -261,13 +266,13 @@ } | ||
_context.t3 = update; | ||
_context.next = 61; | ||
_context.next = 58; | ||
break; | ||
case 58: | ||
_context.next = 60; | ||
case 55: | ||
_context.next = 57; | ||
return getMsg(msgId); | ||
case 60: | ||
case 57: | ||
_context.t3 = _context.sent; | ||
case 61: | ||
case 58: | ||
_msg2 = _context.t3; | ||
@@ -278,3 +283,3 @@ | ||
if (!body) { | ||
_context.next = 84; | ||
_context.next = 81; | ||
break; | ||
@@ -286,3 +291,3 @@ } | ||
_iteratorError2 = undefined; | ||
_context.prev = 67; | ||
_context.prev = 64; | ||
@@ -298,14 +303,14 @@ for (_iterator2 = this.textRegexpCallbacks[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
} | ||
_context.next = 75; | ||
_context.next = 72; | ||
break; | ||
case 71: | ||
_context.prev = 71; | ||
_context.t4 = _context['catch'](67); | ||
case 68: | ||
_context.prev = 68; | ||
_context.t4 = _context['catch'](64); | ||
_didIteratorError2 = true; | ||
_iteratorError2 = _context.t4; | ||
case 75: | ||
_context.prev = 75; | ||
_context.prev = 76; | ||
case 72: | ||
_context.prev = 72; | ||
_context.prev = 73; | ||
@@ -316,7 +321,7 @@ if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
case 78: | ||
_context.prev = 78; | ||
case 75: | ||
_context.prev = 75; | ||
if (!_didIteratorError2) { | ||
_context.next = 81; | ||
_context.next = 78; | ||
break; | ||
@@ -327,12 +332,12 @@ } | ||
case 81: | ||
return _context.finish(78); | ||
case 82: | ||
case 78: | ||
return _context.finish(75); | ||
case 83: | ||
case 79: | ||
return _context.finish(72); | ||
case 80: | ||
if (!handled) this.emit('unhandled', _msg2); | ||
case 84: | ||
case 81: | ||
case 'end': | ||
@@ -342,3 +347,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this, [[22, 26, 30, 38], [31,, 33, 37], [67, 71, 75, 83], [76,, 78, 82]]); | ||
}, _callee, this, [[19, 23, 27, 35], [28,, 30, 34], [64, 68, 72, 80], [73,, 75, 79]]); | ||
})); | ||
@@ -357,5 +362,4 @@ | ||
* @param {Object} msg Сообщение, в чат с которым надо отправить сообщение | ||
* @param {String} message Текст сообщения | ||
* @param {Object} [params] Все остальные параметры | ||
* @param {Boolean} [forward] Если true, к отправляемому | ||
* сообщению будет прикреплено исходное (по умолчанию false) | ||
* @returns {Promise} | ||
@@ -366,9 +370,9 @@ */ | ||
key: 'sendMessage', | ||
value: function sendMessage(msg, text) { | ||
value: function sendMessage(msg, message) { | ||
var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var isLP = Array.isArray(msg); | ||
var peer = isLP ? msg[3] : msg.chat_id ? msg.chat_id + 2e9 : msg.user_id; | ||
if (this.forward && peer > 2e9) params.forward_messages = msg.id; | ||
params = Object.assign(params, { peer_id: peer, message: text }); | ||
var peer_id = isLP ? msg[3] : msg.chat_id ? msg.chat_id + 2e9 : msg.user_id; | ||
if (this.forward && peer_id > 2e9 && !params.forward_messages) params.forward_messages = msg.id; | ||
params = Object.assign(params, { peer_id: peer_id, message: message }); | ||
return this.API.call('messages.send', params); | ||
@@ -375,0 +379,0 @@ } |
{ | ||
"name": "vk-bot-api", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "Tool to make VK chat bots", | ||
@@ -5,0 +5,0 @@ "main": "./lib/vk.js", |
@@ -27,3 +27,3 @@ ## vk-bot-api | ||
token: '<VK_TOKEN>', | ||
prefix: 'Бот' | ||
prefix: /бот/i | ||
}) | ||
@@ -44,3 +44,3 @@ | ||
## Конструктор бота | ||
Создает событие `message`, когда приходит новое сообщение для бота и дополнительные события chat_create, chat_title_update, chat_photo_update, chat_invite_user, chat_kick_user (для бесед) | ||
Создает событие `message`, когда приходит новое сообщение для бота, 'unhalded', когда приходят собщения, на которые не сработал ни один обработчик сообщений и дополнительные события chat_create, chat_title_update, chat_photo_update, chat_invite_user, chat_kick_user (для бесед) | ||
@@ -88,4 +88,3 @@ Параметры: | ||
// сообщение, которое вернул бот | ||
// (нужно для того, чтобы бот понял, | ||
// отправлять в беседу или пользователю) | ||
// (нужно для определения peer_id и автоматичекой пересылки сообщения в чатах) | ||
@@ -96,3 +95,3 @@ 'Привет с:', | ||
{ | ||
forward_messages: msg.id | ||
attachment: 'photo100172_166443618' | ||
}, | ||
@@ -99,0 +98,0 @@ // дополнительные параметры, см. https://vk.com/dev/messages.send |
43944
766
108