Skip to content

Commit

Permalink
verify endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrealeo83 committed Dec 20, 2023
1 parent 8e48d0b commit 93ac2f1
Show file tree
Hide file tree
Showing 3 changed files with 1,320 additions and 3 deletions.
218 changes: 217 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ app.use(function (req, res, next) {

app.use(function (req, res, next) {
var urlobj = url.parse(req.originalUrl);
if (urlobj.pathname === '/' || urlobj.pathname.includes("/push/webhook/endpoint/") ) {
if (urlobj.pathname === '/' || urlobj.pathname === '/test' || urlobj.pathname.includes("/push/webhook/endpoint/") ) {
next();
return;
}
Expand Down Expand Up @@ -79,6 +79,222 @@ app.get("/", (req, res) => {
res.status(200).send("Chat21 Http Server v. 0.1.2")
})

// http://localhost:8004/test?projectid=646c838d55f7620013e4ab92&mqtt_endpoint=wss%3A%2F%2Feu.rtmv3.tiledesk.com%2Fmqws%2Fws&api_endpoint=https%3A%2F%2Fapi.tiledesk.com%2Fv3&chatapi_endpoint=https%3A%2F%2Feu.rtmv3.tiledesk.com%2Fchatapi%2Fapi
const { Chat21Client } = require('./mqttclient/chat21client.js');
app.get("/test", (req, res) => {

let TILEDESK_PROJECT_ID = "" || req.query.projectid;
// if (process.env && process.env.PERFORMANCE_TEST_TILEDESK_PROJECT_ID) {
// TILEDESK_PROJECT_ID = process.env.PERFORMANCE_TEST_TILEDESK_PROJECT_ID
// // console.log("TILEDESK_PROJECT_ID:", TILEDESK_PROJECT_ID);
// }
// else {
// throw new Error(".env.PERFORMANCE_TEST_TILEDESK_PROJECT_ID is mandatory");
// }

// console.log("process.env.PERFORMANCE_TEST_MQTT_ENDPOINT:", process.env.PERFORMANCE_TEST_MQTT_ENDPOINT);
let MQTT_ENDPOINT = "" || req.query.mqtt_endpoint;
// if (process.env && process.env.PERFORMANCE_TEST_MQTT_ENDPOINT) {
// MQTT_ENDPOINT = process.env.PERFORMANCE_TEST_MQTT_ENDPOINT
// // console.log("MQTT_ENDPOINT:", MQTT_ENDPOINT);
// }
// else {
// throw new Error(".env.PERFORMANCE_TEST_MQTT_ENDPOINT is mandatory");
// }

let API_ENDPOINT = "" || req.query.api_endpoint;
// if (process.env && process.env.PERFORMANCE_TEST_API_ENDPOINT) {
// API_ENDPOINT = process.env.PERFORMANCE_TEST_API_ENDPOINT
// // console.log("API_ENDPOINT:", API_ENDPOINT);
// }
// else {
// throw new Error(".env.PERFORMANCE_TEST_API_ENDPOINT is mandatory");
// }

let CHAT_API_ENDPOINT = "" || req.query.chatapi_endpoint;
// if (process.env && process.env.PERFORMANCE_TEST_CHAT_API_ENDPOINT) {
// CHAT_API_ENDPOINT = process.env.PERFORMANCE_TEST_CHAT_API_ENDPOINT
// // console.log("CHAT_API_ENDPOINT:", CHAT_API_ENDPOINT);
// }
// else {
// throw new Error(".env.PERFORMANCE_TEST_CHAT_API_ENDPOINT is mandatory");
// }

let config = {
MQTT_ENDPOINT: MQTT_ENDPOINT,
CHAT_API_ENDPOINT: CHAT_API_ENDPOINT,
APPID: 'tilechat',
TILEDESK_PROJECT_ID: TILEDESK_PROJECT_ID,
MESSAGE_PREFIX: "Performance-test",
}

let user1 = {
fullname: 'User 1',
firstname: 'User',
lastname: '1',
};




let chatClient1 = new Chat21Client(
{
appId: config.APPID,
MQTTendpoint: config.MQTT_ENDPOINT,
APIendpoint: config.CHAT_API_ENDPOINT,
log: false
});

(async () => {
let userdata;
try {
userdata = await createAnonymousUser(TILEDESK_PROJECT_ID, API_ENDPOINT);
}
catch(error) {
console.log("An error occurred during anonym auth:", error);
process.exit(0);
}

user1.userid = userdata.userid;
user1.token = userdata.token;

let group_id;
let group_name;

console.log("Message delay check.");
console.log("MQTT endpoint:", config.MQTT_ENDPOINT);
console.log("API endpoint:", config.CHAT_API_ENDPOINT);
console.log("Tiledesk Project Id:", config.TILEDESK_PROJECT_ID);

console.log("Connecting...")
chatClient1.connect(user1.userid, user1.token, () => {
console.log("chatClient1 connected and subscribed.");
group_id = "support-group-" + "64690469599137001a6dc6f5-" + uuid().replace(/-+/g, "");
group_name = "benchmarks group => " + group_id;
send(group_id, group_name, chatClient1, config, user1, function(delay) {
chatClient1.close()
res.json({delay: delay});

// process.exit(0);
});
});
})();


// res.status(200).send("Chat21 Http Server v. 0.1.2")
})



async function send(group_id, group_name, chatClient, config, user, callback) {
console.log("\n\n***********************************************");
console.log("********* Single message delay script *********");
console.log("***********************************************\n\n");
let time_sent = Date.now();
let handler = chatClient.onMessageAdded((message, topic) => {
console.log("> Incoming message [sender:" + message.sender_fullname + "]: " + message.text);
if (
message &&
message.text.startsWith(config.MESSAGE_PREFIX) &&
(message.sender_fullname !== "User 1" && message.sender_fullname !== "System") && // bot is the sender
message.recipient === group_id
) {
console.log("> Incoming message (sender is the chatbot) used for computing ok.");
let text = message.text.trim();
let time_received = Date.now();
let delay = time_received - time_sent;
console.log("Total delay:" + delay + "ms");

callback(delay);
}
else {
console.log("Message not computed:", message.text);
}
});
console.log("Sending test message...");
let recipient_id = group_id;
let recipient_fullname = group_name;
let message_UUID = uuid().replace(/-+/g, "");
sendMessage(message_UUID, recipient_id, recipient_fullname, async (latency) => {
console.log("Sent ok:", message_UUID);
}, chatClient, config, user);
}

function sendMessage(message_UUID, recipient_id, recipient_fullname, callback, chatClient, config, user) {
const sent_message = config.MESSAGE_PREFIX + "/"+ message_UUID;
console.log("Sending message with text:", sent_message);

chatClient.sendMessage(
sent_message,
'text',
recipient_id,
recipient_fullname,
user.fullname,
{projectId: config.TILEDESK_PROJECT_ID},
null, // no metadata
'group',
(err, msg) => {
if (err) {
console.error("Error send:", err);
}
console.log("Message Sent ok:", msg);
}
);
}

async function createAnonymousUser(tiledeskProjectId,API_ENDPOINT) {
ANONYMOUS_TOKEN_URL = API_ENDPOINT + '/auth/signinAnonymously';
console.log("Getting ANONYMOUS_TOKEN_URL:", ANONYMOUS_TOKEN_URL);
return new Promise((resolve, reject) => {
let data = JSON.stringify({
"id_project": tiledeskProjectId
});

let axios_config = {
method: 'post',
url: ANONYMOUS_TOKEN_URL, //'https://api.tiledesk.com/v3/auth/signinAnonymously',
headers: {
'Content-Type': 'application/json'
},
data : data
};

axios.request(axios_config)
.then((response) => {
console.log("Got Anonymous Token:", JSON.stringify(response.data.token));
CHAT21_TOKEN_URL = API_ENDPOINT + '/chat21/native/auth/createCustomToken';
let config = {
method: 'post',
maxBodyLength: Infinity,
url: CHAT21_TOKEN_URL,
headers: {
'Authorization': response.data.token
}
};

axios.request(config)
.then((response) => {
// console.log(response);

const mqtt_token = response.data.token;
const chat21_userid = response.data.userid;
resolve({
userid: chat21_userid,
token: mqtt_token
});
})
.catch((error) => {
console.log(error);
reject(error);
});
})
.catch((error) => {
console.log(error);
reject(error)
});
});
}

app.get("/verify", (req, res) => {
const decoded = decodejwt(req)
res.status(200).send(decoded)
Expand Down
Loading

0 comments on commit 93ac2f1

Please sign in to comment.