Compare commits

...

1 Commits

Author SHA1 Message Date
rnhmjoj
146a15b7d3 update doge script for matrix 2017-02-26 17:28:15 +01:00

View File

@ -1,6 +1,6 @@
# Description: # Description:
# Doge meme generator # Doge meme generator
# Requires hubot-tg adapter to access message history # Requires hubot-matrix adapter to access history
# #
# Configuration: # Configuration:
# Uses hubot-tg enviroment variables # Uses hubot-tg enviroment variables
@ -13,55 +13,55 @@
# #
net = require 'net'
module.exports = (robot) -> module.exports = (robot) ->
match_all = (regex, str) -> matrix = robot.adapter.client
matches = [] return unless robot.adapterName is 'matrix' and matrix?
str.replace regex, ->
arr = [].slice.call arguments, 0
extras = arr.splice -2
arr.index = extras[0]
arr.input = extras[1]
matches.push arr
matches
# 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 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) -> escape = (str) ->
str.replace(/\d|\?|#/g, '') # not supported str.replace(/\d|\?|#/g, '') # not supported
.replace( /\ /g, '%20') # spaces .replace( /\ /g, '%20') # spaces
# format doge API URL
doge_url = (text) -> doge_url = (text) ->
"http://dogr.io/#{escape text}.png?split=false" "http://dogr.io/#{escape text}.png?split=false"
shiba = (res, words) -> # dogeify text
doge = (res, words) ->
prefix = [ 'much', 'such', 'many', 'very'] prefix = [ 'much', 'such', 'many', 'very']
word = words.filter (x) -> word = words.filter (x) ->
/^[a-zA-ZàèéìòùÀÈÉÌÒÙ\-_!&@#?]{4,}$/.test x /^[a-zA-ZàèéìòùÀÈÉÌÒÙ\-_!&@#?]{4,}$/.test x
(res.random prefix) + ' ' + (res.random words) (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) -> robot.respond /doge(?: (\d+))?/, (res) ->
n = res.match[1] || 20 n = res.match[1] || 10
get_history res.message.room, n, (history) -> get_history res.message.room, n, (err, history) ->
words = concat (message.text.split ' ' for message in history) return res.send "c'è stato un errore: #{err}" if err?
sample = (res.random ['wow', shiba res, words] for _ in [0..8])
res.send (doge_url sample.join '/') 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 '/'