From b15dadb9d6d11d3bbeb8ea2e3262c04084ffe108 Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Fri, 20 Dec 2024 15:23:10 +0000 Subject: [PATCH 1/2] Update Node.js tutorial --- nodejs/basic-example/talkjs-backend/file.json | 18 ----- nodejs/basic-example/talkjs-backend/server.js | 5 +- .../basic-example/talkjs-backend/users.json | 20 ++++++ .../basic-example/talkjs-frontend/index.html | 71 +++++++++---------- .../basic-example/talkjs-frontend/script.js | 36 +++------- 5 files changed, 66 insertions(+), 84 deletions(-) delete mode 100644 nodejs/basic-example/talkjs-backend/file.json create mode 100644 nodejs/basic-example/talkjs-backend/users.json 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..cdbe7511 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(); + let agent = await getUser(1); + let user = await getUser(2); const session = new Talk.Session({ - appId: "", + appId: "", // replace with your app ID me: user, }); var conversation = session.getOrCreateConversation( - Talk.oneOnOneId(user, agent) + "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); - var inbox = session.createInbox(conversation); - inbox.select(conversation); - inbox.mount(document.getElementById("talkjs-container")); + var chatbox = session.createChatbox(conversation); + chatbox.select(conversation); + chatbox.mount(document.getElementById("talkjs-container")); })(); From 044006a9582eeb253ac9b788455a6dcc28613e0c Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Fri, 20 Dec 2024 15:31:32 +0000 Subject: [PATCH 2/2] Few more fixes --- nodejs/basic-example/talkjs-frontend/script.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/nodejs/basic-example/talkjs-frontend/script.js b/nodejs/basic-example/talkjs-frontend/script.js index cdbe7511..e757e97b 100644 --- a/nodejs/basic-example/talkjs-frontend/script.js +++ b/nodejs/basic-example/talkjs-frontend/script.js @@ -13,13 +13,13 @@ const getUser = async (id) => { (async function () { await Talk.ready; - let agent = await getUser(1); - let user = await getUser(2); + const alice = await getUser(1); + const sebastian = await getUser(2); const session = new Talk.Session({ appId: "", // replace with your app ID - me: user, + me: sebastian, }); - var conversation = session.getOrCreateConversation( + const conversation = session.getOrCreateConversation( "nodeJSExampleConversation" ); conversation.setAttributes({ @@ -27,10 +27,10 @@ const getUser = async (id) => { "Example chat for our Node.js tutorial. Try sending a message!", ], }); - conversation.setParticipant(user); - conversation.setParticipant(agent); + conversation.setParticipant(alice); + conversation.setParticipant(sebastian); - var chatbox = session.createChatbox(conversation); + const chatbox = session.createChatbox(conversation); chatbox.select(conversation); chatbox.mount(document.getElementById("talkjs-container")); })();