The JavaScript Runtime
Build bots in JavaScript
Lil’bots provides a built in JavaScript runtime for bots based on Deno. If you have ever worked with Node.js, Deno should feel very familiar. This document will cover the basics of building bots in JavaScript.
The script.mjs file & the main function
The entrypoint to your bot is the script.mjs
file, as specified under the “main” property in the botspec.json
file. When your bot is executed it will look for an exported main
function in that file and call it.
The main
function can be asyncrounous (to let you use await inside) and recieves the three following arguments:
inputs
(object) - the inputs passed to your bot by the user.params
(object) - the parameters configured by the the user for your bot.accounts
(array) - an array containing all the requested account credentials for your bot to use.
A basic main
function will look like this:
Installing dependencies and packages
You may use external packages when building your bots - all you need to do is simply to import these packages, and the lil’bots runtime will automatically install them for you. There are a few sources of packages you may install:
- deno.land (recommended) - fetching modules from denoland is the fastest way to get modules into your bot. Many NPM modules have a version hosted on denoland - so you may want to check there first whenever you need a module. Imports from denoland look like:
- esm.sh - many NPM modules are also stored on the esm.sh CDN for fast downloads. You may install any ESM module like so:
- NPM Modules - you can also use any NPM module in your code. Just import it with an NPM prefix:
You can also specify a specific version by using the @version tag:
Modules and Bot Speeds
Whenever you run your bot, lil’bots has to download all of it’s dependencies - that’s what happens when you see the “starting” startus on your bot run. For that reason, it is recommended to use few modules, and to install them from denoland / esm.sh to ensure fast start times.
Module Caching
The lil’bots runtime caches modules for a certain period of time (for that reason, running the bot for a second time is sometime faster, as the modules are already cached). That means that many popular modules in their latest version are likely cached already - so it is recommended to use them over less common modules / specific versions.
Required Binaries
Some NPM modules require certain binaries to be installed on the host OS. For instance, many media processing modules require FFMPEG to be installed. The lil’bots runtime has a set of installed binaries which you can find here.