Generate and test Bot starter code
Root supplies a command-line utility named create-root that generates starter code. Here, you'll create your project, build it, and test that it works with your community.
1. Generate
-
Open a terminal in your preferred base folder.
-
Run the following command:
npx create-root@latest --bot <your-project-name> -
Open the generated folder in an IDE.
-
(Optional) If you'd like to, take a few minutes to review the generated project. Here are a few files that might be of interest:
package.json- The necessary dependencies for the Root SDK are already included.
- The
buildscript will compile your code. - The
cleanscript is provided as a convenience. It deletes the compiled code, thenode_modulesfolder, and a few generated config files. Feel free to edit theclean.jsfile to modify this script as needed. - The
botscript runs your Bot; when you execute it, you'll saynpm run botso it should be easy to remember.
src/example.ts- You import all Root Bot SDK types from
@rootsdk/server-bot. - The file exports an
initializemethod that's called at Bot startup fromsrc/main.ts. - There's starter code that implements an
echoBot:- Subscribes to the
ChannelMessageEvent.ChannelMessageCreatedevent. - Retrieves the incoming message.
- Sends it back to the same channel.
- Subscribes to the
2. Add DEV_TOKEN
-
Open the
.envfile in the Bot's project folder. -
Add your previously generated
DEV_TOKENto the file. The completed file should look something like this (but contain yourDEV_TOKENvalue rather than this sample):Complete content of a sample .env fileDEV_TOKEN=ACROZj4Ilajlkgjoilkjdljfb5l2jln426k42548fdlj4359204undlajf_lkaejrln3904280294FLWalPd35aoier82m_lkad4lkjas0kj-aeheccewqioclbaljer34akjwqpz_-lk-kO52jQjlaljbjlk5299elk2aklbwpcast67lka0as2lgxlmqJSO98absLa34s90gask2ac
3. Build
-
Open a terminal in your Bot's project folder.
-
Install the necessary packages by running the following command:
npm i -
Build the Bot by running the following command:
npm run build
4. Run
- Execute your Bot by running the following command:
npm run bot
5. Test
Use the Root native client to interact with your Bot.
- Open the Root native client
- Log in if needed.
- Open the community associated with your
DEV_TOKEN. - Enter your Bot's commands to interact with your Bot. The starter-code Bot responds to the
/echocommand and replies with the text you include after the command.