Skip to content

API References

Vu Bao Nhu edited this page Feb 18, 2021 · 3 revisions

BotScript API References

Creating an instance

You can create a new instance of BotScript with a simple setup.

const bot = new BotScript();
bot.parse('+ hello bot\n - Hello human!');
bot.handleAsync('Hello bot').then((reply) => {
  console.log(reply.speechResponse); // Hello human!
});

You may await initialization to ensure that bot is ready!

const bot = new BotScript();
bot.parse('/include: https://raw.githubusercontent.com/yeuai/botscript/master/examples/hello.bot');
// await bot.init() to ensure the one ready.
bot.init().then(async () => {
  const reply = await bot.handleAsync('hello bot');
  console.log(reply.speechResponse); // Hello, human!
});

bot.parse(script: string)

Parse and import script documents according to BotScript Document Specification.

  • More details checkout github hompage: README.md

Example:

const bot = new BotScript();
bot.parse(`
    // declaration: plugin usage
    > addTimeNow

    // declaration: command
    @ geoip https://api.ipify.org/?format=json

    # declaration: a story with conditional command
    + what is my ip
    * true @> geoip
    - Here is your ip: $ip

    # declaration: an other story
    + what time is it
    - it is $time
`);

bot.parseUrl(script: string)

Parse and import script documents from URL which returns BotScript Document Specification.

Example:

const bot = new BotScript();
await bot.parseUrl('https://raw.githubusercontent.com/yeuai/botscript/master/examples/hello.bot');

bot.init()

Ensure all directives and internal process are initialized.

Example:

const bot = new BotScript();
await bot.init();  // Wait for bot ready!

bot.handleAsync(req: Request)

Handle human request message, returns a reply or emit events corresponding matched.

Example:

const reply = await bot.handleAsync('hello');
console.log(reply.speechResponse); // Hello, human!

BotScript Event References

bot.on('ready')

Notify the bot is ready!

Example:

bot.on('ready', () => {
  bot.logger.info('Bot is ready!')
})

bot.on('reply', (reply: Request, ctx: Context) => void)

Notify the bot is ready!

bot.on('parse', (content: string) => void)

Notify the bot is preparing to parse content of scripts.

bot.on('command', (err: Error, req: Request, ctx: Context, command.name: string, result: any) => void)

A command executed

bot.on('*', (event: string, ...args: any) => void)

Captures all events from bot handling.