From ed62a5911779f75267a8812d68297e2cc26ad361 Mon Sep 17 00:00:00 2001 From: Cedric Hoelzl Date: Thu, 2 Jul 2020 09:17:34 +0200 Subject: [PATCH] fix --- config.js | 119 ++++++++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/config.js b/config.js index 9079421..ff533d8 100644 --- a/config.js +++ b/config.js @@ -122,69 +122,64 @@ module.exports = function(){ }, action : (bot,event,message)=>{ let kb = []; - kb.push([bot.mkcb("Add","add",""), - bot.mkcb("Delete","del","")]); - kb.push([bot.mkcb_close("Close")]); - + 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 =>{ - if(res && res != []){ - return get_tracking(res).then(res=>{ - var text = "*Tracking*\n\n"; - for(var rr of res){ - text+=`*${rr.id}* (_${rr.date} - ${rr.loc}_)\n${rr.status}\n\n`; - } - return event.reply( text, {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); - }); + var ids = res.concat(new_ids); + if(ids.length == 0){ + return event.reply("*No Tracking Yet*", {"parse_mode":"Markdown"}); }else{ - return event.reply("*No Tracking Yet*", {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); + 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 : [ - { - 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.text.replace('.','').replace(',','').split('\n'); - return db.get_v(event.chat.id).then(res =>{ - if(res && res != []){ - return db.set_v(event.chat.id,res.concat(ids)).then(res=>{ - return event.reply("Added ids to Tracking.", {"parse_mode":"Markdown"}) - }); - }else{ - return db.set_v(event.chat.id, ids).then(res=>{ - console.log(res) - return event.reply("Added ids to Tracking.", {"parse_mode":"Markdown"}); - }); - } - - }) - } - } - ], + reply : [], regex : [], media : [], callback : [ { - trigger : 'add', + trigger : 'refresh', 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."); + 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"}); + }); + }); + } + + }); } }, { @@ -195,27 +190,29 @@ module.exports = function(){ }); }, action : (bot,event,data)=>{ - if(data){ - return db.get_v(event.chat.id).then(res=>{ - for( var i = 0; i < res.length; i++){ - if ( res[i] == data) { - res.splice(i, 1); }} - db.set_v(event.chat.id,res); - }); - }else{ - return db.get_v(event.chat.id).then(res =>{ - if(res && res != []){ + 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.reply( (res+"") , {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); - }else{ - return event.reply("*No Tracking To Delete*", {"parse_mode":"Markdown"}) + 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"}); + } + }); } } ],