The arguments plugin lets you parse & validate arguments of commands / inline query with ease.
- Joi validation support
- Arguments remapping
- Inline query / Message / Channel posts support
You can find documentation here and examples here and try now here
npm i @opengram/argumentsyarn add @opengram/argumentspnpm add @opengram/argumentsconst{ Opengram }=require('opengram')constbot=newOpengram(process.env.BOT_TOKEN)constJoi=require('joi')constarguments=require('@opengram/arguments')// Create middleware instanceconstsumArgs=arguments({mapping: ['first','second'],// First argument to "first" property, second to "second" propertyerrorHandler: (err,ctx)=>ctx.reply(`Invalid arguments: ${err.message}`),// Error handler for validation errors// Validation schemaschema: 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 argsconst{ first, second }=ctx.state.args.result// Send sum resultreturnctx.replyWithHTML(`<b>Result:</b> ${first+second}`)})bot.launch()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")