diff --git a/nodejs/basic-example/talkjs-backend/file.json b/nodejs/basic-example/talkjs-backend/file.json deleted file mode 100644 index ff18c49c..00000000 --- a/nodejs/basic-example/talkjs-backend/file.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "users": [ - { - "id": 2, - "name": "John Morrison", - "email": "john.morrison@operator.com", - "photoUrl": "https://randomuser.me/api/portraits/men/62.jpg", - "role": "USER" - }, - { - "id": 1, - "name": "Tom Hardy", - "email": "tom.hardy@operator.com", - "photoUrl": "https://randomuser.me/api/portraits/men/1.jpg", - "role": "USER" - } - ] -} \ No newline at end of file diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index 9ae59a5b..e5ec5a78 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -5,7 +5,7 @@ import bodyParser from "body-parser"; import { LowSync, JSONFileSync } from "lowdb"; -const adapter = new JSONFileSync("file.json"); +const adapter = new JSONFileSync("users.json"); const db = new LowSync(adapter); db.read(); db.data ||= { users: [] }; @@ -23,7 +23,6 @@ app.post("/createUser", (req, res) => { const email = req.body.email; const photoUrl = req.body.photoUrl; const role = req.body.role; - console.log(id, name, email, photoUrl, role); db.data.users.push({ id: id, name: name, @@ -47,4 +46,4 @@ app.get("/getUser/:id", (req, res) => { } }); -app.listen(port, () => console.log(`Server up and running at port ${port}!`)); +app.listen(port, () => console.log(`Server up and running on port ${port}`)); diff --git a/nodejs/basic-example/talkjs-backend/users.json b/nodejs/basic-example/talkjs-backend/users.json new file mode 100644 index 00000000..44d3ecc8 --- /dev/null +++ b/nodejs/basic-example/talkjs-backend/users.json @@ -0,0 +1,20 @@ +{ + "users": [ + { + "id": "1", + "name": "Alice", + "email": "alice@example.com", + "photoUrl": "https://talkjs.com/new-web/avatar-7.jpg", + "role": "default", + "welcomeMessage": "Hi 👋" + }, + { + "id": "2", + "name": "Sebastian", + "email": "sebastian@example.com", + "photoUrl": "https://talkjs.com/new-web/avatar-2.jpg", + "role": "default", + "welcomeMessage": "Hello there!" + } + ] +} diff --git a/nodejs/basic-example/talkjs-frontend/index.html b/nodejs/basic-example/talkjs-frontend/index.html index 382a5076..8b03ea81 100644 --- a/nodejs/basic-example/talkjs-frontend/index.html +++ b/nodejs/basic-example/talkjs-frontend/index.html @@ -1,48 +1,43 @@ - - - - + + + TalkJS Node JS Integration - - - - - - + + + + -
- Loading chat... + Loading chat...
- - - \ No newline at end of file + + diff --git a/nodejs/basic-example/talkjs-frontend/script.js b/nodejs/basic-example/talkjs-frontend/script.js index 1daa090e..e757e97b 100644 --- a/nodejs/basic-example/talkjs-frontend/script.js +++ b/nodejs/basic-example/talkjs-frontend/script.js @@ -1,22 +1,10 @@ -const getAgent = async () => { - const response = await fetch("http://127.0.0.1:3000/getUser/1"); - const data = await response.json(); - let agent = new Talk.User({ - id: data.id, - name: data.name, - photoUrl: data.dp, - email: data.email, - role: data.role, - }); - return agent; -}; -const getUser = async () => { - const response = await fetch("http://127.0.0.1:3000/getUser/2"); +const getUser = async (id) => { + const response = await fetch(`http://127.0.0.1:3000/getUser/${id}`); const data = await response.json(); let user = new Talk.User({ id: data.id, name: data.name, - photoUrl: data.dp, + photoUrl: data.photoUrl, email: data.email, role: data.role, }); @@ -25,26 +13,24 @@ const getUser = async () => { (async function () { await Talk.ready; - let agent = await getAgent(); - let user = await getUser(); + const alice = await getUser(1); + const sebastian = await getUser(2); const session = new Talk.Session({ - appId: "", - me: user, + appId: "", // replace with your app ID + me: sebastian, }); - var conversation = session.getOrCreateConversation( - Talk.oneOnOneId(user, agent) + const conversation = session.getOrCreateConversation( + "nodeJSExampleConversation" ); conversation.setAttributes({ welcomeMessages: [ - "You can start typing your message here and one of our agents will be with you shortly.", - "Please do not divulge any of your personal information.", + "Example chat for our Node.js tutorial. Try sending a message!", ], }); - conversation.setParticipant(user); - conversation.setParticipant(agent); - console.log(conversation); + conversation.setParticipant(alice); + conversation.setParticipant(sebastian); - var inbox = session.createInbox(conversation); - inbox.select(conversation); - inbox.mount(document.getElementById("talkjs-container")); + const chatbox = session.createChatbox(conversation); + chatbox.select(conversation); + chatbox.mount(document.getElementById("talkjs-container")); })();