const fs = require('fs'); const Markup = require('telegraf/markup'); const Extra = require('telegraf/extra'); //=== MAIN MODULE CONFIG async function wait(min) { return new Promise(resolve => { setTimeout(resolve, min*60000); }); } const tmr = { atrium:{ desc:{ 0:"Atrium Laundry", 1:"Lessive Atrium" }, sub:{ wash:{ desc:{ 0:"Washing Machine", 1:"Lave Linge" }, tmr: [45,45,45], tmr_str: ["A","B","C"], }, dry:{ desc:{ 0:"Dryer", 1:"Sèche Linge" }, tmr: [45,45,45,45], tmr_str: ["+++","++","+","-"] } } }, basic:{ desc:{ 0:"Basic Timers", 1:"Timer Basique", }, sub:{ min:{ desc:{ 0:"Minutes", 1:"Minutes" }, tmr: [1,5,10,15,30,45], tmr_str: [1,5,10,15,30,45], }, hour:{ desc:{ 0:"Hours", 1:"Heures" }, tmr: [1*60,2*60,4*60,6*60,12*60], tmr_str: [1,2,4,6,12], } } } } const cbt = { run:"r", sel:"s", } function gen_kb(bot, p_en, parr,lang){ let bck_nxt = ""; let bck = ""; let kb = [[]]; let ntmr = tmr; for(let k of Object.keys(ntmr)){ if(p_en.indexOf(k.toString()) == -1){ ntmr[k]=false; continue; } } for(let pp of parr){ try{ if(ntmr[pp].sub){ bck = bck_nxt; bck_nxt = pp.toString(); ntmr = ntmr[pp].sub; }else{ bck = bck_nxt; ntmr = ntmr[pp]; } }catch(err){ return []; } } if(ntmr){ for(let k of Object.keys(ntmr)){ let ctmr = ntmr[k]; if(ctmr.sub){ let str = ctmr.desc[lang]; k[0].push(bot.mkcb(str,cbt.sel,k)); }else if(ctmr.tmr){ let str = ctmr.desc[lang]; let c = parr.join("_") + "_" + k.toString(); kb[0].push(bot.mkcb(str,cbt.sel,c)); } } if(ntmr.tmr){ for(let i in ntmr.tmr){ let v = ntmr.tmr[i]; let str = ntmr.tmr_str[i]; kb[0].push(bot.mkcb(str,cbt.run,v)) } } } if(parr.length > 0) kb.push([bot.mkcb("Back",cbt.sel,bck)]); return kb; } module.exports = function(arr){ this.arr = arr; return { name : "Timer", key : "tmr", version : 0.1, requirements : { gps: false, }, text : [ { trigger : ["/timer"], public : true, desc : {0:"Lists Timer Options", 1:"Liste les options de timer", }, requirements : (bot,event,message)=>{ return new Promise((resolve, reject)=>{ if (message.from.id != message.chat.id) return reject(410); return resolve(200); }); }, action : (bot,event,message)=>{ var kb = gen_kb(bot,this.arr,[],event.master.lang); return event.reply("Select An Option", {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); } }, ], reply : [], regex : [], media : [], callback : [ { trigger : 's', requirements : (bot,event,data)=>{ return new Promise((resolve, reject)=>{ return resolve(200); }); }, action : (bot,event,data)=>{ let arr = []; if(data != "") arr = data.split("_"); let kb = gen_kb(bot,this.arr,arr,event.master.lang); return event.editMessageText("Select An Option", {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); } }, { trigger : 'r', requirements : (bot,event,data)=>{ return new Promise((resolve, reject)=>{ return resolve(200); }); }, action : (bot,event,data)=>{ return event.editMessageText("Running timer of "+parseInt(data)+" minutes !", {"parse_mode":"Markdown"}); wait(parseInt(data)).then((res)=>{ return event.reply("Times Up !"); }).catch(err=>{ bot.error(err); }) } }, ], inline : [], new_member : (bot,event)=>{}, weburl : [], cron : [] } }