
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
node-vk-bot
Advanced tools
Create and control VK bots easily.
npm install --save node-vk-bot
If you are cloning this repository, remember to run npm install
to install dependencies.
// ES6
import Bot from 'node-vk-bot'
// ES5
const Bot = require('node-vk-bot')
const bot = new Bot({
token: 'YOUR TOKEN',
prefix: /^Bot[\s,]/
})
bot
.start()
.get(/Hi|Hello|Hey/i, message => {
const forward = { forward_messages: message.id }
bot.send('Hello!', message.peer_id, forward)
})
To get updates from the server, we use polling.
In the example above you can see a super simple VK Bot. This bot will answer our greetings, that's all.
Let's explain the code, it's pretty simple.
bot.start()
the bot starts polling updates from the server.The API is simple as possible, still, if you have any suggestions about simplifying the API, please fill an issue.
The class used to create new bots, it takes a single argument, an options
object.
new Bot({
token: '5a9bdc30ea18ab4a685a8f773642ba0d',
prefix: /^Bot[\s,]/,
prefixOnlyInChats: true,
chats: [
1,
2e9 + 12
]
})
Parameter | Type | Requried |
---|---|---|
token | String | Yes |
prefix | RexExp | No |
prefixOnlyInChats | Boolean | No |
chats | Array | No |
If prefix
is set, the bot will work only with messages with prefix match. (if prefixOnlyInChats
is true
, then prefix will be checked only for messages from group chats)
If chats
is set, the bot will work only with messages from these chats
Starts polling updates from API.
Emits an update
event after getting updates with the response from server.
Update examples.
See poll.js
for more info.
Listens on specific message matching the pattern which can be an string or a regexp.
In case of an string, the message should "start" with the string, that is, the string is converted to new RegExp('^' + string)
. (Attention: it'll not work with prefix)
bot.get(/Hello/i, msg => {
console.log(msg)
})
The argument passed to callback is a Message
object and result of patter.exec(text)
.
Sends message.
bot.send('text', peer_id, params)
Upload a photo.
The only parameter is a path to picture. Returns a Promise that resolves with a photo object
bot.uploadPhoto('~/kittens.png').then(photo => {
console.log(photo)
})
Access VK API.
bot.api('users.get', { user_ids: 1 })
Stops the bot from listening on updates.
bot.stop()
The update event is emitted whenever there is a response from LongPoll.
bot.on('update', update => {
if (update[7].from === 1) {
console.log('Got an message from Pavel Durov!');
}
})
#####command-notfound
This event is emitted whenever there's no .get()
listeners matching
bot.on('command-notfound', msg => {
bot.send('What?', msg.peer_id)
}
Message
Object{
id: Number, // message id
peer_id: Number, // message's chat peer id
date: Number, // time (in Unixtime format)
title: String, // chat title
body: String, // message body
user_id: Number // sender's ID
attachments: Object // vk.com/dev/using_longpoll see the attachments section
}
FAQs
Create and control VK bots easily.
The npm package node-vk-bot receives a total of 11 weekly downloads. As such, node-vk-bot popularity was classified as not popular.
We found that node-vk-bot demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.