Skip to content

Instantly share code, notes, and snippets.

@learyjk
Last active October 31, 2024 01:13
Show Gist options
  • Save learyjk/b9c81000312c5ca4a1f49ec46e3cbe24 to your computer and use it in GitHub Desktop.
Save learyjk/b9c81000312c5ca4a1f49ec46e3cbe24 to your computer and use it in GitHub Desktop.
TalkJS USers
Talk.ready.then(function() {
const me = new Talk.User({
id: member.id,
name: member.customFields["free-user"],
email: member.auth.email,
photoUrl: member.profileImage,
welcomeMessage: null,
role: "brand",
});
console.log({
me
});
const session = new Talk.Session({
appId: 'tsvaeCVc',
me: me,
});
console.log({
session
});
const other = new Talk.User({
id: '{{wf {"path":"memberstack-id","type":"PlainText"\} }}',
name: '{{wf {"path":"name","type":"PlainText"\} }}',
email: '{{wf {"path":"email-address","type":"Email"\} }}',
photoUrl: '{{wf {"path":"profile-photo","type":"ImageRef"\} }}',
welcomeMessage: null,
role: "freelancer",
custom: {
slug: '{{wf {"path":"slug","type":"PlainText"\} }}'
}
});
const conversationId = Talk.oneOnOneId(member.id, '{{wf {"path":"memberstack-id","type":"PlainText"\} }}')
const conversation = session.getOrCreateConversation(conversationId);
conversation.setParticipant(me);
conversation.setParticipant(other);
const chatbox = session.createChatbox();
chatbox.select(conversation);
chatbox.mount(document.getElementById('talkjs-container'));
// Automation to handle whether or not a brand has sent their first message.
chatbox.onSendMessage((sendMessageEvent) => {
if (me?.custom?.hasSentFirstMessage) return;
const url = "https://hook.us1.make.com/o7odp0djqy1167a2pi0m56tqn9mvk3en";
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(sendMessageEvent),
})
.then((response) => response.text())
.then((result) => {
console.log("Success:", result);
// Update the custom field after the first message is sent
session.currentUser.set({
custom: {
hasSentFirstMessage: 'true'
}
})
.then(() => {
console.log("User updated successfully");
})
.catch((error) => {
console.error("Error updating user:", error);
});
})
.catch((error) => console.error("Error:", error));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment