export default { async fetch(request) { function rawHtmlResponse(html) { return new Response(html, { headers: { "content-type": "text/html;charset=UTF-8", }, }); } if (request.method === "POST") { // replace with your API key const apiKey = 'HERE_IS_YOUR_API_KEY'; // replace subdomain with yours!!! const apiHost = "https://brightrockgames.userecho.com/api/v2"; // replace with your Helpdesk ID const apiHelpdeskId = 4; // same init data for user ant ticket requests. Just replace "data" var apiInitData = { headers: { "content-type": "application/x-www-form-urlencoded; charset=UTF-8", }, method: 'POST', }; // get parameters posted to the script const reqBodyJson = await request.json(); // build user data. Get email and name. Mark email as unverified if we are not sure it's actual user's email. // it will create a new user instead of merge to the existing to prevent security issue const userData = { "email": reqBodyJson.email, "name": reqBodyJson.name || reqBodyJson.email.split('@')[0], "language": "en", "email_verified": false } const userURL = apiHost + "/users/get_or_create.json?access_token=" + apiKey; apiInitData["body"] = JSON.stringify(userData); const userResponse = await fetch(userURL, apiInitData); const userResults = await userResponse.json(); // Now, when we have user data - time to post new ticket if (userResults.status == "success"){ const ticketURL = apiHost + "/forums/" + apiHelpdeskId + "/topics.json?access_token=" + apiKey + '&user_id=' + userResults.data.id; apiInitData["body"] = JSON.stringify(reqBodyJson); const ticketResponse = await fetch(ticketURL, apiInitData); const ticketResults = await ticketResponse.json(); return new Response(JSON.stringify(ticketResults)); } else { return new Response('Error while create a user'); } } }, };