lib-vk
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -1,3 +0,6 @@ | ||
const structMessage = require('./utils/messageStruct'); | ||
const structsMessage = require('./utils/messageStruct'); | ||
const unescapeHTML = text => text.replace(/<|>|<br>|&|"/g, word => {return {'<': '<','>': '>','<br>': '\n','&': '&','"': '"'}[word] || word}) | ||
module.exports = | ||
@@ -8,18 +11,39 @@ class Message | ||
{ | ||
const typeMessage = structMessage[message[0]] ? structMessage[message[0]].type : ''; | ||
const typeMessage = structsMessage[message[0]] ? structsMessage[message[0]].type : ''; | ||
const structMessage = structsMessage[message[0]]; | ||
if(typeMessage) structMessage.struct.forEach((key, index) => this[key] = message[index]); else return this.type = null; | ||
if(typeMessage === 'messageNew' || typeMessage === 'messageEdit') | ||
{ | ||
structMessage[message[0]].struct.forEach((key, index) => this[key] = message[index]) | ||
this.text = this.text ? this.text.replace(/<br>/g, '\n') : this.text; | ||
(!this.peer_id > 2e9) && (this.conversation_message_id = 0) | ||
this.text = this.text ? unescapeHTML(this.text) : this.text; | ||
this.isChat = this.peerId > 2e9; | ||
if(this.isChat) this.chatId = this.peerId - 2e9 | ||
message[6] && ((this.senderId = message[6].from ?? null) ?? (this.senderId = this.peerId)); | ||
if(message[6] && message[6].source_act) | ||
{ | ||
this.event = { | ||
eventType: message[6].source_act, | ||
eventUserId: message[6].source_mid | ||
} | ||
message[6].source_message && (this.event.text = unescapeHTML(message[6].source_message)); | ||
message[6].source_chat_local_id && (this.event.conversationMessageId = message[6].source_chat_local_id); | ||
message[6].source_text && (this.event.text = unescapeHTML(message[6].source_text)); | ||
message[6].source_old_text && (this.event.oldText = unescapeHTML(message[6].source_old_text)); | ||
} | ||
this.addition = message[7]; | ||
this.senderId = +this.senderId; | ||
} | ||
else if (typeMessage === 'userOnline' || typeMessage === 'userOffline') | ||
else if (typeMessage === 'userOnline' || typeMessage === 'userOffline') this.userId = -this.userId; | ||
else if(typeMessage === 'changingChatInfo') | ||
{ | ||
structMessage[message[0]].struct.forEach((key, index) => this[key] = message[index]); | ||
this.userId = this.userId * -1; | ||
this.typeEventIsChat = structMessage.typeEventIsChat[this.typeEventIsChat]; | ||
this[structMessage.infoType[message[1]]] = message[3]; | ||
} | ||
else if(typeMessage) | ||
{ | ||
structMessage[message[0]].struct.forEach((key, index) => this[key] = message[index]); | ||
} | ||
@@ -26,0 +50,0 @@ |
@@ -8,7 +8,7 @@ module.exports = | ||
'type', | ||
'id', | ||
'conversation_message_id', | ||
'peer_id', | ||
'messageId', | ||
'minorId', | ||
'peerId', | ||
'date', | ||
'text' | ||
'text', | ||
] | ||
@@ -21,5 +21,5 @@ }, | ||
'type', | ||
'id', | ||
'conversation_message_id', | ||
'peer_id', | ||
'messageId', | ||
'minorId', | ||
'peerId', | ||
'date', | ||
@@ -31,3 +31,3 @@ 'text' | ||
// Reading all outgoing messages in $peerId that arrived before the message with $localId | ||
type: 'ReadingAllOutMessages', | ||
type: 'readingAllOutMessages', | ||
struct: [ | ||
@@ -37,3 +37,2 @@ 'type', | ||
'localId', | ||
] | ||
@@ -65,2 +64,31 @@ }, | ||
}, | ||
// Changing the chat information $peer_id with the type $type_id, $info - additional information about the changes, depends on the type of event. | ||
52: { | ||
type: 'changingChatInfo', | ||
struct: [ | ||
'type', | ||
'typeEventIsChat', | ||
'peerId', | ||
], | ||
typeEventIsChat: { | ||
1: 'newTitleChat', | ||
2: 'newPhotoChat', | ||
3: 'newAdminChat', | ||
4: 'pinnedMessageChat', | ||
5: 'userJoinedChat', | ||
6: 'userLeftChat', | ||
7: 'userExcludedChat', | ||
9: 'userRemovedAdmin' | ||
}, | ||
infoType: { | ||
1: 'info', | ||
2: 'info', | ||
3: 'adminId', | ||
5: 'conversationMessageId', | ||
6: 'userId', | ||
7: 'userId', | ||
8: 'userId', | ||
9: 'userId' | ||
} | ||
}, | ||
/** The user $userId is typing text in the dialog. | ||
@@ -67,0 +95,0 @@ * The event comes once every ~5 seconds when typing. $flags = 1. |
@@ -7,2 +7,3 @@ const axios = require('axios').default; | ||
class VK | ||
@@ -12,11 +13,18 @@ { | ||
{ | ||
if(options.longPoll) | ||
{ | ||
this.groupId = options.longPoll.groupId; | ||
this.token = options.longPoll.token; | ||
} | ||
this.groupId = options.groupId; | ||
this.token = options.token; | ||
this.secret = options.secret ? (callback.use(bodyParser.json()) && callback.listen(80) && options.secret) : options.secret; | ||
this.path = options.path; | ||
if(options.callback) | ||
{ | ||
this.secret = options.callback.secret; | ||
this.path = options.callback.path; | ||
callback.use(bodyParser.json()) && callback.listen(80); | ||
} | ||
// default.. | ||
this.arrayKey = new Map(); | ||
} | ||
@@ -41,2 +49,3 @@ | ||
/** excludes a person or people from the conversation: | ||
@@ -64,3 +73,3 @@ * [constant].chatKick([ids array]) | ||
{ | ||
return this.Query('messages.send', typeof params === 'string' ? {message: params, peer_id: message.peer_id, random_id: 0} : params.chat_id ? params : (params.peer_id ? params : (params.peer_id = message.peer_id) && params)); | ||
return this.Query('messages.send', typeof params === 'string' ? {message: params, peer_id: message.peer_id, random_id: 0} : (params.chat_id ? params : (params.peer_id ? params : (params.peer_id = message.peer_id) && params))); | ||
} | ||
@@ -93,3 +102,3 @@ | ||
if(update.type === 'confirmation') return res.send(this.secret); | ||
!this.arrayKey.has(update.event_id) && (this.eventPush(new Message(update), [type, func]) || this.arrayKey.set(update.event_id)); | ||
!this.arrayKey.has(update.event_id) && (this.eventPush(update, [type, func]) || this.arrayKey.set(update.event_id)); | ||
return res.send('OK'); | ||
@@ -101,5 +110,5 @@ }); | ||
{ | ||
const response = (await axios.get(this.groupId ? server : 'https://' + server, {params: {key: key, act: 'a_check', wait: 25, ts: ts, mode: 128, version: 3 , httpAgent: httpAgent}})).data; | ||
const response = (await axios.get(this.groupId ? server : 'https://' + server, {params: {key: key, act: 'a_check', wait: 25, ts: ts, mode: '2 | 8 | 32 | 64 | 128', version: 3 , httpAgent: httpAgent}})).data; | ||
ts = response.ts; | ||
if(response.updates) for (const update of response.updates) {!this.arrayKey.has(update.event_id ?? update[1]) && (this.eventPush(this.groupId ? update : new Message(update), [type, func]) || this.arrayKey.set(update.event_id ?? update[1]))}; | ||
if(response.updates) for (const update of response.updates) {(!this.callback || !this.arrayKey.has(update.event_id)) && (this.eventPush(this.groupId ? update : new Message(update), [type, func]) || this.arrayKey.set(update.event_id))}; | ||
}; | ||
@@ -134,3 +143,4 @@ } | ||
async loadingMessage(message) { | ||
async loadingMessage(message) | ||
{ | ||
return (await this.Query('messages.getById', {message_ids: [message.id]})).response.items[0]; | ||
@@ -146,3 +156,3 @@ } | ||
{ | ||
key[0].includes(update.type) && key[1](update.object ? update.object.message : update); this.arrayKey.size >= 150 && this.arrayKey.clear() | ||
(key[0].includes(update.type) || !key[0]) && key[1](update.object ? update.object.message : update); this.arrayKey.size >= 150 && this.arrayKey.clear() | ||
} | ||
@@ -149,0 +159,0 @@ } |
{ | ||
"name": "lib-vk", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "Compact SDK with VK API for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "./lib-vk/VK.js", |
142
README.md
@@ -64,98 +64,118 @@ # lib-vk | ||
## The name of all events and their structure for pages | ||
- Adding a new message | ||
``` | ||
// Adding a new message. | ||
* messageNew | ||
{ | ||
'type' | ||
'id' | ||
'conversation_message_id' | ||
'peer_id' | ||
'date' | ||
'text' | ||
'type' {string} | ||
'id' {integer} | ||
'minorId' {integer} | ||
'peerId' {integer} | ||
'date' {integer} | ||
'text' {string} | ||
} | ||
``` | ||
// Edit the message. | ||
- Edit the message | ||
``` | ||
* messageEdit | ||
{ | ||
'type' | ||
'id' | ||
'conversation_message_id' | ||
'peer_id' | ||
'date' | ||
'text' | ||
'type' {string} | ||
'id' {integer} | ||
'minorId' {integer} | ||
'peerId' {integer} | ||
'date' {integer} | ||
'text' {string} | ||
} | ||
``` | ||
// Reading all outgoing messages in $peerId that arrived before the message with $localId | ||
* ReadingAllOutMessages | ||
- Reading all outgoing messages in $peerId that arrived before the message with $localId | ||
``` | ||
* readingAllOutMessages | ||
{ | ||
'type' | ||
'peerId' | ||
'localId' | ||
'type' {string} | ||
'peerId' {integer} | ||
'localId' {integer} | ||
} | ||
``` | ||
// A friend of $userId has become online. The extra contains the platform ID. | ||
// $timestamp — the time of the last action of the user $userId on the site. | ||
- A friend of $userId has become online. The extra contains the platform ID | ||
- $timestamp — the time of the last action of the user $userId on the site | ||
``` | ||
* userOnline | ||
{ | ||
'type' | ||
'userId' | ||
'extra' | ||
'timestamp' | ||
'type' {string} | ||
'userId' {integer} | ||
'extra' {integer} | ||
'timestamp' {integer} | ||
} | ||
``` | ||
// Friend $userId has become offline ($flags is 0 if the user has left the site and 1 if offline by timeout ) | ||
// $timestamp — the time of the last action of the user $userId on the site. | ||
- Friend $userId has become offline ($flags is 0 if the user has left the site and 1 if offline by timeout) | ||
- $timestamp — the time of the last action of the user $userId on the site | ||
``` | ||
* userOffline | ||
{ | ||
'type' | ||
'type' | ||
'userId' | ||
'flags' | ||
'timestamp' | ||
'type' {sting} | ||
'userId' {integer} | ||
'flags' {integer} | ||
'timestamp' {integer} | ||
} | ||
``` | ||
- Changing the chat information $peer_id with the type $type_id, $info - additional information about the changes, depends on the type of event | ||
``` | ||
* changingChatInfo | ||
{ | ||
'type' {string} | ||
'typeEventIsChat' {string} | ||
'peerId' {integer} | ||
'infoType' {string OR integer} | ||
} | ||
``` | ||
// The user $userId is typing text in the dialog. | ||
// The event comes once every ~5 seconds when typing. $flags = 1. | ||
- The user $userId is typing text in the dialog | ||
- The event comes once every ~5 seconds when typing. $flags = 1 | ||
``` | ||
* messageTyping | ||
{ | ||
'type' | ||
'userId' | ||
'flags' | ||
'type' {string} | ||
'userId' {integer} | ||
'flags' {integer} | ||
} | ||
``` | ||
// The user $userId types text in the conversation $chatId. | ||
- The user $userId types text in the conversation $chatId | ||
``` | ||
* messageTypingIsChat | ||
{ | ||
'type' | ||
'userId' | ||
'chatId' | ||
'type' {string} | ||
'userId' {integer} | ||
'chatId' {integer} | ||
} | ||
``` | ||
// Users $userIds type text in the conversation $peerId. | ||
// A maximum of five conversation participants are transmitted, the total number of printers is indicated in $totalCount. | ||
// $ts is the time when this event was generated. | ||
- Users $userIds type text in the conversation $peerId | ||
- A maximum of five conversation participants are transmitted, the total number of printers is indicated in $totalCount | ||
- $ts is the time when this event was generated | ||
``` | ||
* messageTypingsIsChat | ||
{ | ||
'type' | ||
'userIds' | ||
'peerId' | ||
'totalCount' | ||
'ts' | ||
'type' {string} | ||
'userIds' {integer} | ||
'peerId' {integer} | ||
'totalCount' {integer} | ||
'ts' {integer} | ||
} | ||
``` | ||
// Users $userIds record an audio message in the conversation $peerId. | ||
- Users $userIds record an audio message in the conversation $peerId | ||
``` | ||
* recordsAudiomessage | ||
{ | ||
'type' | ||
'userIds' | ||
'peerId' | ||
'totalCount' | ||
'ts' | ||
'type' {string} | ||
'userIds' {integer} | ||
'peerId' {integer} | ||
'totalCount' {integer} | ||
'ts' {integer} | ||
} | ||
@@ -162,0 +182,0 @@ ``` |
17400
297
183