Node.js SDK

A Node.js Bot SDK for SendingNetwork.

1. Add Dependency

Start by installing the SDK as a dependency in your Node.js project:

npm i sendingnetwork-bot-sdk

2. Prepare a Configuration File

Create a file named bot.creds.json and provide the following information:

{
    "nodeUrl": "NODE_URL",
    "walletAddress": "YOUR_WALLET_ADDRESS",
    "privateKey": "YOUR_PRIVATE_KEY",
    "developerKey": "YOUR_DEV_KEY"
}

You can configure the NODE_URL with https://portal0101.sending.network to connect to a public Edge Node.

Make sure to replace YOUR_WALLET_ADDRESS , YOUR_PRIVATE_KEY and YOUR_DEV_KEY with the actual value.

3. Create an Instance of SDNClient

After setting up the configuration file, you can create an instance of the SDNClient class. This will allow you to interact with the SendingNetwork API.

You will need a developer key to access the Edge Network. For further insight into the mechanism and to request a developer key, please consult the provided guide here.

// preLogin to get a message to sign
let auth = new SDNAuth(nodeUrl)
let loginMessage = await auth.didPreLogin(address)

let web3 = new Web3()
// sign with wallet account key
let walletSignature = web3.eth.accounts.sign(loginMessage["message"], key)
// sign with developer key (add '0x' prefix if necessary)
let developerKeySignature = web3.eth.accounts.sign(loginMessage["message"], developerKey)

// didLogin to get access token
let loginResp = await auth.didLoginWithAppToken(address,
    loginMessage["did"], loginMessage["message"], walletSignature["signature"], developerKeySignature["signature"],
    loginMessage["random_server"], loginMessage["updated"])
let accessToken = loginResp["access_token"]

// create SDNClient with a storage file
let storage = new SimpleFsStorageProvider("/path/to/bot/storage/file");
let client = new SDNClient(nodeUrl, accessToken, storage);

// register message listener
client.on("room.message", async (roomId: string, event: any) => {
    LogService.info("index", roomId, event);
});

// start syncing
await client.start();

4. Interact with the SDK

Once you have created an instance of SDNClient and started syncing with the SendingNetwork server, you can use the SDK to perform various actions. Here are a few examples:

Create a new room

await client.createRoom({ name: 'My Room' });

Invite a user to a room

await client.inviteUser('user_id', 'room_id');

Send a message

await client.sendText('room_id', 'Hello, world!');

Examples

For more examples and detailed use cases, you can refer to the examples directory in the SDK repository.

Last updated