A Slack bot that lets you access the `figlet` banner generation tool via a slash command
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

figbot.rb 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'figlet'
  2. require 'sinatra'
  3. require 'dotenv/load'
  4. def fig(input)
  5. opts = parse(input)
  6. banners = []
  7. font = Figlet::Font.new(File.expand_path('./lib/banner.flf'))
  8. figlet = Figlet::Typesetter.new(font)
  9. chunks = opts[:message].chars.to_a.each_slice(5).to_a.map{|chunk| chunk.join}
  10. chunks.each do |chunk|
  11. banner = figlet[chunk.strip].gsub(/#/, opts[:ink]).gsub(/ /, opts[:space])
  12. unless uses_emoji(opts)
  13. banner = "```#{banner}```"
  14. end
  15. banners << banner
  16. end
  17. banners.join("\n")
  18. end
  19. def parse(input)
  20. opts = {
  21. :message => String.new,
  22. :space => " ",
  23. :ink => "#",
  24. }
  25. position = 0
  26. encountered_options = false
  27. while position < input.length
  28. if m = input.slice(position..-1).match(/^space=(\S+)/)
  29. encountered_options = true
  30. opts[:space] = m[1].strip
  31. position += m[0].length
  32. elsif m = input.slice(position..-1).match(/^ink=(\S+)/)
  33. encountered_options = true
  34. opts[:ink] = m[1].strip
  35. position += m[0].length
  36. elsif m = input.slice(position..-1).match(/^font=(\S+)/)
  37. encountered_options = true
  38. opts[:font] = m[1].strip
  39. position += m[0].length
  40. else
  41. if not encountered_options
  42. opts[:message] << input[position]
  43. end
  44. position += 1
  45. end
  46. end
  47. opts[:message] = opts[:message].strip
  48. opts
  49. end
  50. def uses_emoji(opts)
  51. (opts[:space][0] == ':' and opts[:space][-1] == ':') or (opts[:ink][0] == ':' and opts[:ink][-1] == ':')
  52. end
  53. post '/' do
  54. content_type :json
  55. {
  56. response_type: 'in_channel',
  57. text: fig(params[:text])
  58. }.to_json
  59. end