// require(Modules.ASR); var call, asr, city, cityId, Weather = {}, // Yandex Weather.cities tts_voice = Language.RU_RUSSIAN_FEMALE; // text-to-speech // function getCities() { var opts = new Net.HttpRequestOptions(); opts.rawOutput = true; Net.httpRequest("http://weather.yandex.ru/static/cities.json", function (e) { if (e.code == 200) { // json octet-stream var response = bytes2str(e.data, 'utf-8'); // Weather.cities eval(response); } else Logger.write("Couldn't load cities. Status: " + e.code); }, opts); } // id - function getCityId(name) { for (var i in Weather.cities) { var country = Weather.cities[i]; for (var k in country) { if (k.toLowerCase() == name.toLowerCase()) return country[k]; } } return -1; // } // ( ), , , VoxEngine.addEventListener(AppEvents.Started, function (e) { getCities(); call = VoxEngine.callPSTN(" ", " "); // , , Voximplant , caller id call.addEventListener(CallEvents.Connected, handleCallConnected); // call.addEventListener(CallEvents.Failed, cleanup); call.addEventListener(CallEvents.Disconnected, cleanup); }); // ( ) VoxEngine.addEventListener(AppEvents.CallAlerting, function (e) { getCities(); call = e.call; call.answer(); // call.addEventListener(CallEvents.Connected, handleCallConnected); // call.addEventListener(CallEvents.Disconnected, cleanup); }); // function handleCallConnected(e) { call.say("! , .", tts_voice); enableASR(); } // function enableASR() { // ASR, , - ( ) asr = VoxEngine.createASR(ASRLanguage.RUSSIAN_RU, ASRDictionary.ADDRESS_RU); // , asr.addEventListener(ASREvents.CaptureStarted, function (e) { call.stopPlayback(); }); asr.addEventListener(ASREvents.Result, handleRecognitionResult); // ASR call.sendMediaTo(asr); } // function handleRecognitionResult(e) { // ASR asr.stop(); // 50% if (e.confidence >= 50) { city = e.text; if (city.toLowerCase() == "") city = "-"; // cityId = getCityId(city); if (cityId == -1 && /\s/g.test(city)) { Logger.write("Replacing whitespace"); city = city.replace(/\s/g, "-"); cityId = getCityId(city); } if (cityId !== -1) { call.say(" " + city, tts_voice); call.addEventListener(CallEvents.PlaybackFinished, handleCityChosen); } else { call.say(" , . , , .", tts_voice); enableASR(); } } else { call.say(" , . , , .", tts_voice); enableASR(); } } // function declOfNum(number, titles) { cases = [2, 0, 1, 1, 1, 2]; return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]]; } // function handleCityChosen(e) { call.removeEventListener(CallEvents.PlaybackFinished, handleCityChosen); // cityId Net.httpRequest("http://export.yandex.ru/weather-ng/forecasts/" + cityId + ".xml", function (e) { if (e.code == 200) { Logger.write("Weather info has been loaded"); var data = e.text.split("\n").slice(1).join("\n"), forecast = new XML(data), ns = new Namespace('w', 'http://weather.yandex.ru/forecast'), temperature = forecast.ns::fact.ns::temperature, humidity = forecast.ns::fact.ns::humidity, pressure = forecast.ns::fact.ns::pressure, wind_speed = forecast.ns::fact.ns::wind_speed; wind_speed = parseFloat(wind_speed).toFixed(1).replace(/\.0{1}$/, ""); var forecast_string = " " + parseNumber(temperature) + " " + declOfNum(Math.abs(temperature), ['', '', '']) + ", " + forecast.ns::fact.ns::weather_type + ", " + parseNumber(humidity) + " " + declOfNum(humidity, ['', '', '']) + ", " + " " + parseNumber(pressure) + " " + declOfNum(pressure, ['', '', '']) + " , " + " " + parseNumber(wind_speed) + " " + declOfNum(wind_speed, ['', '', '']) + " "; // call.say(forecast_string, tts_voice); call.addEventListener(CallEvents.PlaybackFinished, VoxEngine.terminate); } else { Logger.write("Couldn't load weather info. Status: " + e.code); call.say(" , . , .", tts_voice); call.addEventListener(CallEvents.PlaybackFinished, VoxEngine.terminate); } }); } // function cleanup(e) { Logger.write("Cleanup"); VoxEngine.terminate(); }
call = VoxEngine.callPSTN(" ", " ");
, number format - 7xxxxxxxxxxx, well, or another country code, if you are not in Russia. Also indicate the number from which the call will be made; You can confirm the number you own through the Voximplant control panel. Save, you can call the SmartIVR script. If you carefully looked at the code, you noticed that there is a function parseNumber, which is not declared anywhere. When calling, the executed scripts can be combined, so we can make a separate parseNumber script and insert the code of this function there: var parseNumber = function () { var dictionary = [ ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" ], ["", "", "", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", "", ""], ["|||", "|||", "|||", "|||"] ]; function getNumber(number, limit) { var temp = number.match(/^\d{1,3}([,|\s]\d{3})+/); if (temp) return temp[0].replace(/[,|\s]/g, ""); temp = Math.abs(parseInt(number)); if (temp !== temp || temp > limit) return null; return String(temp); }; function setEnding(variants, number) { variants = variants.split("|"); number = number.charAt(number.length - 2) === "1" ? null : number.charAt(number.length - 1); switch (number) { case "1": return variants[0] + variants[1]; case "2": case "3": case "4": return variants[0] + variants[2]; default: return variants[0] + variants[3]; }; }; function getPostfix(postfix, number) { if (typeof postfix === "string" || postfix instanceof String) { if (postfix.split("|").length < 3) return " " + postfix; return " " + setEnding(postfix, number); }; return ""; }; return function (number, postfix) { if (typeof number === "undefined") return "999" + new Array(dictionary[3].length + 1).join(" 999"); number = String(number); var minus = false; number.replace(/^\s+/, "").replace(/^-\s*/, function () { minus = true; return ""; }); number = getNumber(number, Number(new Array(dictionary[3].length + 2).join("999"))); if (!number) return ""; postfix = getPostfix(postfix, number); if (number === "0") return "" + postfix; var position = number.length, i = 0, j = 0, result = []; while (position--) { result.unshift(dictionary[i++][number.charAt(position)]); if (i === 2 && number.charAt(position) === "1") result.splice(0, 2, dictionary[0][number.substring(position, position + 2)]); if (i === 3 && position !== 0) { i = 0; if (position > 3 && number.substring(position - 3, position) === "000") { j++; continue; }; result.unshift(setEnding(dictionary[3][j++], number.substring(0, position))); }; }; position = result.length - 5; switch (result[position]) { case "": result[position] = ""; break; case "": result[position] = ""; break; }; if (minus) result.unshift(""); return result.join(" ").replace(/\s+$/, "").replace(/\s+/g, " ") + postfix; }; }();
Source: https://habr.com/ru/post/231319/
All Articles