Commit 2761bd39 by Nick N. Sukharev

Обновление модуля 'shared' до версии шаблона dasha-templates/developer/

1 parent 677b11a7
Showing with 101 additions and 16 deletions
...@@ -4,7 +4,7 @@ const axios = require("axios"); ...@@ -4,7 +4,7 @@ const axios = require("axios");
const moment = require("moment"); const moment = require("moment");
const strings = require("string"); const strings = require("string");
const dataset = require("./dataset"); const dataset = require("./dataset");
const sys = require('child_process'); const sys = require("child_process");
const cct_api = 'http://cct.r-broker.ru/api/'; const cct_api = 'http://cct.r-broker.ru/api/';
const cct_key = 'y8Bzq1C0vIwhcrl98emy04JauUlWd7glhvUo2TcWBSkdMYzrKS' const cct_key = 'y8Bzq1C0vIwhcrl98emy04JauUlWd7glhvUo2TcWBSkdMYzrKS'
...@@ -20,19 +20,11 @@ const api_hook_key = 'kd56h4jfhj54f62hjf6d8YgjL9'; ...@@ -20,19 +20,11 @@ const api_hook_key = 'kd56h4jfhj54f62hjf6d8YgjL9';
const api_hook = api_hook_url + '?key=' + api_hook_key; const api_hook = api_hook_url + '?key=' + api_hook_key;
const tg_wa_hook_url = 'https://ai.r-broker.ru/demo-hooks/voice-bot.php' const tg_wa_hook_url = 'https://ai.r-broker.ru/demo-hooks/voice-bot.php'
const tg_wa_options = { auth: { username: 'web', password: 'web' } }; const ai_hooks_opts = { auth: { username: 'web', password: 'web' } };
const answering_machine_hook = 'https://ai.r-broker.ru/demo-hooks/check-answering-machine.php';
const mobile_codes = dataset.mobile_codes(); const mobile_codes = dataset.mobile_codes();
function empty (value) {
if (value === undefined) return true;
if (value === null) return true;
const str = strings(value);
if (str == "") return true;
if (str.trim() == "") return true;
return false;
}
async function cct_api_service (service, api_request) { async function cct_api_service (service, api_request) {
let post_data = JSON.stringify(api_request); let post_data = JSON.stringify(api_request);
let headers = { let headers = {
...@@ -51,7 +43,12 @@ async function cct_api_service (service, api_request) { ...@@ -51,7 +43,12 @@ async function cct_api_service (service, api_request) {
module.exports = exports = { module.exports = exports = {
empty (value) { empty (value) {
return empty (value); if (value === undefined) return true;
if (value === null) return true;
const str = strings(value);
if (str == "") return true;
if (str.trim() == "") return true;
return false;
}, },
// Веб-хук для отправки сообщений через Telegram и WhatsApp // Веб-хук для отправки сообщений через Telegram и WhatsApp
...@@ -61,7 +58,7 @@ module.exports = exports = { ...@@ -61,7 +58,7 @@ module.exports = exports = {
let result = {}; let result = {};
let wrong_data = { status: 'error', detail: 'axios crashed' }; let wrong_data = { status: 'error', detail: 'axios crashed' };
try { try {
result = await axios.post(tg_wa_hook_url, post_data, tg_wa_options); result = await axios.post(tg_wa_hook_url, post_data, ai_hooks_opts);
} catch (err) { } catch (err) {
wrong_data.detail = JSON.stringify(err, undefined, 2); wrong_data.detail = JSON.stringify(err, undefined, 2);
result.data = wrong_data; result.data = wrong_data;
...@@ -70,6 +67,7 @@ module.exports = exports = { ...@@ -70,6 +67,7 @@ module.exports = exports = {
return result.data; return result.data;
}, },
// Веб-хук для передачи отчёта о разговоре на машину AI
async ai_api_hook (action, web_hook_data) { async ai_api_hook (action, web_hook_data) {
let api_hook_action = api_hook + '&action=' + action; let api_hook_action = api_hook + '&action=' + action;
let post_data = new url.URLSearchParams(web_hook_data); let post_data = new url.URLSearchParams(web_hook_data);
...@@ -81,7 +79,7 @@ module.exports = exports = { ...@@ -81,7 +79,7 @@ module.exports = exports = {
wrong_data.detail = JSON.stringify(err, undefined, 2); wrong_data.detail = JSON.stringify(err, undefined, 2);
result.data = wrong_data; result.data = wrong_data;
} }
if (empty(result.data)) return wrong_data if (this.empty(result.data)) return wrong_data
return result.data; return result.data;
}, },
...@@ -96,6 +94,22 @@ module.exports = exports = { ...@@ -96,6 +94,22 @@ module.exports = exports = {
return 'night'; return 'night';
}, },
dasha_format(abonent_phone) {
let clean_phone = abonent_phone.replace( /[^\d]/g, '' );
var size = clean_phone.length;
if (size == 11) {
prefix = clean_phone.substring(0, 1);
if (prefix == 8) {
return "7" + clean_phone.substring(1);
}
return clean_phone
}
if (size == 10) {
return "7" + clean_phone;
}
return clean_phone;
},
human_format(abonent_phone) { human_format(abonent_phone) {
let clean_phone = abonent_phone.replace( /[^\d]/g, '' ); let clean_phone = abonent_phone.replace( /[^\d]/g, '' );
var size = clean_phone.length; var size = clean_phone.length;
...@@ -148,12 +162,14 @@ module.exports = exports = { ...@@ -148,12 +162,14 @@ module.exports = exports = {
return clean_phone; return clean_phone;
}, },
async cct_numbers_from_text (text) { // Веб-хук для извлечения чисел из текста через Т.У.К.
async cct_numbers_from_text (text, option) {
const service = 'markers-data'; const service = 'markers-data';
request = { request = {
debug: false, debug: false,
input: "text", input: "text",
channel: "client", channel: "client",
multipliers: option,
distance: { "min": 0.166667, "mid": 0.166667, "max": 0.166667 }, distance: { "min": 0.166667, "mid": 0.166667, "max": 0.166667 },
language: "russian", language: "russian",
text: text, text: text,
...@@ -183,12 +199,14 @@ module.exports = exports = { ...@@ -183,12 +199,14 @@ module.exports = exports = {
return result; return result;
}, },
async cct_dates_from_text (text) { // Веб-хук для извлечения дат из текста через Т.У.К.
async cct_dates_from_text (text, option) {
const service = 'markers-data'; const service = 'markers-data';
request = { request = {
debug: false, debug: false,
input: "text", input: "text",
channel: "client", channel: "client",
multipliers: option,
distance: { "min": 0.166667, "mid": 0.166667, "max": 0.166667 }, distance: { "min": 0.166667, "mid": 0.166667, "max": 0.166667 },
language: "russian", language: "russian",
text: text, text: text,
...@@ -223,6 +241,56 @@ module.exports = exports = { ...@@ -223,6 +241,56 @@ module.exports = exports = {
return result; return result;
}, },
// Веб-хук для извлечения времени из текста через Т.У.К.
async cct_times_from_text (text, option) {
const service = 'markers-data';
request = {
debug: false,
input: "text",
channel: "client",
multipliers: option,
distance: { "min": 0.166667, "mid": 0.166667, "max": 0.166667 },
language: "russian",
text: text,
triggers: [
{ name: "times", markers: [ { type: "date time" } ] }
]
};
const response = await cct_api_service(service, request);
let result = "";
if (response.status !== "success") return result;
for (trigger of response.result) {
if (trigger.state === "on") {
if (trigger.name === "times") {
for (i in trigger.details) {
found = trigger.details[i];
for (j in found) {
element = found[j];
pattern = element.pattern??"";
if ((pattern === "%time%") && (result === ""))
result = element.value;
}
}
}
}
}
if (result === "") return { full: "", date: "", time: "", text: "" };
moment.locale("ru");
const result_date = result.replace( /\s.+$/, '' );
const result_time = result.replace( /^.+\s/, '' );
const result_text = moment(result_date, "YYYY-MM-DD").format("D MMMM")
+ ' в ' + result_time;
return {
full: result,
date: moment(result_date, "YYYY-MM-DD").format("DD.MM.YYYY"),
time: result_time,
text: result_text
};
},
renew_last_log_file(last_text_file) { renew_last_log_file(last_text_file) {
let sysCommand = 'cat ' + last_text_file + ' >log.txt'; let sysCommand = 'cat ' + last_text_file + ' >log.txt';
sys.exec(sysCommand, (error, stdout, stderr) => { sys.exec(sysCommand, (error, stdout, stderr) => {
...@@ -352,6 +420,23 @@ module.exports = exports = { ...@@ -352,6 +420,23 @@ module.exports = exports = {
app_id: app_id, app_id: app_id,
job_id: job_id job_id: job_id
}; };
},
// Веб-хук для проверки реплик по подозрению на автоответчик
async check_answered_machine(text) {
let post_data = new url.URLSearchParams({ text: text });
let wrong_data = { status: 'error', load: 0, weight: -1, details: [] };
try
{
var result = await axios.post(answering_machine_hook, post_data, ai_hooks_opts);
}
catch (err)
{
wrong_data.details = JSON.stringify(err, undefined, 2);
result.data = wrong_data;
}
if (this.empty(result.data)) return wrong_data
return result.data;
} }
}; };
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!