From 9d388996e9ec703b34fc61eea9a4c789833d3fbd Mon Sep 17 00:00:00 2001 From: poka Date: Fri, 7 Aug 2020 17:49:09 +0200 Subject: [PATCH] =?UTF-8?q?Hello=20Bo=E1=B8=A0=20on=20Telegram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/debug.js | 8 +++ scripts/example.coffee | 106 --------------------------------- scripts/g1-info.js | 53 +++++++++++++++++ scripts/gbanque.js | 43 ++++++++++++++ scripts/reload.coffee | 129 +++++++++++++++++++++++++++++++++++++++++ scripts/shell | 1 + 6 files changed, 234 insertions(+), 106 deletions(-) create mode 100644 scripts/debug.js delete mode 100644 scripts/example.coffee create mode 100644 scripts/g1-info.js create mode 100644 scripts/gbanque.js create mode 100644 scripts/reload.coffee create mode 120000 scripts/shell diff --git a/scripts/debug.js b/scripts/debug.js new file mode 100644 index 0000000..c1af690 --- /dev/null +++ b/scripts/debug.js @@ -0,0 +1,8 @@ +const util = require('util'); + +module.exports = (robot) => { + robot.hear(/debug user/i, function (msg) { + username = msg.envelope + msg.reply("```" + util.inspect(username) + "```") + }); +} diff --git a/scripts/example.coffee b/scripts/example.coffee deleted file mode 100644 index 7c9839c..0000000 --- a/scripts/example.coffee +++ /dev/null @@ -1,106 +0,0 @@ -# Description: -# Example scripts for you to examine and try out. -# -# Notes: -# They are commented out by default, because most of them are pretty silly and -# wouldn't be useful and amusing enough for day to day huboting. -# Uncomment the ones you want to try and experiment with. -# -# These are from the scripting documentation: https://github.com/github/hubot/blob/master/docs/scripting.md - -module.exports = (robot) -> - - # robot.hear /badger/i, (res) -> - # res.send "Badgers? BADGERS? WE DON'T NEED NO STINKIN BADGERS" - # - # robot.respond /open the (.*) doors/i, (res) -> - # doorType = res.match[1] - # if doorType is "pod bay" - # res.reply "I'm afraid I can't let you do that." - # else - # res.reply "Opening #{doorType} doors" - # - # robot.hear /I like pie/i, (res) -> - # res.emote "makes a freshly baked pie" - # - # lulz = ['lol', 'rofl', 'lmao'] - # - # robot.respond /lulz/i, (res) -> - # res.send res.random lulz - # - # robot.topic (res) -> - # res.send "#{res.message.text}? That's a Paddlin'" - # - # - # enterReplies = ['Hi', 'Target Acquired', 'Firing', 'Hello friend.', 'Gotcha', 'I see you'] - # leaveReplies = ['Are you still there?', 'Target lost', 'Searching'] - # - # robot.enter (res) -> - # res.send res.random enterReplies - # robot.leave (res) -> - # res.send res.random leaveReplies - # - # answer = process.env.HUBOT_ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING - # - # robot.respond /what is the answer to the ultimate question of life/, (res) -> - # unless answer? - # res.send "Missing HUBOT_ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING in environment: please set and try again" - # return - # res.send "#{answer}, but what is the question?" - # - # robot.respond /you are a little slow/, (res) -> - # setTimeout () -> - # res.send "Who you calling 'slow'?" - # , 60 * 1000 - # - # annoyIntervalId = null - # - # robot.respond /annoy me/, (res) -> - # if annoyIntervalId - # res.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH" - # return - # - # res.send "Hey, want to hear the most annoying sound in the world?" - # annoyIntervalId = setInterval () -> - # res.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH" - # , 1000 - # - # robot.respond /unannoy me/, (res) -> - # if annoyIntervalId - # res.send "GUYS, GUYS, GUYS!" - # clearInterval(annoyIntervalId) - # annoyIntervalId = null - # else - # res.send "Not annoying you right now, am I?" - # - # - # robot.router.post '/hubot/chatsecrets/:room', (req, res) -> - # room = req.params.room - # data = JSON.parse req.body.payload - # secret = data.secret - # - # robot.messageRoom room, "I have a secret: #{secret}" - # - # res.send 'OK' - # - # robot.error (err, res) -> - # robot.logger.error "DOES NOT COMPUTE" - # - # if res? - # res.reply "DOES NOT COMPUTE" - # - # robot.respond /have a soda/i, (res) -> - # # Get number of sodas had (coerced to a number). - # sodasHad = robot.brain.get('totalSodas') * 1 or 0 - # - # if sodasHad > 4 - # res.reply "I'm too fizzy.." - # - # else - # res.reply 'Sure!' - # - # robot.brain.set 'totalSodas', sodasHad+1 - # - # robot.respond /sleep it off/i, (res) -> - # robot.brain.set 'totalSodas', 0 - # res.reply 'zzzzz' diff --git a/scripts/g1-info.js b/scripts/g1-info.js new file mode 100644 index 0000000..11a2140 --- /dev/null +++ b/scripts/g1-info.js @@ -0,0 +1,53 @@ +const exec = require('child_process').exec; + +module.exports = (robot) => { + robot.hear(/^rib|G1LibreBot rib|^pubkey|G1LibreBot pubkey/i, function (msg) { + username = msg.envelope.user.username + cmd = msg.envelope.message.text + process.env.hb_cmd = cmd + process.env.hb_username = username + + exec("scripts/shell/g1-infos.sh pubkey $hb_username $hb_cmd", function(err, stdout, stderr) { + if (err) { + msg.send("Une erreur est survenu ...") + } else { + msg.send(stdout) + } + }); + }); + + robot.hear(/^g1 membres|G1LibreBot g1 membres|^g1 membre|G1LibreBot g1 membre/i, function (msg) { + exec("scripts/shell/g1-infos.sh membres", function(err, stdout, stderr) { + if (err) { + msg.send("Une erreur est survenu ...") + } else { + msg.send(stdout) + } + }); + }); + + robot.hear(/^.\/silkaj|G1LibreBot .\/silkaj/i, function (msg) { + cmd = msg.envelope.message.text + process.env.hb_cmd = cmd + + exec("scripts/shell/g1-infos.sh silkaj $hb_cmd", function(err, stdout, stderr) { + if (err) { + msg.send("Une erreur est survenu ...") + } else { + msg.send(stdout) + } + }); + }); + +} + + +/* EXAMPLES +module.exports = (robot) => { + robot.respond(/hello/i, function (msg) { + msg.reply('Howdy!'); + }); + robot.hear(/howdy/i, function (msg) { + msg.send('Hola!'); + }); +}*/ diff --git a/scripts/gbanque.js b/scripts/gbanque.js new file mode 100644 index 0000000..a932117 --- /dev/null +++ b/scripts/gbanque.js @@ -0,0 +1,43 @@ +const exec = require('child_process').exec + +module.exports = (robot) => { + robot.hear(/^gb|G1LibreBot gb|^.\/gbanque|G1LibreBot .\/gbanque/i, function (msg) { + username = msg.envelope.user.username + process.env.hb_username = username + cmd = msg.envelope.message.text + process.env.hb_cmd = cmd + room = msg.envelope.user.telegram_chat.title + process.env.hb_room = room + + msg.send("room:" + room) + exec("scripts/shell/gbanque.sh ~~~$hb_cmd~~~$hb_username~~~$hb_room", function(err, stdout, stderr) { + if (err) { + msg.send("Une erreur est survenu ..." + stderr) + } else { + msg.send(stdout) + } + }); + }); +} + +/* bbot style +bot.global.text({ + starts: ['./gbanque', 'biz ./gbanque', 'biz gb', 'gb'] +}, (b) => { + username = b.message.user.name + process.env.hb_username = username + room = b.message.user.room.name + process.env.hb_room = room + cmd = b.message.text + process.env.hb_cmd = cmd + + gbanque = exec("src/shell/gbanque.sh ~~~$hb_cmd~~~$hb_username~~~$hb_room", function(err, stdout, stderr) { + if (err) { + b.respond("Une erreur est survenu: " + stderr) + } else { + b.respond(stdout) + } + }); + +} +*/ diff --git a/scripts/reload.coffee b/scripts/reload.coffee new file mode 100644 index 0000000..f6d4e42 --- /dev/null +++ b/scripts/reload.coffee @@ -0,0 +1,129 @@ +# Description: +# Allows Hubot to (re)load scripts without restart +# +# Commands: +# hubot reload - Reloads scripts without restart. Loads new scripts too. (a fork version that works perfectly) +# +# Author: +# spajus +# vinta +# m-seldin + +Fs = require 'fs' +Path = require 'path' + +oldCommands = null +oldListeners = null + +module.exports = (robot) -> + + robot.respond /reload/i, id:'reload-scripts.reload', (msg) -> + try + oldCommands = robot.commands + oldListeners = robot.listeners + + robot.commands = [] + robot.listeners = [] + + reloadAllScripts msg, success, (err) -> + msg.send err + catch error + console.log "Hubot reloader:", error + msg.send "Could not reload all scripts: #{error}" + + success = (msg) -> + # Cleanup old listeners and help + for listener in oldListeners + listener = {} + oldListeners = null + oldCommands = null + msg.send "Reloaded all scripts" + + + walkSync = (dir, filelist) -> + #walk through given directory and collect files + files = Fs.readdirSync(dir) + filelist = filelist || [] + for file in files + fullPath = Path.join(dir,file) + robot.logger.debug "Scanning file : #{fullPath}" + + if (Fs.statSync(fullPath).isDirectory()) + filelist = walkSync(fullPath, filelist) + else + #add full path file to returning collection + filelist.push(fullPath) + return filelist + + # ref: https://github.com/srobroek/hubot/blob/e543dff46fba9e435a352e6debe5cf210e40f860/src/robot.coffee + deleteScriptCache = (scriptsBaseDir) -> + if Fs.existsSync(scriptsBaseDir) + fileList = walkSync scriptsBaseDir + + for file in fileList.sort() + robot.logger.debug "file: #{file}" + if require.cache[require.resolve(file)] + try + cacheobj = require.resolve(file) + console.log "Invalidate require cache for #{cacheobj}" + delete require.cache[cacheobj] + catch error + console.log "Unable to invalidate #{cacheobj}: #{error.stack}" + robot.logger.debug "Finished deleting script cache!" + + reloadAllScripts = (msg, success, error) -> + robot = msg.robot + robot.emit('reload_scripts') + + robot.logger.debug "Deleting script cache..." + + scriptsPath = Path.resolve ".", "scripts" + deleteScriptCache scriptsPath + robot.load scriptsPath + + scriptsPath = Path.resolve ".", "src", "scripts" + deleteScriptCache scriptsPath + robot.load scriptsPath + + robot.logger.debug "Loading hubot scripts..." + + hubotScripts = Path.resolve ".", "hubot-scripts.json" + Fs.exists hubotScripts, (exists) -> + if exists + Fs.readFile hubotScripts, (err, data) -> + if data.length > 0 + try + scripts = JSON.parse data + scriptsPath = Path.resolve "node_modules", "hubot-scripts", "src", "scripts" + robot.loadHubotScripts scriptsPath, scripts + catch err + error "Error parsing JSON data from hubot-scripts.json: #{err}" + return + + robot.logger.debug "Loading hubot external scripts..." + + robot.logger.debug "Deleting cache for apppulsemobile" + deleteScriptCache Path.resolve ".","node_modules","hubot-apppulsemobile","src" + + externalScripts = Path.resolve ".", "external-scripts.json" + Fs.exists externalScripts, (exists) -> + if exists + Fs.readFile externalScripts, (err, data) -> + if data.length > 0 + try + robot.logger.debug "DATA : #{data}" + scripts = JSON.parse data + + if scripts instanceof Array + for pkg in scripts + scriptPath = Path.resolve ".","node_modules",pkg,"src" + robot.logger.debug "Deleting cache for #{pkg}" + robot.logger.debug "Path : #{scripts}" + deleteScriptCache scriptPath + catch err + error "Error parsing JSON data from external-scripts.json: #{err}" + robot.loadExternalScripts scripts + return + robot.logger.debug "step 5" + + success(msg) diff --git a/scripts/shell b/scripts/shell new file mode 120000 index 0000000..75c4e8e --- /dev/null +++ b/scripts/shell @@ -0,0 +1 @@ +../../bog/src/shell/ \ No newline at end of file