Skip to content

Commit

Permalink
Merge pull request #2 from NEXBotio/mainrebase
Browse files Browse the repository at this point in the history
Mainrebase
  • Loading branch information
NEXBotio committed Dec 10, 2023
2 parents cd1f1b6 + 34edc8a commit ff7f272
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 113 deletions.
149 changes: 77 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
- [MessageStream](#messagestream)
- [Installation](#installation)
- [Example Usage](#example-usage)
- [Sending a Message](#sending-a-message)
- [Nexbot Authentication Endpoint](#nexbot-authentication-endpoint)
- [JavaScript](#javascript)
- [TypeScript](#typescript)
- [Invoking useChatStream](#invoking-usechatstream)
- [JavaScript](#javascript-1)
- [TypeScript](#typescript-1)
- [Nexbot Authentication Endpoint](#nexbot-authentication-endpoint)
- [Sending a Message](#sending-a-message)
- [JavaScript](#javascript-2)
- [TypeScript](#typescript-2)
- [Dependency Note](#dependency-note)
Expand Down Expand Up @@ -81,6 +81,80 @@ import { useChatStream } from "react-nexbot";

### Example Usage

### Nexbot Authentication Endpoint

For Nexbot authentication, your endpoint should call the Nexbot API as follows:

```bash
curl -X GET \
-H "Authorization: Bearer YOUR_SERVER_SECRET" \
"https://apis.nexbot.io/web/v1/secrets/generate_single_use_token"
```


##### JavaScript
```javascript
fetch("https://apis.nexbot.io/web/v1/secrets/generate_single_use_token", {
method: "GET",
headers: {
"Authorization": "Bearer YOUR_SERVER_SECRET"
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

```

##### TypeScript
```typescript
const getSingleUseToken = async (): Promise<any> => {
try {
const response = await fetch("https://apis.nexbot.io/web/v1/secrets/generate_single_use_token", {
method: "GET",
headers: {
"Authorization": "Bearer YOUR_SERVER_SECRET"
}
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
return await response.json();
} catch (error) {
console.error('Error:', error);
}
};
```

The response will contain a JSON payload with the key `access_token`. Your callback must provide only this string.

The callback is initially used by the hook to cache a single-use token, which opens a websocket connection with our servers and is immediately invalidated. The hook refreshes the token every 25 minutes using the callback. While the callback does not have to be an API call, using one allows Nexbot to handle token refresh for you.


#### Invoking useChatStream

##### JavaScript
```javascript
const { sendMessage } = useChatStream(() => {
return axios.get("https://your.api/nexbot/authing/endpoint")
.then((res) => {
return res.data.token;
});
}, botId);
```

##### TypeScript
```typescript
const botId: string = 'your-bot-id'; // Replace with your actual bot ID

const { sendMessage } = useChatStream(() => {
return axios.get("https://your.api/nexbot/authing/endpoint")
.then((res) => {
return res.data.token as string;
});
}, botId);
```

#### Sending a Message

##### JavaScript
Expand All @@ -104,6 +178,7 @@ useEffect(() => {
}
}, [willStream]);
```

##### TypeScript
```typescript
const [willStream, setWillStream] = useState<boolean>(false);
Expand All @@ -130,78 +205,8 @@ useEffect(() => {
};
```

#### Invoking useChatStream

##### JavaScript
```javascript
const { sendMessage } = useChatStream(() => {
return axios.get("https://your.api/nexbot/authing/endpoint")
.then((res) => {
return res.data.token;
});
}, botId);
```

##### TypeScript
```typescript
const botId: string = 'your-bot-id'; // Replace with your actual bot ID

const { sendMessage } = useChatStream(() => {
return axios.get("https://your.api/nexbot/authing/endpoint")
.then((res) => {
return res.data.token as string;
});
}, botId);
```

### Nexbot Authentication Endpoint

For Nexbot authentication, your endpoint should call the Nexbot API as follows:

```bash
curl -X GET \
-H "Authorization: Bearer YOUR_SERVER_SECRET" \
"https://apis.nexbot.io/web/v1/secrets/generate_single_use_token"
```


##### JavaScript
```javascript
fetch("https://apis.nexbot.io/web/v1/secrets/generate_single_use_token", {
method: "GET",
headers: {
"Authorization": "Bearer YOUR_SERVER_SECRET"
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

```

##### TypeScript
```typescript
const getSingleUseToken = async (): Promise<any> => {
try {
const response = await fetch("https://apis.nexbot.io/web/v1/secrets/generate_single_use_token", {
method: "GET",
headers: {
"Authorization": "Bearer YOUR_SERVER_SECRET"
}
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
return await response.json();
} catch (error) {
console.error('Error:', error);
}
};
```

The response will contain a JSON payload with the key `access_token`. Your callback must provide only this string.

The callback is initially used by the hook to cache a single-use token, which opens a websocket connection with our servers and is immediately invalidated. The hook refreshes the token every 25 minutes using the callback. While the callback does not have to be an API call, using one allows Nexbot to handle token refresh for you.

### Dependency Note

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-nexbot",
"version": "1.0.0",
"version": "1.0.1",
"description": "Hooks and utilities to chat with NEXBots from your project",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,6 @@
"react": "^18.2.0"
},
"dependencies": {
"axios": "^1.6.2",
"node-fetch": "^3.3.2",
"react-dom": "^18.2.0",
"react-query": "^3.39.3",
Expand Down
2 changes: 1 addition & 1 deletion react-nexbot-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"axios": "^1.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-nexbot": "file:../react-nexbot-1.0.0.tgz",
"react-nexbot": "file:../react-nexbot-1.0.1.tgz",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion react-nexbot-test/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import axios from "axios";
function App() {
const { sendMessage } = useChatStream(() => {
return axios
.get("https://localhost:8000/secrets/generate_single_use_token", {
.get("https://apis.nexbot.io/web/v1/secrets/generate_single_use_token", {
headers: {
Authorization: "Bearer " + process.env.REACT_APP_SERVER_SECRET,
},
Expand Down
40 changes: 3 additions & 37 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@

// For Firebase JS SDK v7.20.0 and later, measurementId is optional

let BASE__AND_PATH:string;
let WEBSOCKET_CHAT_PATH:string;
let WEBSOCKET_USER_INFO_PATH:string;

// if (process.env.NODE_ENV === 'production') {

BASE__AND_PATH = "https://apis.nexbot.io/web/v1".replace(/\/+$/, "");
WEBSOCKET_CHAT_PATH = "wss://websockets.nexbot.io"
WEBSOCKET_USER_INFO_PATH=WEBSOCKET_CHAT_PATH
// }
// else {
// BASE__AND_PATH = "https://localhost:8000".replace(/\/+$/, "");
// WEBSOCKET_CHAT_PATH = "wss://localhost:8050"
// WEBSOCKET_USER_INFO_PATH = "wss://localhost:8051"
// }
export { BASE__AND_PATH, WEBSOCKET_CHAT_PATH, WEBSOCKET_USER_INFO_PATH };



// // For Firebase JS SDK v7.20.0 and later, measurementId is optional
// let BASE_PATH:string;
// let WEBSOCKET_CHAT_PATH:string;
// let WEBSOCKET_USER_INFO_PATH:string;

// const userBranch = process.env.USER_BRANCH;

// if (userBranch === 'master') {
// BASE_PATH = "https://apis.nexbot.io/web/v1".replace(/\/+$/, "");
// WEBSOCKET_CHAT_PATH = "wss://websockets.nexbot.io"
// WEBSOCKET_USER_INFO_PATH=WEBSOCKET_CHAT_PATH
// }
// else if (userBranch === 'dev') {
// BASE_PATH = "https://apis-dev.nexbot.io/web/v1".replace(/\/+$/, "");
// WEBSOCKET_CHAT_PATH = "wss://websockets-dev.nexbot.io"
// WEBSOCKET_USER_INFO_PATH=WEBSOCKET_CHAT_PATH
// } else {

// BASE_PATH = "https://localhost:8000".replace(/\/+$/, "");
// WEBSOCKET_CHAT_PATH = "wss://localhost:8050"
// WEBSOCKET_USER_INFO_PATH = "wss://localhost:8051"
// }
// export { BASE_PATH, WEBSOCKET_CHAT_PATH, WEBSOCKET_USER_INFO_PATH };



export { BASE__AND_PATH, WEBSOCKET_CHAT_PATH, WEBSOCKET_USER_INFO_PATH };

0 comments on commit ff7f272

Please sign in to comment.