Skip to content

Commit 831f500

Browse files
committed
Simplify setting up example users
1 parent fe4520d commit 831f500

File tree

2 files changed

+21
-119
lines changed

2 files changed

+21
-119
lines changed

howtos/how-to-set-an-away-message/index.html

+21-6
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,34 @@
3737
<script>
3838
Talk.ready.then(function () {
3939
const me = new Talk.User({
40+
id: "autoReplyExampleUser",
41+
name: "Sebastian",
42+
43+
photoUrl: "https://talkjs.com/images/avatar-5.jpg",
44+
role: "customer",
45+
welcomeMessage: "Hi!",
46+
});
47+
const talkSession = new Talk.Session({
48+
appId: "<APP_ID>", // replace with your app ID
49+
me: me,
50+
});
51+
52+
var other = new Talk.User({
4053
id: "autoReplyExampleSupportAgent",
4154
name: "Alice",
4255
4356
photoUrl: "https://talkjs.com/images/avatar-1.jpg",
4457
role: "support",
45-
welcomeMessage: "Hey there! How can I help?",
46-
});
47-
const talkSession = new Talk.Session({
48-
appId: "<APP_ID>", // replace with your app ID
49-
me: me,
58+
welcomeMessage: "Hey, how can I help?",
5059
});
5160

52-
const inbox = talkSession.createInbox();
61+
var conversation = talkSession.getOrCreateConversation(
62+
"autoReplyExampleConversation"
63+
);
64+
conversation.setParticipant(me);
65+
conversation.setParticipant(other);
66+
67+
const inbox = talkSession.createInbox({ selected: conversation });
5368
inbox.mount(document.getElementById("talkjs-container"));
5469
});
5570
</script>

howtos/how-to-set-an-away-message/server.js

-113
Original file line numberDiff line numberDiff line change
@@ -72,116 +72,3 @@ app.post("/talkjs", async (req, res) => {
7272

7373
res.status(200).end();
7474
});
75-
76-
// EVERYTHING BELOW IS SETUP CODE FOR THIS EXAMPLE
77-
// You won't need any of it in your live app!
78-
//
79-
// It's just here so that you can play around with this example more easily
80-
// Whenever you start the server, we make sure the two example users are created
81-
// recreate the two conversations, and send messages from the example users
82-
83-
async function setupConversation(i) {
84-
const conversationId = `autoReplyExample${i}`;
85-
const userId = `autoReplyExampleUser${i}`;
86-
87-
// Delete the conversation (if it exists)
88-
await fetch(`${basePath}/v1/${appId}/conversations/${conversationId}`, {
89-
method: "delete",
90-
headers: {
91-
Authorization: `Bearer ${secretKey}`,
92-
},
93-
});
94-
95-
// Create a new conversation
96-
await fetch(`${basePath}/v1/${appId}/conversations/${conversationId}`, {
97-
method: "put",
98-
headers: {
99-
"Content-Type": "application/json",
100-
Authorization: `Bearer ${secretKey}`,
101-
},
102-
body: JSON.stringify({
103-
participants: ["autoReplyExampleSupportAgent", userId],
104-
}),
105-
});
106-
107-
// Send a message from the user to make sure it will show up in the conversation list
108-
await fetch(
109-
`${basePath}/v1/${appId}/conversations/${conversationId}/messages`,
110-
{
111-
method: "post",
112-
headers: {
113-
"Content-Type": "application/json",
114-
Authorization: `Bearer ${secretKey}`,
115-
},
116-
body: JSON.stringify([
117-
{
118-
text: "Everything is broken again!",
119-
sender: userId,
120-
type: "UserMessage",
121-
},
122-
]),
123-
}
124-
);
125-
}
126-
127-
async function setup() {
128-
const supportAgent = fetch(
129-
`${basePath}/v1/${appId}/users/autoReplyExampleSupportAgent`,
130-
{
131-
method: "put",
132-
headers: {
133-
"Content-Type": "application/json",
134-
Authorization: `Bearer ${secretKey}`,
135-
},
136-
body: JSON.stringify({
137-
name: "Alice",
138-
email: ["[email protected]"],
139-
photoUrl: "https://talkjs.com/images/avatar-1.jpg",
140-
role: "support",
141-
welcomeMessage: "Hey there! How can I help?",
142-
}),
143-
}
144-
);
145-
146-
const user1 = fetch(`${basePath}/v1/${appId}/users/autoReplyExampleUser1`, {
147-
method: "put",
148-
headers: {
149-
"Content-Type": "application/json",
150-
Authorization: `Bearer ${secretKey}`,
151-
},
152-
body: JSON.stringify({
153-
name: "Sebastian",
154-
email: ["[email protected]"],
155-
photoUrl: "https://talkjs.com/images/avatar-5.jpg",
156-
role: "customer",
157-
welcomeMessage: "Hi!",
158-
}),
159-
});
160-
161-
const user2 = fetch(`${basePath}/v1/${appId}/users/autoReplyExampleUser2`, {
162-
method: "put",
163-
headers: {
164-
"Content-Type": "application/json",
165-
Authorization: `Bearer ${secretKey}`,
166-
},
167-
body: JSON.stringify({
168-
name: "Bob",
169-
email: ["[email protected]"],
170-
photoUrl: "https://talkjs.com/images/avatar-4.jpg",
171-
role: "customer",
172-
welcomeMessage: "Hello!",
173-
}),
174-
});
175-
176-
await supportAgent;
177-
await user1;
178-
await user2;
179-
180-
const conv1 = setupConversation(1);
181-
const conv2 = setupConversation(2);
182-
183-
await conv1;
184-
await conv2;
185-
}
186-
187-
setup();

0 commit comments

Comments
 (0)