From c45d28ee740e053cb5700085a8a3ff11dd97d68d Mon Sep 17 00:00:00 2001 From: Cedric Hoelzl Date: Wed, 1 Jul 2020 11:00:51 +0200 Subject: [PATCH] kekek --- README.md | 3 -- config.js | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ db.js | 52 ++++++++++++++++++++++ 3 files changed, 182 insertions(+), 3 deletions(-) delete mode 100644 README.md create mode 100644 config.js create mode 100644 db.js diff --git a/README.md b/README.md deleted file mode 100644 index b51f7fa..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# telebot-17track - -Using 17track.net to provide package status \ No newline at end of file diff --git a/config.js b/config.js new file mode 100644 index 0000000..92a1231 --- /dev/null +++ b/config.js @@ -0,0 +1,130 @@ +const fs = require('fs'); + +const Markup = require('telegraf/markup'); +const Extra = require('telegraf/extra'); + +const db_js = require('./db.js'); + +var db = new db_js("17track"); + +//=== TOOL FUNCTIONS + +/* Here you usualy write util functions that are used multiple times in the code (or a second file is recommended)*/ + +//=== 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("Add","add",""), + bot.mkcb("Delete","del","")]); + kb.push([bot.mkcb_close("Close")]); + + return db.get_v(event.chat.id).then(res =>{ + console.log(res); + return event.reply( (res+"") , {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); + }).catch(err => { + return event.reply("*No Tracking Yet*", {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}) + }) + + + + } + }, + ], + reply : [ + { + requirements : (bot,event,message)=>{ + return new Promise((resolve,reject)=>{ + if(!message.text || message.reply_to_message.text != "Reply to this message with a tracking id") + return reject(400); + return resolve(200); + }); + }, + action : (bot,event,message)=>{ + var ids = message.replace('.','').split('\n'); + return db.get_v(event.chat.id).then(res =>{ + return db.set_v(event.chat.id,res.concat(ids)).then(res=>{ + return event.reply("Added ids to Tracking.", {"parse_mode":"Markdown"}) + }); + }).catch(err => { + return db.set_v(event.chat.id, ids).then(res=>{ + return event.reply("Added ids to Tracking.", {"parse_mode":"Markdown"}); + }); + + }) + } + } + ], + regex : [], + media : [], + callback : [ + { + trigger : 'add', + requirements : (bot,event,data)=>{ + return new Promise((resolve, reject)=>{ + return resolve(200); + }); + }, + action : (bot,event,data)=>{ + return event.reply("Reply to this message with a tracking id"); + } + }, + { + trigger : 'del', + requirements : (bot,event,data)=>{ + return new Promise((resolve, reject)=>{ + return resolve(200); + }); + }, + action : (bot,event,data)=>{ + console.log(data); + if(data){ + return db.get_v(event.chat.id).then(res=>{ + db.set_v(event.chat.id,res.remove(data)); + }); + }else{ + return db.get_v(event.chat.id).then(res =>{ + + let kb = []; + for(var rr in res){ + kb.push([bot.mkcb(rr,"del",rr)]) + } + kb.push([bot.mkcb_close("Close")]); + return event.reply( (res+"") , {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); + }).catch(err => { + return event.reply("*No Tracking To Delete*", {"parse_mode":"Markdown"}) + }) + } + } + } + ], + inline : [], + new_member : (bot,event)=>{}, + weburl : [], + cron : [] +} + +} \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..ceb7a73 --- /dev/null +++ b/db.js @@ -0,0 +1,52 @@ +const low = require('lowdb'); +const FileSync = require('lowdb/adapters/FileSync'); + +var db = null; + +function db_init(db){ + db.defaults({ chats:[]}).write(); +} + +var dbc = function(name){ + const db_file = "./db/"+name+".json"; + var db = low(new FileSync(db_file)); + db_init(db); + return db; +}; + + + +module.exports = function(name){ + this.name = name; + this.db = dbc(name); + + this.get_chat = function(obj){ + + return new Promise((resolve, reject)=>{ + let res = this.db.get("chats").find({ id: obj.chat_id }).value(); + if(res) return resolve(res); + + this.db.get("chats").push({ id:obj.chat_id, lang: 0, geo:{lon:0,lat:0}}).write(); + res = this.db.get("chats").find({ id: obj.chat_id }).value(); + if(res) return resolve(res); + + return reject({error:true, msg:"Error With DB..."}); + }); + }; + + this.dump_db = function(){ + return new Promise((resolve, reject)=>{ + resolve(this.db.get("chats").value()); + }); + }; + + this.set_v = function(cid,v){ + return this.db.find({id:cid}).set('tracking',v).write(); + }; + + this.get_v = function(cid){ + return this.db.find({id:cid}).get('tracking').value(); + } + + return this; +};