Skip to content

Commit

Permalink
demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Mar 19, 2024
1 parent 1a88f1b commit f96a434
Show file tree
Hide file tree
Showing 5 changed files with 1,633 additions and 15 deletions.
15 changes: 13 additions & 2 deletions test-browser-esm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
<script type="module" src="./dist/bundle.js"></script>
</head>
<body>
<button onclick="getNextMessage()">Chat</button>
<!-- <button onclick="getNextMessage()">Chat</button>
<p id="chatresult"></p>
<button onclick="streamNextMessage()">Chat (Stream)</button>
<p id="chatresult-stream"></p>
<p id="chatresult-stream"></p> -->
<button onclick="getSwitchboard()">Get SwitchBoard</button>
<pre id="zendesk-switchboard"></pre>
<button onclick="getSwitchboardIntegrations()">Get SwitchBoard Integrations</button>
<pre id="zendesk-switchboard-integrations"></pre>
<button onclick="getConversation()">Get Zendesk Conversation</button>
<pre id="zendesk-conversation"></pre>
<button onclick="getZendeskMessages()">Get Zendesk Messages</button>
<pre id="zendesk-messages"></pre>
<input type="text" id="userInput">
<button onclick="sendMessage()">Send</button>
<pre id="zendesk-send-message"></pre>
</body>
</html>
56 changes: 56 additions & 0 deletions test-browser-esm/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,59 @@ window.streamNextMessage = async () => {
});
stream.consume();
};

/**
*
* UI to show the conversation ID + messages
* user post message
* show ChatApiServer receiving it and reply
* UI update to show latest message
* user post message asking for agent
* show in Zendesk UI that a conversation is created
* show that when the ticket is close, it suppose to go back to the bot.
*/

window.getZendeskMessages = async () => {
const res = await fetch("http://localhost:3030/zendesk/getMessages")
const data = await res.json()
const el = document.getElementById("zendesk-messages");
el.textContent = JSON.stringify(data, null, 2);
// data.messages?.forEach((message) => {
// const p = document.createElement('p')
// p.textContent = `[id: ${message.id} | timestamp: ${message.received}]\n${message.author.type == 'user' ? message.author.displayName : 'business'}: ${message.content.text}`
// el.appendChild(p)
// })
}

window.getSwitchboard = async () => {
const res = await fetch("http://localhost:3030/zendesk/getSwitchboard")
const data = await res.json()
const el = document.getElementById("zendesk-switchboard");
el.textContent = JSON.stringify(data, null, 2);
}

window.getSwitchboardIntegrations = async () => {
const res = await fetch("http://localhost:3030/zendesk/getSwitchboardIntegrations")
const data = await res.json()
const el = document.getElementById("zendesk-switchboard-integrations");
el.textContent = JSON.stringify(data, null, 2);
}

window.getConversation = async () => {
const res = await fetch("http://localhost:3030/zendesk/getConversation")
const data = await res.json()
const el = document.getElementById("zendesk-conversation");
el.textContent = JSON.stringify(data, null, 2);
}

window.sendMessage = async () => {
const res = await fetch("http://localhost:3030/zendesk/sendMessage", {
method: "POST",
body: JSON.stringify({
text: document.getElementById("userInput").value
})
})
const data = await res.json()
const el = document.getElementById("zendesk-send-message");
el.textContent = JSON.stringify(data, null, 2);
}
189 changes: 183 additions & 6 deletions test-node-cjs/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import dotenv from "dotenv";

dotenv.config();

const SunshineConversationsClient = require('sunshine-conversations-client')
const defaultClient = SunshineConversationsClient.ApiClient.instance;
// Configure HTTP basic authorization: basicAuth
const basicAuth = defaultClient.authentications['basicAuth'];
basicAuth.username = 'app_65f47ae8b9abcc933fd17de3'
basicAuth.password = 'pmeuH7n_Eumw7gZwsTF8y-8T7vSswDJvac2W-4L8avh8P50bEUetWD2DCdtPLV4VFJBfMu_Opf5o8fLVlXWFVw'

// const convoApiInstance = new SunshineConversationsClient.ConversationsApi();
// const participantsApiInstance = new SunshineConversationsClient.ParticipantsApi();
const messagesApiInstance = new SunshineConversationsClient.MessagesApi();

const appId = "63320dceb6c27f00f3925dd2";
const conversationId = "65f8a3bac9c8043e32efccc7";

const config: ChatConfig = {
apiKey: process.env["TEST_BOT_API_KEY"] || "API_KEY_PLACEHOLDER",
botId: process.env["TEST_BOT_ID"] || "BOT_ID_PLACEHOLDER",
Expand All @@ -34,6 +48,137 @@ const request: MessageRequest = {
],
};


async function getZendeskMessages() {
try {
const res = await fetch("https://tags1632230385.zendesk.com/sc/v2/apps/63320dceb6c27f00f3925dd2/conversations/65f8a3bac9c8043e32efccc7/messages", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString("base64")
}
})
return res.json()
} catch (e) {
console.log('error', e)
}
}

async function getSwitchboard() {
try {
const res = await fetch("https://tags1632230385.zendesk.com/sc/v2/apps/63320dceb6c27f00f3925dd2/switchboards", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString("base64")
}
})
return res.json()
} catch (e) {
console.log('error', e)
}
}

async function getSwitchboardIntegrations() {
try {
const res = await fetch("https://tags1632230385.zendesk.com/sc/v2/apps/63320dceb6c27f00f3925dd2/switchboards/63320dcfa1d65400f3d8d425/switchboardIntegrations", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString("base64")
}
})
return res.json()
} catch (e) {
console.log('error', e)
}
}

async function getConversation() {
try {
const res = await fetch("https://tags1632230385.zendesk.com/sc/v2/apps/63320dceb6c27f00f3925dd2/conversations?filter[userExternalId]=123456789", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString("base64")
}
})
return res.json()
} catch (e) {
console.log('error', e)
}
}

async function sendMessage(input: string) {
try {
const res = await fetch("https://tags1632230385.zendesk.com/sc/v2/apps/63320dceb6c27f00f3925dd2/conversations/65f8a3bac9c8043e32efccc7/messages", {
method: "POST",
body: JSON.stringify({
author: {
type: "user",
userExternalId: "123456789"
},
content: {
type: "text",
text: input
}
}),
headers: {
"Content-Type": "application/json",
Authorization: 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString("base64")
}
})
return res.json()
} catch (e) {
console.log('error', e)
}
}

async function zendesk(req: any, res: any) {
// const messagePost = new SunshineConversationsClient.MessagePost(
// new SunshineConversationsClient.Author("user"),
// new SunshineConversationsClient.Author("user")
// );
let data: any;

//join conversation
// try {
// data = await participantsApiInstance.joinConversation(appId, conversationId, {
// userExternalId: '123456789',
// subscribeSDKClient: false
// })
// } catch (e) {
// console.error('ERROR1', e);
// res.end(JSON.stringify("ERROR", null, 2));
// return;
// }
// console.log('Join conversation successfully. Returned data: ' + data);
// res.end(JSON.stringify("DONE", null, 2));

//post message
try {
data = await messagesApiInstance.postMessage(appId, conversationId, {
author: {
type: 'user',
userExternalId: '123456789'
},
content: {
type: "text",
text: "this is a test message!"
},
metadata: {
"hello": "world"
}
})
} catch (e) {
console.error('ERROR2', e);
res.end(JSON.stringify("ERROR2", null, 2));
return;
}
console.log('Post message successfully. Returned data: ');
res.end(JSON.stringify(data, null, 2));
}

async function stream(res: any) {
const chatCore = provideChatCoreInternal(config, internalConfig);
const stream = await chatCore.streamNextMessage(request);
Expand All @@ -51,12 +196,44 @@ async function stream(res: any) {
const server = http.createServer(async (req: any, res: any) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
if (req.url === "/streaming") {
stream(res);
} else {
const chatCore = provideChatCoreInternal(config, internalConfig);
const reply = await chatCore.getNextMessage(request);
res.end(JSON.stringify(reply, null, 2));
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5050');
console.log(req.url)
if (req.url === '/zendesk') {
zendesk(req, res)
return;
}
if (req.url == '/zendesk/getMessages') {
const a = await getZendeskMessages()
res.end(JSON.stringify(a, null, 2));
return;
}
if (req.url == '/zendesk/getSwitchboard') {
const a = await getSwitchboard()
res.end(JSON.stringify(a, null, 2));
return;
}
if (req.url == '/zendesk/getSwitchboardIntegrations') {
const a = await getSwitchboardIntegrations()
res.end(JSON.stringify(a, null, 2));

Check failure on line 217 in test-node-cjs/node.ts

View workflow job for this annotation

GitHub Actions / linting / linting

'stream' is defined but never used. Allowed unused vars must match /^_/u
return;
}
if (req.url == '/zendesk/getConversation') {
const a = await getConversation()
res.end(JSON.stringify(a, null, 2));
return;
}
if (req.url == '/zendesk/sendMessage') {
const chunks: any[] = [];
req.on('data', (c: any) => {
chunks.push(c)
});
req.on('end', async () => {
const data = Buffer.concat(chunks).toString();
const parsedData = JSON.parse(data);
const a = await sendMessage(parsedData.text)
res.end(JSON.stringify(a, null, 2));
return;
})
}
});

Expand Down
Loading

0 comments on commit f96a434

Please sign in to comment.