# NodeJS SDK
A NodeJS Bot SDK for SendingNetwork.
## Quick Start
### Add dependency
```shell
npm i sendingnetwork-bot-sdk
```
### Prepare a configuration file
Provide server node url, wallet address and private key in file bot.creds.json:
```json
{
"nodeUrl": "https://example.com",
"walletAddress": "",
"privateKey": ""
}
```
### Create an instance of `SDNClient`
After reading the configuration file, create an instance of `SDNClient`. Register event listener and start syncing.
```typescript
// preLogin to get message to sign
let auth = new SDNAuth(nodeUrl)
let loginMessage = await auth.didPreLogin(address)
// sign message
let web3 = new Web3()
web3.eth.accounts.wallet.add(key);
let signature = await web3.eth.sign(loginMessage["message"], address)
// didLogin to get access token
let loginResp = await auth.didLogin(
loginMessage["did"], loginMessage["message"], signature["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();
```
### Call API functions
```typescript
// create new room
await client.createRoom({name: roomName})
// invite user to the room
await client.inviteUser(inviteUserId, inviteRoomId)
// send room message
await client.sendText(sendRoomId, "hello world")
```
## Examples
See more use cases in `examples` directory.
## Client API Doc
### Login DID
```typescript
public async didLogin(did: string, message: string, token: string, nonce: string, update_time: string): Promise<any>
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| did | string | user did | true |
| message | string | signing message | true |
| token | string | signature | true |
| nonce | string | random nonce | true |
| update_time | string | update time of did document | true |
output params:
| Name | Type | Description | Required |
| :------- | :---------- | :---------- | :------- |
| user_id | string | user did | true |
| access_token | string | access token | true |
| device_id | string | device id | true |
### Create Room
```typescript
public createRoom(properties: RoomCreateOptions = {}): Promise<string>
```
input params:RoomCreateOptions
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| name | string | room name | true |
| topic | string | room topic | false |
| invite | string array | array of user ids to invite | false |
output params:
| Name | Type | Description | Required |
| :------- | :---------- | :---------- | :------- |
| roomId | string | id of the new room | true |
### Invite User
```typescript
public inviteUser(userId, roomId)
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
| userId | string | user id | true |
output params: none
### Kick User
```typescript
public kickUser(userId, roomId, reason = null)
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
| userId | string | user id | true |
| reason | string | invite reason | false |
return values: none
### Join Room
```typescript
public joinRoom(roomId: string): Promise<string>
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
output params: none
### Leave Room
```typescript
public leaveRoom(roomId: string, reason?: string)
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
| reason | string | leave reason | false |
output params: none
### Get Room Memebers
```typescript
public getAllRoomMembers(roomId: string, atToken?: string): Promise<MembershipEvent[]>
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
| atToken | string | optional batch token, null for "now" | true |
output params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| MembershipEvent[] | array of MembershipEvent | membership event data | true |
### Send Message
```typescript
public sendText(roomId: string, text: string): Promise<string>
```
input params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id | true |
| text | string | message to send | true |
output params:
| Name | Type | Description | Required |
| :------ | :----- | :---------- | :------- |
| eventId | string | event id | true |