|
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import fetch from "node-fetch"; |
| 4 | + |
| 5 | +const appId = "<APP_ID>"; |
| 6 | +const secretKey = "<SECRET_KEY>"; |
| 7 | + |
| 8 | +const basePath = "https://api.talkjs.com"; |
| 9 | + |
| 10 | +const app = express(); |
| 11 | +app.use(cors()); |
| 12 | +app.use(express.json()); |
| 13 | + |
| 14 | +app.listen(3000, () => console.log("Server is up")); |
| 15 | + |
| 16 | +const senderId = `threadsExampleSender`; |
| 17 | +const receiverId = `threadsExampleReceiver`; |
| 18 | + |
| 19 | +function getMessages(messageId) { |
| 20 | + return fetch( |
| 21 | + `${basePath}/v1/${appId}/conversations/replyto_${messageId}/messages`, |
| 22 | + { |
| 23 | + method: "GET", |
| 24 | + headers: { |
| 25 | + "Content-Type": "application/json", |
| 26 | + Authorization: `Bearer ${secretKey}`, |
| 27 | + }, |
| 28 | + } |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +// Create a thread as a new conversation |
| 33 | +async function createThread(parentMessageId, parentConvId, participants) { |
| 34 | + return fetch( |
| 35 | + `${basePath}/v1/${appId}/conversations/replyto_${parentMessageId}`, |
| 36 | + { |
| 37 | + method: "PUT", |
| 38 | + headers: { |
| 39 | + "Content-Type": "application/json", |
| 40 | + Authorization: `Bearer ${secretKey}`, |
| 41 | + }, |
| 42 | + body: JSON.stringify({ |
| 43 | + participants: participants, |
| 44 | + subject: "Replies", |
| 45 | + custom: { |
| 46 | + parentConvId: parentConvId, |
| 47 | + parentMessageId: parentMessageId, |
| 48 | + }, |
| 49 | + }), |
| 50 | + } |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +async function duplicateParentMessageText(parentMessageId, messageText) { |
| 55 | + return fetch( |
| 56 | + `${basePath}/v1/${appId}/conversations/replyto_${parentMessageId}/messages`, |
| 57 | + { |
| 58 | + method: "POST", |
| 59 | + headers: { |
| 60 | + "Content-Type": "application/json", |
| 61 | + Authorization: `Bearer ${secretKey}`, |
| 62 | + }, |
| 63 | + body: JSON.stringify([ |
| 64 | + { |
| 65 | + text: messageText, |
| 66 | + type: "SystemMessage", |
| 67 | + }, |
| 68 | + ]), |
| 69 | + } |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +async function updateReplyCount(messageId, conversationId, count) { |
| 74 | + return fetch( |
| 75 | + `${basePath}/v1/${appId}/conversations/${conversationId}/messages/${messageId}`, |
| 76 | + { |
| 77 | + method: "PUT", |
| 78 | + headers: { |
| 79 | + "Content-Type": "application/json", |
| 80 | + Authorization: `Bearer ${secretKey}`, |
| 81 | + }, |
| 82 | + body: JSON.stringify({ |
| 83 | + custom: { replyCount: count.toString() }, |
| 84 | + }), |
| 85 | + } |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +app.post("/newThread", async (req, res) => { |
| 90 | + // Get details of the message we'll reply to |
| 91 | + const parentMessageId = req.body.messageId; |
| 92 | + const parentConvId = req.body.conversationId; |
| 93 | + const parentMessageText = req.body.messageText; |
| 94 | + const parentParticipants = req.body.participants; |
| 95 | + |
| 96 | + const response = await getMessages(parentMessageId); |
| 97 | + const messages = await response.json(); |
| 98 | + |
| 99 | + // Create a message with the text of the parent message if one doesn't already exist |
| 100 | + if (!messages.data?.length) { |
| 101 | + await createThread(parentMessageId, parentConvId, parentParticipants); |
| 102 | + await duplicateParentMessageText(parentMessageId, parentMessageText); |
| 103 | + } |
| 104 | + |
| 105 | + res.status(200).end(); |
| 106 | +}); |
| 107 | + |
| 108 | +// Endpoint for message.sent webhook |
| 109 | +app.post("/updateReplyCount", async (req, res) => { |
| 110 | + const data = req.body.data; |
| 111 | + const conversationId = data.conversation.id; |
| 112 | + const messageType = data.message.type; |
| 113 | + |
| 114 | + if (conversationId.startsWith("replyto_") && messageType === "UserMessage") { |
| 115 | + const { parentMessageId, parentConvId } = data.conversation.custom; |
| 116 | + |
| 117 | + const response = await getMessages(parentMessageId); |
| 118 | + const messages = await response.json(); |
| 119 | + |
| 120 | + const messageCount = messages.data.length; |
| 121 | + |
| 122 | + // Ignore the first message in thread (it's a repeat of the parent message) |
| 123 | + if (messageCount > 1) { |
| 124 | + await updateReplyCount(parentMessageId, parentConvId, messageCount - 1); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + res.status(200).end(); |
| 129 | +}); |
| 130 | + |
| 131 | +// THIS IS SETUP CODE FOR THE EXAMPLE |
| 132 | +// You won't need any of it in your live app! |
| 133 | +// |
| 134 | +// It's just here so that you can play around with this example more easily |
| 135 | +// Whenever you run this script, we make sure the two example users are created |
| 136 | +// recreate the two conversations, and send messages from the example users |
| 137 | + |
| 138 | +async function setupConversation() { |
| 139 | + const conversationId = "threadsExample"; |
| 140 | + |
| 141 | + // Delete the conversation (if it exists) |
| 142 | + await fetch(`${basePath}/v1/${appId}/conversations/${conversationId}`, { |
| 143 | + method: "delete", |
| 144 | + headers: { |
| 145 | + Authorization: `Bearer ${secretKey}`, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + // Create a new conversation |
| 150 | + await fetch(`${basePath}/v1/${appId}/conversations/${conversationId}`, { |
| 151 | + method: "put", |
| 152 | + headers: { |
| 153 | + "Content-Type": "application/json", |
| 154 | + Authorization: `Bearer ${secretKey}`, |
| 155 | + }, |
| 156 | + body: JSON.stringify({ |
| 157 | + participants: [receiverId, senderId], |
| 158 | + }), |
| 159 | + }); |
| 160 | +} |
| 161 | + |
| 162 | +async function sendMessage(messageText) { |
| 163 | + const conversationId = "threadsExample"; |
| 164 | + |
| 165 | + // Send a message from the user to make sure it will show up in the conversation list |
| 166 | + await fetch( |
| 167 | + `${basePath}/v1/${appId}/conversations/${conversationId}/messages`, |
| 168 | + { |
| 169 | + method: "post", |
| 170 | + headers: { |
| 171 | + "Content-Type": "application/json", |
| 172 | + Authorization: `Bearer ${secretKey}`, |
| 173 | + }, |
| 174 | + body: JSON.stringify([ |
| 175 | + { |
| 176 | + text: messageText, |
| 177 | + sender: senderId, |
| 178 | + type: "UserMessage", |
| 179 | + }, |
| 180 | + ]), |
| 181 | + } |
| 182 | + ); |
| 183 | +} |
| 184 | + |
| 185 | +async function setup() { |
| 186 | + const receiver = fetch(`${basePath}/v1/${appId}/users/${receiverId}`, { |
| 187 | + method: "PUT", |
| 188 | + headers: { |
| 189 | + "Content-Type": "application/json", |
| 190 | + Authorization: `Bearer ${secretKey}`, |
| 191 | + }, |
| 192 | + body: JSON.stringify({ |
| 193 | + name: "Alice", |
| 194 | + |
| 195 | + role: "default", |
| 196 | + photoUrl: "https://talkjs.com/new-web/avatar-14.jpg", |
| 197 | + }), |
| 198 | + }); |
| 199 | + |
| 200 | + const sender = fetch(`${basePath}/v1/${appId}/users/${senderId}`, { |
| 201 | + method: "PUT", |
| 202 | + headers: { |
| 203 | + "Content-Type": "application/json", |
| 204 | + Authorization: `Bearer ${secretKey}`, |
| 205 | + }, |
| 206 | + body: JSON.stringify({ |
| 207 | + name: "Bob", |
| 208 | + |
| 209 | + role: "default", |
| 210 | + photoUrl: "https://talkjs.com/new-web/avatar-15.jpg", |
| 211 | + }), |
| 212 | + }); |
| 213 | + await receiver; |
| 214 | + await sender; |
| 215 | + |
| 216 | + const conv = setupConversation(); |
| 217 | + await conv; |
| 218 | + |
| 219 | + const message = sendMessage("We've added reply threads to the chat!"); |
| 220 | + await message; |
| 221 | +} |
| 222 | + |
| 223 | +setup(); |
0 commit comments