Skip to content

The arguments plugin for Opengram, lets you parse & validate arguments of commands / inline query with ease.

License

Notifications You must be signed in to change notification settings

OpengramJS/arguments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e0ed1a7 · Mar 4, 2023

History

62 Commits
Sep 29, 2022
Oct 2, 2022
Mar 3, 2023
Dec 17, 2022
Dec 17, 2022
Sep 29, 2022
Sep 29, 2022
Sep 29, 2022
Sep 29, 2022
Sep 29, 2022
Sep 29, 2022
Mar 3, 2023
Aug 21, 2022
Mar 4, 2023
Mar 3, 2023
Feb 14, 2023
Feb 14, 2023

Repository files navigation

logo

Opengram arguments

The arguments plugin lets you parse & validate arguments of commands / inline query with ease.

CI codecov npm downloads javascript style guide Codacy Badge License: MIT FOSSA Status

Features

  • Joi validation support
  • Arguments remapping
  • Inline query / Message / Channel posts support

Docs

You can find documentation here and examples here and try now here

Installation

NPM

npm i @opengram/arguments

Yarn

yarn add @opengram/arguments

PNPM

pnpm add @opengram/arguments

Quick start

const { Opengram } = require('opengram')
const bot = new Opengram(process.env.BOT_TOKEN)

const Joi = require('joi')
const arguments = require('@opengram/arguments')
  
// Create middleware instance
const sumArgs = arguments({
  mapping: ['first', 'second'], // First argument to "first" property, second to "second" property
  errorHandler: (err, ctx) => ctx.reply(`Invalid arguments: ${err.message}`), // Error handler for validation errors
  // Validation schema
  schema: Joi.object({
    first: Joi
      .number()
      .integer()
      .required(),
    second: Joi
      .number()
      .integer()
      .required()
  })
})

bot.command('sum', sumArgs, ctx => {
  // Destructuring assignment from safe, validated object, with converted to number args
  const { first, second } = ctx.state.args.result
  // Send sum result
  return ctx.replyWithHTML(`<b>Result:</b> ${first + second}`)
})

bot.launch()

Flowchart

Loading
flowchart TB
  U("Incoming update") --> M("Upstream middlewares")
  M --> F("Filter by given update types")
  F --> E("Has command entity?")
  E -- No --> N("Nothing to do, run next")
  E -- Yes --> Parsing --> HV("Need mapping?")
  HV -- No --> K
  HV -- Yes --> Mapping --> NV("Need validation?")
  NV -- No --> K
  NV -- Yes --> VC("Validate / cast") --> ISV("Valid?")
  ISV -- Yes --> K
  ISV -- No --> ER("Error handler")
  K("Add result to <code>ctx.state</code> and run next") --> DM("Downstream middlewares")

License

FOSSA Status

About

The arguments plugin for Opengram, lets you parse & validate arguments of commands / inline query with ease.

Resources

License

Stars

Watchers

Forks

Packages

No packages published