This repository has been archived by the owner on Mar 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
256 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# VKBOTAPI | ||
|
||
# Contents | ||
* [api](#api) | ||
* * [call()](#call) | ||
* [msg](#msg) | ||
* * [poll()](#poll) | ||
|
||
# Docs | ||
|
||
## api | ||
* ### call() | ||
|
||
## msg | ||
* ### poll() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { Bot } = require('../index') | ||
|
||
const bot = new Bot({ | ||
token: process.argv[2] | ||
}) | ||
|
||
bot.msg.hear(/hello/i, msg => { | ||
msg.send('hello1') | ||
msg.send('hello2') | ||
msg.send('hello3') | ||
msg.send('hello4') | ||
msg.send('hello5') | ||
}) | ||
|
||
bot.msg.poll() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const utils = require('./utils') | ||
|
||
class API { | ||
constructor (parent) { | ||
this.parent = parent | ||
} | ||
|
||
/** | ||
* Запрос к VK API | ||
* @param {String} method Вызываемый метод API | ||
* @param {{}} data Объект параметров { ключ: значение } | ||
* @returns {{}} Ответ сервера в виде JSON | ||
*/ | ||
async call (method, data = {}) { | ||
data.v = data.v || this.parent.v | ||
|
||
const parameters = [] | ||
|
||
for (const key in data) { | ||
parameters.push(`${key}=${data[key]}`) | ||
} | ||
|
||
const url = `https://api.vk.com/method/${method}?${parameters.join('&')}&access_token=${this.parent.token}` | ||
|
||
return JSON.parse(await utils.httpGet(url)) | ||
} | ||
} | ||
|
||
module.exports = { | ||
API | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Message { | ||
constructor (options) { | ||
this.bot = options.bot | ||
|
||
this.regexp = options.regexp | ||
|
||
this.code = options.code | ||
this.messageId = options.messageId | ||
this.flags = options.flags | ||
this.peerId = options.peerId | ||
this.timestamp = options.timestamp | ||
this.roomName = options.roomName | ||
this.text = options.text | ||
this.attachments = options.attachments | ||
|
||
this.match = this.text.match(this.regexp) | ||
} | ||
|
||
async send (text) { | ||
const msg = await this.bot.api.call('messages.send', { | ||
peer_id: this.peerId, | ||
message: text, | ||
random_id: Math.random() | ||
}) | ||
|
||
if (msg.error) { | ||
switch (msg.error.error_code) { | ||
case 14: | ||
setTimeout(() => this.send(...arguments), 1000) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
Message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.