/src/scripts/spin.coffee

https://github.com/russelldavis/hubot-scripts · CoffeeScript · 110 lines · 78 code · 11 blank · 21 comment · 11 complexity · a51fcd2bea9517c5291b36d8bc2e762f MD5 · raw file

  1. # Spin & spun text formatter script
  2. #
  3. # Script can be used to generate different combinations of the input text by variating synonyms for example.
  4. #
  5. # 1. supports nested sets
  6. # 2. supports multiline input
  7. #
  8. # spin me <query> - Returns a spun version of the input.
  9. # spun me <query> - Returns a spintax version of the input using API.
  10. # spun and spin me <query> - Returns a spun version of the input using spintax API first.
  11. # spin the last spun - Returns a spun version of the input generated by the last spun me call.
  12. #
  13. # Example: spin me This is {my nested {spintax|spuntext} formatted string|your nested {spintax|spuntext} formatted string} test.
  14. #
  15. # TODO: 1. support different syntaxes like '[spin]', '~', '[/spin]' instead of default '{', '|', '}'
  16. # 2. reserved words not to spin
  17. #
  18. crypto = require 'crypto'
  19. env = process.env
  20. # spin vars
  21. split_str = '|'
  22. replace_regexp_str = /[{}]/gi
  23. match_regexp_str = /{[^{}]+?}/gi
  24. spin = (txt) ->
  25. if txt
  26. txt = capture(txt) while txt.match(match_regexp_str)
  27. txt
  28. capture = (txt) ->
  29. match = txt.match(match_regexp_str)
  30. if (match[0])
  31. words = match[0].split(split_str)
  32. replace = words[Math.floor(Math.random()*words.length)].replace(replace_regexp_str, '')
  33. txt = txt.replace(match[0], replace)
  34. txt
  35. # spun vars
  36. secret_key = env.HUBOT_SPINNER_SECRET_KEY
  37. access_key = env.HUBOT_SPINNER_ACCESS_KEY
  38. host = 'www.articlemanager.us'
  39. path = '/api/spinner.php'
  40. version = '2011-06-24'
  41. scheme = 'http'
  42. gmdatenow = () ->
  43. g = new Date()
  44. m = g.getMonth() + 1
  45. d = g.getDate()
  46. y = g.getFullYear()
  47. h = g.getHours()+2
  48. i = g.getMinutes()
  49. s = g.getSeconds()
  50. "#{y}-#{m}-#{d} #{h}:#{i}:#{s}"
  51. get_signature = (host, path, secret_key, access_key, timestamp, version) ->
  52. newline = "\n"
  53. request = "timestamp=#{escape(timestamp)}&version=#{escape(version)}&access_key=#{escape(access_key)}"
  54. data = "GET" + newline + host + newline + path + newline + request
  55. hmac = crypto.createHmac('sha256', secret_key)
  56. hmac.update(data)
  57. hmac.digest('base64')
  58. spun = (msg, spinner = null, robot_brain = null) ->
  59. timestamp = gmdatenow()
  60. signature = get_signature(host, path, secret_key, access_key, timestamp, version)
  61. msg.http(scheme + "://" + host + path)
  62. .query
  63. timestamp: timestamp
  64. version: version
  65. access_key: access_key
  66. signature: signature
  67. type: 'json'
  68. .post(msg.match[1]) (err, res, body) ->
  69. if err
  70. msg.send err
  71. else if res.statusCode is not 200
  72. msg.send "Service not available! Status code: " + res.statusCode
  73. else
  74. body = JSON.parse(body)
  75. if body.response is 'error'
  76. msg.send body.message
  77. else
  78. if spinner
  79. msg.send spinner body.message
  80. else
  81. # set to the memory so user can spin it with separate "spin the last spun" command
  82. if robot_brain
  83. if not robot_brain.data.spin
  84. robot_brain.data.spin = []
  85. robot_brain.data.spin[msg.message.user.reply_to] = body.message
  86. msg.send body.message
  87. module.exports = (robot) ->
  88. robot.hear /^spin me ([\s\S]*)/i, (msg) ->
  89. msg.send spin msg.match[1]
  90. robot.hear /^spun me ([\s\S]*)/i, (msg) ->
  91. spun(msg, null, robot.brain)
  92. robot.hear /^spun and spin me ([\s\S]*)/i, (msg) ->
  93. spun(msg, spin)
  94. robot.hear /^spin the last spun$/i, (msg) ->
  95. # get input from memory
  96. if @robot.brain.data.spin[msg.message.user.reply_to]
  97. msg.send spin robot.brain.data.spin[msg.message.user.reply_to]
  98. else
  99. msg.send "Nothing so spin. Please use spun first."