const fs = require('fs'); const fetch = require('node-fetch'); const Markup = require('telegraf/markup'); const Extra = require('telegraf/extra'); const db_js = require('./db.js'); var db = new db_js("17track"); //=== TOOL FUNCTIONS const track_url = 'https://t.17track.net/restapi/track'; var cnvrt = function(v) { var str = ''; for (var i = 0; i < v.length; i++) { str += v.charCodeAt(i).toString(16); } return str; }; function acc(a, b) { var c = 1315423911 ^ b << 16; for (var i = a.length - 1; i >= 0; i--) { c ^= (c << 5) + a.charCodeAt(i) + (c >> 2); } return Math.abs(c & 2147483647); }; function atb(a) { var b = a; while (b['length'] < 0x8) { b = '0' + b; } return b; }; var token = function(data) { var classes = ["navbar navbar-default yq-header", "nav navbar-toolbar navbar-right navbar-toolbar-right yq-navbar-toolbar", "yq-dropdown-menu-media-apps", "yq-navbar-menuIcon-24 yq-head-apps", "dropdown-menu yq-moreapp-container", "list-group yq-list-group-moreapp clearfix", "hide yq-dropdown-menu-media-msg", "yq-head-msg yq-navbar-menuIcon-24", "yq-msg-nomsg vertical-align panel text-center hide", "yq-msg-network-err vertical-align panel text-center hide", "dropdown-menu dropdown-menu-media yq-dropdown-menu-media-user", "dropdown-menu-footer yq-user-footer clearfix", "margin-horizontal-10 yq-input-container", "dropdown-menu dropdown-menu-media yq-track-dropdown", "yq-more-track-container clearfix", "yq-tools-small", "navbar-brand-logo hidden-xs yq-default-logo", "navbar-brand-logo visible-xs yq-default-logo", "tab-content yq-panel-tracklist scrollable is-enabled scrollable-vertical", "yq-panel-gad", "modal-dialog yq-modal-dialog"]; var rnd = Math.round((Math.random()* classes.length)); var res = []; var value = "yq-"; var key = "Last-Event-ID"; if (classes.length > rnd) { value = classes[rnd]; } value += '/' + Date.now().toString(16) + '/18c/true'; res[0]= cnvrt(value.split('').reverse().join('')); res[1] = rnd.toString(16); res[2] = rnd.toString(16).length; res[3] = 2; res[4] = atb(acc(value, rnd).toString(16)); res[5] = atb(acc(data,data.length).toString(16)); return res.join('') }; async function get_tracking(trck_ids){ var data = { "data": [],"guid":"","timeZoneOffset":-120 }; for(var id of trck_ids){ data.data.push({num:id,fc:0,sc:0}); } var keyid = token(JSON.stringify(data)); const opts = { method: 'POST', body: JSON.stringify(data), headers: { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'en-US,en;q=0.5', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'https://t.17track.net/en', 'Cookie': 'v5_TranslateLang=en; v5_AppIconsVersion=1; Last-Event-ID='+keyid, 'Cache-Control': 'no-cache', }, // agent: new http.Agent({ // keepAlive: true // }) }; return fetch(track_url, opts) .then(res => res.json()) .then(res => { var results = []; for(var rr of res.dat){ if(rr.track) results.push({id:rr.no, date:rr.track.z0.a, loc:rr.track.z0.c, status:rr.track.z0.z}); else results.push({id:rr.no, date:'', loc:'', status:"Unknown"}); } return results; }); } //=== MAIN MODULE CONFIG module.exports = function(){ this.key = "17trk"; return { name : "17Track", //Unique Module name key : this.key, //Unique Key for this module, if multiple instances add a number/text to it version : 0.1, //Version Number, Currently unused requirements : { gps: false, //Currently un-used (all module have access to GPS) }, text : [ { trigger : ["/track"], //Array of Triggers public : true, //Should it be shown to user (user can still run the command without knowing it exists) desc : {0:"Tracking with options", 1:"*insert french*", 2:"*insert japanese*", 3:"*insert arabic*", }, requirements : (bot,event,message)=>{ return new Promise((resolve, reject)=>{ return resolve(200); }); }, action : (bot,event,message)=>{ let kb = []; kb.push([bot.mkcb("Refresh","refresh","")]) ; kb.push([bot.mkcb_close("Close"),bot.mkcb("Delete","del","")]); var new_ids = []; if(message.text_arg && message.text_arg.length >0){ new_ids = message.text_args.replace('.','').replace(',','\n').replace(' ','\n').split('\n'); } return db.get_v(event.chat.id).then(res =>{ var ids = res.concat(new_ids); if(ids.length == 0){ return event.reply("*No Tracking Yet*", {"parse_mode":"Markdown"}); }else{ return db.set_v(event.chat.id,ids).then(res=>{ return get_tracking(res).then(res=>{ var text = "*Tracking*\n\n"; for(var rr of res){ text+=`*${rr.id}* (_${rr.date?rr.date:'Unknown'} - ${rr.loc?rr.loc:'Unknown'}_)\n${rr.status}\n\n`; } return event.reply( text, {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); }); }); } }); } }, ], reply : [], regex : [], media : [], callback : [ { trigger : 'refresh', requirements : (bot,event,data)=>{ return new Promise((resolve, reject)=>{ return resolve(200); }); }, action : (bot,event,data)=>{ let kb = []; kb.push([bot.mkcb("Refresh","refresh","")]) ; kb.push([bot.mkcb_close("Close"),bot.mkcb("Delete","del","")]); return db.get_v(event.chat.id).then(res =>{ var ids = res; if(ids.length == 0){ return event.editMessageText("*No Tracking Yet*", {"parse_mode":"Markdown"}); }else{ return db.set_v(event.chat.id,ids).then(res=>{ return get_tracking(res).then(res=>{ var text = `*Tracking* (Updated: ${new Date().toLocaleString()})\n\n`; for(var rr of res){ text+=`*${rr.id}* (_${rr.date?rr.date:'Unknown'} - ${rr.loc?rr.loc:'Unknown'}_)\n${rr.status}\n\n`; } return event.editMessageText( text, {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); }); }); } }); } }, { trigger : 'del', requirements : (bot,event,data)=>{ return new Promise((resolve, reject)=>{ return resolve(200); }); }, action : (bot,event,data)=>{ return db.get_v(event.chat.id).then(res =>{ if(res && res != []){ if(data){ for( var i = 0; i < res.length; i++){ if ( res[i] == data) { res.splice(i, 1); } } return db.set_v(event.chat.id,res).then(res=>{ return event.editMessageText("*Tracking Deleted*", {"parse_mode":"Markdown"}); }); }else{ let kb = []; for(var rr of res){ kb.push([bot.mkcb(rr,"del",rr)]) } kb.push([bot.mkcb_close("Close")]); return event.editMessageText( "Select Tracking Number To Delete" , {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); } }else{ return event.editMessageText("*No Tracking To Delete*", {"parse_mode":"Markdown"}); } }); } } ], inline : [], new_member : (bot,event)=>{}, weburl : [], cron : [] } }