diff --git a/scripts/doge.coffee b/scripts/doge.coffee index f620e22..0075338 100644 --- a/scripts/doge.coffee +++ b/scripts/doge.coffee @@ -1,6 +1,6 @@ # Description: # Doge meme generator -# Requires hubot-tg adapter to access message history +# Requires hubot-matrix adapter to access history # # Configuration: # Uses hubot-tg enviroment variables @@ -13,55 +13,55 @@ # -net = require 'net' - module.exports = (robot) -> - match_all = (regex, str) -> - matches = [] - str.replace regex, -> - arr = [].slice.call arguments, 0 - extras = arr.splice -2 - arr.index = extras[0] - arr.input = extras[1] - matches.push arr - matches + matrix = robot.adapter.client + return unless robot.adapterName is 'matrix' and matrix? + # get the messages history for a chat room + get_history = (room, size, callback) -> + room = matrix.getRoom room.id + matrix.scrollback room, size, (err, res) -> + return callback err, null if err? + return unless res.chunk? + + callback null, res.chunk.map (event) -> + type: event.content.msgtype + sender: robot.brain.userForId event.sender + content: event.content.body + + # concat list of lists concat = (list) -> list.reduce (x, y) -> x.concat y + # remove duplicates in a list + nub = (list) -> Array.from new Set list + + # URL escape strings escape = (str) -> str.replace(/\d|\?|#/g, '') # not supported .replace( /\ /g, '%20') # spaces + # format doge API URL doge_url = (text) -> "http://dogr.io/#{escape text}.png?split=false" - shiba = (res, words) -> + # dogeify text + doge = (res, words) -> prefix = [ 'much', 'such', 'many', 'very'] word = words.filter (x) -> /^[a-zA-ZàèéìòùÀÈÉÌÒÙ\-_!&@#?]{4,}$/.test x (res.random prefix) + ' ' + (res.random words) - run_command = (command, callback) -> - client = net.connect robot.adapter.port, robot.adapter.host, -> - client.write command+'\n' - client.setEncoding 'utf8' - client.on 'data', (reply) -> - if callback? - callback (reply.split '\n')[1..-3] - client.end() - - get_history = (chat, size, callback) -> - regex = /\[(.+)\] (.+) [>«»]+ ((?:(?!\n(\[|\d))[\s\S])+)/g - parse_line = (x) -> - date: x[1] - peer: x[2].replace(chat, '').trim() - text: x[3].trim().replace '\n', ' ' - run_command "history #{chat} #{size}", (lines) -> - callback ((match_all regex, lines.join '\n').map parse_line) - robot.respond /doge(?: (\d+))?/, (res) -> - n = res.match[1] || 20 - get_history res.message.room, n, (history) -> - words = concat (message.text.split ' ' for message in history) - sample = (res.random ['wow', shiba res, words] for _ in [0..8]) - res.send (doge_url sample.join '/') \ No newline at end of file + n = res.match[1] || 10 + get_history res.message.room, n, (err, history) -> + return res.send "c'è stato un errore: #{err}" if err? + + words = ((nub concat (history.filter (msg) -> + msg.type is 'm.text').map (msg) -> + msg.content.split /\W/).sort (a, b) -> + b.length - a.length).slice 0, 50 + + sample = [0..15].map -> + if Math.random() <= 0.2 then 'wow' + else doge res, words + res.send doge_url sample.join '/'