This commit is contained in:
Cedric Hoelzl 2020-07-01 11:00:51 +02:00
parent d56e63b5e8
commit c45d28ee74
3 changed files with 182 additions and 3 deletions

View File

@ -1,3 +0,0 @@
# telebot-17track
Using 17track.net to provide package status

130
config.js Normal file
View File

@ -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 : []
}
}

52
db.js Normal file
View File

@ -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;
};