Botact enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
$ npm i https://github.com/Antosik/botact-micro
const bodyParser = require('body-parser')
const express = require('express')
const { Botact } = require('botact-micro')
const app = express()
const bot = new Botact({
confirmation: process.env.CONFIRMATION,
token: process.env.TOKEN
})
bot.event('group_join', ({ reply }) => reply('Thanks!'))
bot.on(({ reply }) => reply('What?'))
app.use(bodyParser.json())
app.post('/', bot.listen)
app.listen(process.env.PORT)
Core
- constructor(settings)
- .api(method, settings)
- .execute(method, settings, callback)
- .reply(user_id, message, attachment)
- .listen(req, res)
Actions
Upload helpers
Botact API: Core β
Create bot.
Botact Flow:
Turn settings.redis
to true, if you will use Botact Flow.
For detailed redis config see this
Definition:
constructor (settings: {
confirmation: string; // required
token: string; // required
group_id?: number;
})
Usage:
const { Botact } = require('botact')
const bot = new Botact({
confirmation: process.env.CONFIRMATION,
token: process.env.TOKEN
})
Call API method (https://vk.com/dev/methods).
Definition:
async api (
method: string, // required
options?: object, // api call parameters
): Promise<any>; // Promise with response/error
Usage:
const user_data = await bot.api('users.get', {
user_ids: 1
})
Call API by execute.
Definition:
async execute (
method: string, // required
options?: object, // api call parameters
callback?: function
): Promise<any>; // Promise with response/error
Usage:
bot.execute('users.get', {
user_ids: 1
}, (body) => {
// {
// response: [{
// id: 1,
// first_name: 'ΠΠ°Π²Π΅Π»',
// last_name: 'ΠΡΡΠΎΠ²'
// }]
// }
})
Sends message to user
Definition:
async reply (
user_id: number,
message: string, // required, if attachment not setten
attachment: string // required, if message not setten
): Promise<any> // Promise with response/error
Usage:
bot.on((ctx) => {
// with shortcut from context
ctx.reply('Hi, this is start!')
// function from context
ctx.sendMessage(ctx.user_id, 'Hi, this is start!')
// simple usage
bot.reply(ctx.user_id, 'Hi, this is start!')
// to multiple users
bot.reply([ ctx.user_id, 1 ], 'Hi, this is start!')
})
Start listen Express server.
Definition:
listen (
req: any, // Express request, required
res: any // Express response, required
)
Usage:
bot.listen(req, res)
Botact API: Actions β
Add event handler .
Definition:
event (
event: string | string[],
callback: function
): Botact;
Usage:
bot.event('group_join', ({ reply }) => reply('Thanks!'))
Add reserved callback.
Definition:
on (
type: string,
callback: function
): Botact;
OR
on (
callback: function
): Botact;
Usage:
bot.on(({ reply }) => reply('What?'))
bot.on('audio', ({ reply }) => reply('Great music!'))
Add middleware.
Definition:
use (
callback: function
): Botact
Usage:
bot.use(ctx => ctx.date = new Date())
bot.on(({ date }) => {
// Fri Nov 24 2017 16:00:21 GMT+0300 (MSK)
})
Botact API: Upload helpers β
Upload and save cover. See detailed settings here.
Definition:
async uploadCover (
filepath: string, // Path to file with cover
settings?: object
): Promise<any> // Promise with response/error
Usage:
await bot.uploadCover('./cover.jpg', { crop_x2: 1590 })
// {
// images: [
// {
// url: "URL",
// width: 1920,
// height: 1080
// },
// [Object],
// [Object],
// [Object],
// [Object]
// ]
// }
Uploads document to peer.
Definition:
async uploadDocument (
filepath: string, // Path to file
peer_id: number,
type: 'doc' | 'audio_message' // 'doc' by default
): Promise<any>; // Promise with response/error
Usage:
await bot.uploadDocument('./book.pdf', 1234)
// {
// response:
// [{
// id: 1234,
// owner_id: 1234,
// title: "",
// ...
// }]
// }
Uploads photo to peer.
Definition:
async uploadPhoto (
filepath: string, // Path to picture
peer_id: number
): Promise<any> // Promise with response/error
Usage:
await bot.uploadPhoto('./picture.png', 1234)
// {
// id: 1234,
// album_id: 1234,
// owner_id: 1234,
// ...
// }
Botact includes TypeScript definitions.
$ npm test
MIT.