puvox-library 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library_standard.js +125 -52
- package/package.json +1 -1
- package/test.js +8 -0
package/library_standard.js
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
* const helpers = new PuvoxLibrary();
|
11
11
|
* console.log ( helpers.get_last_child_of_array(array) );
|
12
12
|
* console.log ( helpers.get_visitor_ip() );
|
13
|
+
* console.log ( helpers.telegramMessage("hello world", "1234567890", "BOTKEY123:456789") );
|
13
14
|
* ... etc
|
14
15
|
*
|
15
16
|
*/
|
@@ -18,7 +19,7 @@ const puvox_library =
|
|
18
19
|
{
|
19
20
|
// ########## ARRAY ########## //
|
20
21
|
arrayValue(obj_arr, key, default_){
|
21
|
-
return (key in obj_arr ? obj_arr[key] : default_);
|
22
|
+
return (obj_arr && key in obj_arr ? obj_arr[key] : default_);
|
22
23
|
},
|
23
24
|
arrayValueLower(obj_arr, key, default_){
|
24
25
|
const val = this.arrayValue(obj_arr, key, default_);
|
@@ -382,8 +383,9 @@ const puvox_library =
|
|
382
383
|
isArray(x) { return ( (!!x) && (x.constructor === Array) ) || (Array.isArray(x)); },
|
383
384
|
|
384
385
|
isSimpleVariableType(obj){ return this.isSimpleVariableTypeName(typeof obj); },
|
385
|
-
isSimpleVariableTypeName(
|
386
|
-
|
386
|
+
isSimpleVariableTypeName(typeName_){ return this.inArray( [ "boolean", "integer", "float", "double", "decimal", "string"], typeName_); },
|
387
|
+
isNumericVariableType(obj){ return this.isNumericVariableTypeName(typeof obj); },
|
388
|
+
isNumericVariableTypeName(typeName_){ return this.inArray( [ "integer", "float", "double", "decimal"], typeName_); },
|
387
389
|
|
388
390
|
stringToBoolean(string){
|
389
391
|
switch(string.toLowerCase().trim()){
|
@@ -460,12 +462,18 @@ const puvox_library =
|
|
460
462
|
trimOnlyFromEnd(content){
|
461
463
|
return content.replace(/\s*$/,"");
|
462
464
|
},
|
465
|
+
startsWith(content, what){
|
466
|
+
return content.startsWith(what);
|
467
|
+
},
|
463
468
|
startsWithArray(content,array){
|
464
469
|
array.forEach(function(val){
|
465
470
|
if (content.startsWith(val)) return true;
|
466
471
|
})
|
467
472
|
return false;
|
468
473
|
},
|
474
|
+
endsWith(content, what){
|
475
|
+
return content.endsWith(what);
|
476
|
+
},
|
469
477
|
endsWithArray(content,array){
|
470
478
|
array.forEach(function(val){
|
471
479
|
if (content.endsWith(val)) return true;
|
@@ -1295,6 +1303,8 @@ const puvox_library =
|
|
1295
1303
|
)
|
1296
1304
|
},
|
1297
1305
|
|
1306
|
+
milliseconds(){ return (new Date().getTime()); },
|
1307
|
+
|
1298
1308
|
fancyTimeFormat(time)
|
1299
1309
|
{
|
1300
1310
|
var time = time.toFixed(0);
|
@@ -2467,36 +2477,68 @@ const puvox_library =
|
|
2467
2477
|
|
2468
2478
|
|
2469
2479
|
// region ### TELEGRAM FUNCTIONS ###
|
2470
|
-
|
2471
|
-
|
2472
|
-
|
2473
|
-
|
2474
|
-
|
2475
|
-
|
2476
|
-
|
2480
|
+
async telegramMessage(text, chat_id, bot_key, extra_opts={}){
|
2481
|
+
const is_repeated_call = 'is_repeated_call' in extra_opts;
|
2482
|
+
const use_cache = 'cache' in extra_opts;
|
2483
|
+
if (! ('parse_mode' in extra_opts)){
|
2484
|
+
extra_opts['parse_mode'] = 'html';
|
2485
|
+
}
|
2486
|
+
if (! ('disable_web_page_preview' in extra_opts)){
|
2487
|
+
extra_opts['disable_web_page_preview'] = true;
|
2488
|
+
}
|
2489
|
+
// whether it's without `-100` prefix
|
2490
|
+
chat_id = chat_id.toString();
|
2491
|
+
if (!this.startsWith(chat_id, '-100')) chat_id = '-100' + chat_id;
|
2492
|
+
text = this.br2nl(text);
|
2493
|
+
text = this.stripTags(text,'<b><strong><i><em><u><ins><s><strike><del><a><code><pre>'); // allowed: https://core.telegram.org/bots/api#html-style
|
2494
|
+
text = text.substring(0, 4095); //max telegram message length 4096
|
2495
|
+
const requestOpts = Object.assign({'chat_id':chat_id, 'text':text}, extra_opts);
|
2496
|
+
delete requestOpts['cache'];
|
2497
|
+
delete requestOpts['is_repeated_call'];
|
2498
|
+
const responseText = await this.getRemoteData('https://api.telegram.org/bot'+ bot_key +'/sendMessage', requestOpts); // pastebin_com/u0J1Cph3 //'sendMessage?'.http_build_query(opts, '');
|
2477
2499
|
try {
|
2478
2500
|
const responseJson = JSON.parse(responseText);
|
2479
2501
|
if (responseJson.ok){
|
2480
|
-
return
|
2481
|
-
}
|
2482
|
-
|
2483
|
-
|
2484
|
-
|
2485
|
-
if(
|
2486
|
-
|
2487
|
-
|
2488
|
-
|
2489
|
-
|
2490
|
-
|
2491
|
-
return response;
|
2502
|
+
return responseJson;
|
2503
|
+
}
|
2504
|
+
// for some reason, if still unsupported format submitted, resubmit the plain format
|
2505
|
+
//i.e. {"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: Unsupported start tag \"br/\" at byte offset 43"}
|
2506
|
+
else {
|
2507
|
+
if( responseJson.description.indexOf ('Bad Request: can\'t parse entities')> -1 ){
|
2508
|
+
if ( ! is_repeated_call ){
|
2509
|
+
const extraOpts2 = this.objectCopy(opts);
|
2510
|
+
text = "[SecondSend with stipped tags] \r\n" + this.stripTags(text) ;
|
2511
|
+
extraOpts2['is_repeated_call'] = true;
|
2512
|
+
return this.telegram_message(text, chat_id, bot_key, extraOpts2);
|
2492
2513
|
}
|
2493
|
-
} else {
|
2494
|
-
return response;
|
2495
2514
|
}
|
2515
|
+
return responseJson;
|
2496
2516
|
}
|
2497
2517
|
} catch (ex) {
|
2498
|
-
return {'ok': false, 'description': responseText };
|
2518
|
+
return {'ok': false, 'description': ex.message + ':::' + responseText };
|
2519
|
+
}
|
2520
|
+
},
|
2521
|
+
|
2522
|
+
telegram_interval_ms: 50, // telegram seems to accept around 30 times per second, so we'd better wait around that milliseconds
|
2523
|
+
telegram_last_sent_time: 0,
|
2524
|
+
|
2525
|
+
async telegramMessageCached(text, chat_id, bot_key, extra_opts={}){
|
2526
|
+
const curMS = this.milliseconds();
|
2527
|
+
const goneMS = curMS - this.telegram_last_sent_time;
|
2528
|
+
if ( goneMS < this.telegram_interval_ms ){
|
2529
|
+
await this.sleep (this.telegram_interval_ms - goneMS);
|
2530
|
+
}
|
2531
|
+
this.telegram_last_sent_time = curMS;
|
2532
|
+
const cacheId = this.cache.file.idForContent( text +'_'+ chat_id +'_'+ bot_key +'_'+ JSON.stringify(extra_opts) );
|
2533
|
+
if (this.cache.file.addIdIfNotExists('function_telegram_message', cacheId) ){
|
2534
|
+
return this.telegramMessage(text, chat_id, bot_key, extra_opts);
|
2535
|
+
}
|
2536
|
+
else {
|
2537
|
+
return false;
|
2499
2538
|
}
|
2539
|
+
//if(is_callable([$this,'notifications_db_entry']))
|
2540
|
+
// $this->notifications_db_entry($key, $array['chat_id'], $this->stringify($res), time(), $ok );
|
2541
|
+
//return $res;
|
2500
2542
|
},
|
2501
2543
|
|
2502
2544
|
openUrlInBrowser(url)
|
@@ -2657,26 +2699,27 @@ const puvox_library =
|
|
2657
2699
|
// } catch (ex) { reject (ex); }
|
2658
2700
|
// });
|
2659
2701
|
},
|
2660
|
-
|
2661
|
-
|
2662
2702
|
// if(setHashInAddress) { window.location.hash = id_or_Name; }
|
2663
2703
|
|
2664
2704
|
|
2665
2705
|
|
2666
2706
|
|
2667
2707
|
// ######## CACHE ITEMS (client-side JS) ########
|
2668
|
-
|
2669
|
-
setAppName (name){ this.
|
2670
|
-
|
2671
|
-
|
2672
|
-
|
2673
|
-
|
2674
|
-
|
2708
|
+
_privateAppName : null, //override with anything you want
|
2709
|
+
setAppName (name){ this._privateAppName = name; },
|
2710
|
+
getAppName(){
|
2711
|
+
if (!this._privateAppName || this._privateAppName === ''){
|
2712
|
+
throw new Error ('Before you start using caching functions, please at first define your appplication\'s name(identifier) at first with .setAppName("whatever_my_app_name"), so it will get its own cache-storage');
|
2713
|
+
}
|
2714
|
+
return this._privateAppName;
|
2715
|
+
},
|
2675
2716
|
|
2717
|
+
cache: {
|
2718
|
+
localStorage : {
|
2676
2719
|
storage: typeof window !== 'undefined' ? window.localStorage : null,
|
2677
2720
|
|
2678
2721
|
get(name, defaultValue, expireSeconds = 0){
|
2679
|
-
let appName =
|
2722
|
+
let appName = puvox_library.getAppName();
|
2680
2723
|
let val = this.storage.getItem(appName + '_' + name);
|
2681
2724
|
let expireVal = this.storage.getItem(appName + '_createtime_' + name);
|
2682
2725
|
if (val === null) {
|
@@ -2696,15 +2739,17 @@ const puvox_library =
|
|
2696
2739
|
},
|
2697
2740
|
set(name, value){
|
2698
2741
|
try{
|
2699
|
-
|
2700
|
-
this.storage.setItem(
|
2742
|
+
const appName = puvox_library.getAppName();
|
2743
|
+
this.storage.setItem(appName + '_' +name, value);
|
2744
|
+
this.storage.setItem(appName + '_createtime_' +name, (new Date()).getTime());
|
2701
2745
|
return true;
|
2702
2746
|
}
|
2703
2747
|
catch(ex){ alert("Cache storage quote exceeded. can't save value. err598"); return false; }
|
2704
2748
|
},
|
2705
|
-
remove(name
|
2706
|
-
|
2707
|
-
this.storage.removeItem(
|
2749
|
+
remove(name){
|
2750
|
+
const appName = puvox_library.getAppName();
|
2751
|
+
this.storage.removeItem(appName + '_' + name);
|
2752
|
+
this.storage.removeItem(appName + '_createtime_' + name);
|
2708
2753
|
},
|
2709
2754
|
getItem(name, subItemName, defaultValue){
|
2710
2755
|
let val = this.get(name, '{}');
|
@@ -2725,30 +2770,27 @@ const puvox_library =
|
|
2725
2770
|
return this.set(name, JSON.stringify(parsed) );
|
2726
2771
|
}
|
2727
2772
|
},
|
2728
|
-
file
|
2773
|
+
file: {
|
2729
2774
|
// ########## CACHE DIRS (server-side JS) ##########
|
2730
|
-
parent(){ return puvox_library; },
|
2731
|
-
AppName(){ return puvox_library.AppName; },
|
2732
|
-
|
2733
2775
|
customCacheDir:null,
|
2734
|
-
|
2776
|
+
dir(){
|
2735
2777
|
if (!this.customCacheDir){
|
2736
|
-
this.customCacheDir =
|
2778
|
+
this.customCacheDir = puvox_library.file.getTempDir() + '/';
|
2737
2779
|
}
|
2738
|
-
let finaldir = this.customCacheDir + '
|
2780
|
+
let finaldir = this.customCacheDir + '_cache_' + puvox_library.getAppName() + '/';
|
2739
2781
|
return finaldir;
|
2740
2782
|
},
|
2741
2783
|
filePath(uniqFileName){
|
2742
|
-
const parent =
|
2784
|
+
const parent = puvox_library;
|
2743
2785
|
uniqFileName = parent.isString(uniqFileName) || parent.isNumeric(uniqFileName) ? uniqFileName : JSON.stringify(uniqFileName);
|
2744
2786
|
uniqFileName = parent.sanitize_key_dashed(parent.getCharsFromStart(uniqFileName, 15)) + "_"+ parent.md5(uniqFileName);
|
2745
|
-
filePath= this.
|
2787
|
+
filePath= this.dir() + uniqFileName + "_tmp"; //"/".
|
2746
2788
|
return filePath;
|
2747
2789
|
},
|
2748
2790
|
//
|
2749
2791
|
get(uniqFileName, defaultContent ='', expire_seconds=8640000, decode = true)
|
2750
2792
|
{
|
2751
|
-
const parent =
|
2793
|
+
const parent = puvox_library;
|
2752
2794
|
let filePath = this.filePath(uniqFileName);
|
2753
2795
|
if ( filePath.length < 3) return "too tiny filename";
|
2754
2796
|
|
@@ -2783,7 +2825,7 @@ const puvox_library =
|
|
2783
2825
|
},
|
2784
2826
|
set(uniqFileName, content)
|
2785
2827
|
{
|
2786
|
-
const parent =
|
2828
|
+
const parent = puvox_library;
|
2787
2829
|
let filePath= this.filePath(uniqFileName);
|
2788
2830
|
let contentFinal = parent.isString(content) ? content : ((parent.isArray(content) || parent.isObject(content)) ? JSON.stringify(content) : content);
|
2789
2831
|
return parent.file.write(filePath, contentFinal);
|
@@ -2809,6 +2851,37 @@ const puvox_library =
|
|
2809
2851
|
// console.log("writeFileAppendJson", e);
|
2810
2852
|
// }
|
2811
2853
|
// },
|
2854
|
+
containerDefaultPrefix: "_cached_ids_",
|
2855
|
+
tempIds:{},
|
2856
|
+
idForContent(slugOrContent){
|
2857
|
+
return puvox_library.md5(puvox_library.isSimpleVariableType(slugOrContent) ? slugOrContent : JSON.stringify(slugOrContent));
|
2858
|
+
},
|
2859
|
+
existsId(containerSlug, id){
|
2860
|
+
return (id in this.getIds(containerSlug));
|
2861
|
+
},
|
2862
|
+
getIds(containerSlug) {
|
2863
|
+
if (! (containerSlug in this.tempIds)) {
|
2864
|
+
const content = puvox_library.file.read(this.dir() + this.containerDefaultPrefix + containerSlug, '{}');
|
2865
|
+
this.tempIds[containerSlug] = JSON.parse(content);
|
2866
|
+
}
|
2867
|
+
return this.tempIds[containerSlug];
|
2868
|
+
},
|
2869
|
+
setIds(containerSlug, idsDict) {
|
2870
|
+
this.tempIds[containerSlug] = idsDict;
|
2871
|
+
return puvox_library.file.write(this.dir() + this.containerDefaultPrefix + containerSlug, JSON.stringify(this.tempIds[containerSlug]));
|
2872
|
+
},
|
2873
|
+
addId(containerSlug, id){
|
2874
|
+
const ids = this.getIds(containerSlug);
|
2875
|
+
ids[id] = 1;
|
2876
|
+
this.setIds(containerSlug, ids);
|
2877
|
+
},
|
2878
|
+
addIdIfNotExists(containerSlug, id){
|
2879
|
+
if (! this.existsId(containerSlug, id)){
|
2880
|
+
this.addId(containerSlug, id);
|
2881
|
+
return true;
|
2882
|
+
}
|
2883
|
+
return false;
|
2884
|
+
},
|
2812
2885
|
}
|
2813
2886
|
},
|
2814
2887
|
|
@@ -2823,7 +2896,7 @@ const puvox_library =
|
|
2823
2896
|
return this._required_instances[name];
|
2824
2897
|
}
|
2825
2898
|
},
|
2826
|
-
file
|
2899
|
+
file: {
|
2827
2900
|
parent() {return puvox_library;},
|
2828
2901
|
fs() {return puvox_library.modules('fs');},
|
2829
2902
|
os() {return puvox_library.modules('os');},
|
package/package.json
CHANGED
package/test.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
const h = require('./library_standard.js');
|
2
|
+
function c(...args){console.log(...args);} function cc(o){console.dir(o, {'maxArrayLength': null});} function x(...args){c(...args);process.exit();} function xx(o){cc(o);process.exit();}
|
3
|
+
|
4
|
+
h.setAppName("myApp");
|
5
|
+
async function x(){
|
6
|
+
c(await h.telegramMessageCached('Hello World', 1668904096, '1651207488:AAF0KDLW6tmDUElN7RxVbp5RwQK5u77znhU'));
|
7
|
+
}
|
8
|
+
x();
|