Created
May 16, 2022 19:42
-
-
Save OsvaldoFrias/6e536312a1a08cb21a3711c812bf9326 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BOT_TOKEN = ''; | |
const URL_BASE = 'http://localhost:21093'; | |
const REST_API_URL_BASE = 'http://localhost:8898'; | |
const request = require('request'); | |
// const fs = require('fs'); | |
const http = require('http'); | |
const https = require('https'); | |
const url = require('url'); | |
const { Telegraf } = require('telegraf'); | |
const bot = new Telegraf(BOT_TOKEN); | |
bot.start(async (ctx) => { | |
ctx.reply('Bienvenido al Bot de reconocimiento facial de SecurOS FaceX. Envíe la foto de un rostro.'); | |
}); | |
bot.on("message", async (ctx) => { | |
// console.log(ctx); | |
// console.log(ctx.message.contact); | |
const securOSUser = await findTelegramUser(ctx.from.username); | |
if (securOSUser.length === 0) { | |
await ctx.reply('Esta cuenta de Telegram no está registrada para usar el bot de reconocimiento facial.'); | |
return; | |
} | |
let queryData = url.parse(`?${securOSUser[0].comment.replace('\n', '&')}`, true).query; | |
// console.log(queryData); | |
if (!securOSUser[0].comment || !queryData['TELEGRAM_BOT_MESSAGES'] || queryData['TELEGRAM_BOT_MESSAGES'].trim().toLowerCase() === 'false') { | |
await ctx.reply('Esta cuenta de Telegram no tiene permisos para enviar y recibir mensajes del bot de reconocimiento facial.'); | |
return; | |
} | |
if (ctx.message.photo) { | |
await ctx.reply('Procesando imagen...'); | |
await ctx.replyWithChatAction('typing'); | |
const fileId = ctx.message.photo[ctx.message.photo.length - 1].file_id; | |
const urlFiles = `https://api.telegram.org/bot${BOT_TOKEN}/getFile?file_id=${fileId}`; | |
// console.log(urlFiles); | |
request(urlFiles, function (error, response, body) { | |
const files = JSON.parse(body); | |
// console.log(files); | |
const filePath = files.result.file_path; | |
const urlFile = `https://api.telegram.org/file/bot${BOT_TOKEN}/${filePath}`; | |
console.log(urlFile); | |
createImportSession() | |
.then(async (session) => { | |
const formData = await createFormData(ctx, urlFile); | |
const addImageResponse = await addImageToSession(session.id, formData); | |
if (addImageResponse.statusCode !== 201) { | |
console.log(addImageResponse.body); | |
await ctx.reply('Error interno.'); | |
return; | |
} | |
const processImageResponse = await processImageOnSession(session.id); | |
if (processImageResponse.statusCode !== 202) { | |
console.log(processImageResponse.body); | |
await ctx.reply('Error procesando la imagen.'); | |
return; | |
} | |
let state = "queued"; | |
let responseProcess; | |
while (state === "queued" || state === "processing") { | |
responseProcess = await resultProcess(session.id); | |
//console.log(responseProcess, responseProcess.state); | |
state = responseProcess.state; | |
await sleep(1000); | |
} | |
if (state === "completed") { | |
const items = responseProcess.items; | |
// console.log(items) | |
if (items && items.length > 0) { | |
for (let idx in items) { | |
const item = items[idx]; | |
const faces = item.faces; | |
if (faces && faces.length > 0) { | |
for (let jdx in faces) { | |
const face = faces[jdx]; | |
const faceMatches = face['face_matches']; | |
if (faceMatches && faceMatches.length > 0) { | |
for (let kdx in faceMatches) { | |
const faceMatch = faceMatches[kdx]; | |
const personMatch = faceMatch['person']['_links']['_self']; | |
const faceImage = faceMatch['matched_person_face_image']['_links']['source']; | |
const personFound = await httpGet(`${URL_BASE}${personMatch}`); | |
let personData = `👤 ${personFound.first_name || ''} ${personFound.middle_name || ''} ${personFound.last_name || ''}`; | |
let listData = `${personFound.lists.reduce((previousValue, currentValue) => { | |
let priority = currentValue.priority === 0 ? `🟥` : currentValue.priority === 1 ? `🟦` : `✅`; | |
return previousValue + `${priority} - ${currentValue.name}` + '\n'; | |
}, '\n')}`; | |
// ctx.replyWithPhoto({ url: `${URL_BASE}${faceImage}` }, { caption: "Coincidencia encontrada" + '\n' + JSON.stringify(personFound) }); | |
await ctx.replyWithPhoto({ | |
source: await getStreamFromUrlHttp(`${URL_BASE}${faceImage}`) | |
}, { | |
caption: "Coincidencia encontrada" + '\n' + personData + listData | |
}); | |
} | |
await ctx.reply('Procesamiento finalizado.'); | |
return; | |
} | |
} | |
} | |
} | |
} | |
await ctx.reply('Procesamiento finalizado. No se encontraron coincidencias.'); | |
} | |
}).catch(async (err) => { | |
console.log(err); | |
await ctx.reply('Hubo un error, favor de notificar al administrador del sistema.'); | |
}); | |
}); | |
} else { | |
await ctx.reply('Favor de subir unicamente fotografías.'); | |
} | |
}); // obtiene foto | |
// para obtener la foto se debe se debe consultar los siguientes URL | |
// utilizar el file_id del resultado del json ctx.message.photo y meterlo en la url: | |
// https://api.telegram.org/bot5223428242:AAF4fS0ZM1GRtmNTIfkyf6yeCMUPBNCck-Q/getFile?file_id=AgACAgEAAxkBAAMGYl90JcltMLSJ0d3sRGqHnKgT8Q8AAheqMRvYAAH5RkX8VY3edXIiAQADAgADeAADJAQ | |
// como respuesta el file_id indicara el nombre del archivo guardado en los servidores de telegram de forma temporal | |
// Para descargar utilizar el siguiente URL | |
// https://api.telegram.org/file/bot5223428242:AAF4fS0ZM1GRtmNTIfkyf6yeCMUPBNCck-Q/photos/file_0.jpg donde filce_0 corresponde al nombre de la image que se utiliza para realizar el analitico. | |
bot.launch(); | |
function findTelegramUser(telegramUser) { | |
return new Promise((resolve, reject) => { | |
httpGet(REST_API_URL_BASE + '/api/v1/persons') | |
.then(async (persons) => { | |
const res = persons.data.filter(p => { | |
return p.name === telegramUser; | |
}); | |
resolve(res); | |
}) | |
.catch(ex => { | |
console.log(ex); | |
reject(ex); | |
}); | |
}); | |
} | |
function createImportSession() { | |
return new Promise((resolve, reject) => { | |
request.post(`${URL_BASE}/v1/spotter/import/session`, (err, httpResponse, body) => { | |
if (err) return reject(err); | |
console.log(httpResponse.statusCode, body, JSON.parse(body).id); | |
resolve(JSON.parse(body)); | |
}); | |
}); | |
} | |
async function createFormData(ctx, urlFile) { | |
const formData = { | |
data: { | |
value: JSON.stringify({ | |
"source": ctx.from.username, | |
"first_name": ctx.from.first_name, | |
"last_name": "Telegram" | |
}), | |
options: { | |
contentType: 'application/json' | |
} | |
}, | |
image: { | |
// value: fs.createReadStream('C:/Users/osval/Downloads/cristian.jpg'), | |
value: await getStreamFromUrl(urlFile), | |
options: { | |
filename: 'image.jpg', | |
contentType: 'image/jpeg' | |
} | |
} | |
} | |
return formData; | |
} | |
function addImageToSession(sessionId, formData) { | |
return new Promise((resolve, reject) => { | |
request.post({ | |
url: `${URL_BASE}/v1/spotter/import/session/${sessionId}?action=add_image`, | |
headers: { | |
"Content-Type": "multipart/form-data" | |
}, | |
formData | |
}, (err, httpResponse, body) => { | |
if (err) reject(err); | |
// console.log(httpResponse.statusCode, body, JSON.parse(body).id); | |
resolve(httpResponse); | |
}); | |
}); | |
} | |
function processImageOnSession(sessionId) { | |
return new Promise((resolve, reject) => { | |
request.post(`${URL_BASE}/v1/spotter/import/session/${sessionId}?action=process`, (err, httpResponse, body) => { | |
if (err) reject(err); | |
// console.log(httpResponse.statusCode, body); | |
resolve(httpResponse); | |
}); | |
}); | |
} | |
function resultProcess(sessionId) { | |
return new Promise((resolve, reject) => { | |
request(`${URL_BASE}/v1/spotter/import/session/${sessionId}`, (err, httpResponse, body) => { | |
if (err) reject(err); | |
// console.log(httpResponse.statusCode, body); | |
resolve(JSON.parse(body)); | |
}) | |
}); | |
} | |
function httpGet(url) { | |
return new Promise((resolve, reject) => { | |
request(url, (err, httpResponse, body) => { | |
if (err) reject(err); | |
// console.log(httpResponse.statusCode, body); | |
resolve(JSON.parse(body)); | |
}) | |
}); | |
} | |
function getStreamFromUrlHttp(url) { | |
return new Promise((resolve, reject) => { | |
try { | |
http.get(url, (stream) => { | |
resolve(stream); | |
}); | |
} catch (ex) { | |
console.error(ex); | |
reject(ex); | |
} | |
}); | |
} | |
function getStreamFromUrl(url) { | |
return new Promise((resolve, reject) => { | |
try { | |
https.get(url, (stream) => { | |
resolve(stream); | |
}); | |
} catch (ex) { | |
console.error(ex); | |
reject(ex); | |
} | |
}); | |
} | |
function sleep(ms) { | |
return new Promise((resolve) => { | |
setTimeout(resolve, ms); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment