JavaScript Client#
Prerequisites
If you have not installed a daemon, please follow the π Quickstart.
Before you start, make sure you have a running instance of the Olvid daemon with an identity created on it, and a valid client key to connect using with this identity.
If you wish to verify that your daemon is functional and/or retrieve a lost client key, use the following command: docker compose run --rm cli key get
We have developed a Node.js module in TypeScript to create simple programs interacting with an Olvid daemon.
Here is the procedure for setting up a demonstration program that connects to your daemon instance and implements a basic chatbot.
Installation#
Letβs first create our working directory.
mkdir -p olvid-bot
cd olvid-bot
We can now set up the project.
npm init -y
npm install @olvid/bot-node
mkdir -p src
touch .env src/main.ts
npm pkg set scripts.main="npx tsx src/main.ts"
Configuration#
To connect to a daemon, your program needs to know the daemonβs address and the client key to use. For this, we use environment variables or a .env file.
Replace the client key with the one you created when setting up your daemon, and the daemon address if necessary.
echo OLVID_DAEMON_URL=http://localhost:50051 > .env
echo OLVID_CLIENT_KEY=ReplaceWithYourClientKey >> .env
First program#
You can now copy and paste the following code into the file src/main.ts.
The program will display the identity with which you are connected to the daemon and then wait for messages to arrive. At the moment, it only responds to the !ping command, but you can modify the code to change its behavior.
import {OlvidClient, datatypes} from "@olvid/bot-node";
import {command, onMessageReceived, onMessageSent, helpers} from "@olvid/bot-node";
class ExampleBot extends OlvidClient {
@command("^!ping")
async help(message: datatypes.Message) {
await helpers.message.reply(this, message, "pong")
}
@onMessageReceived()
async messageReceived(message: datatypes.Message) {
console.log("<", message.body);
}
@onMessageSent()
async messageSent(message: datatypes.Message) {
console.log(">", message.body);
}
}
async function main() {
let bot = new ExampleBot();
console.log("Started bot as:", (await bot.identityGet()).displayName)
await bot.runForever();
}
main().then()
Run your program using the following command. (Use CTRL+C to interrupt it).
npm run main
When everything works, you can use this program as a basis for your project by modifying it.