GithubHelp home page GithubHelp logo

antosik / botact-micro Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bifot/botact

0.0 2.0 0.0 1.49 MB

πŸ”¬ Micro version of πŸ€– https://github.com/bifot/botact

License: MIT License

JavaScript 100.00%

botact-micro's Introduction

botact botact

botact-micro.js

Botact enables developers to focus on writing reusable application logic instead of spending time building infrastructure.

! This is "micro" version, which doesn't include some functions, like Botact Flow, hears, command !

Table of content

Install

$ npm i https://github.com/Antosik/botact-micro

Usage

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)

Botact API

Methods

Core

Actions

Upload helpers


Botact API: Core ↑

constructor(settings)

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
})

.api(method, settings)

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
})

.execute(method, settings, callback)

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: 'Π”ΡƒΡ€ΠΎΠ²'
  //   }]
  // }
})

.reply(user_id, message, attachment)

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!')
})

.listen(req, res)

Start listen Express server.

Definition:

listen (
  req: any,     // Express request, required
  res: any      // Express response, required
)

Usage:

bot.listen(req, res)

Botact API: Actions ↑

.event(event, callback)

Add event handler .

Definition:

event (
  event: string | string[], 
  callback: function
): Botact;

Usage:

bot.event('group_join', ({ reply }) => reply('Thanks!'))

.on(type, callback)

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!'))

.use(callback)

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 ↑

.uploadCover(file, settings)

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]
//   ]
// }

.uploadDocument(file, peer_id, type)

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: "",
//       ... 
//     }]
// }

.uploadPhoto(file, peer_id)

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,
//   ...
// }

TypeScript

Botact includes TypeScript definitions.

Tests

$ npm test

License

MIT.

botact-micro's People

Contributors

alextolmachev avatar antosik avatar ibakaidov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.