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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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('./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. get '/' do
  54. 'FigBot'
  55. end
  56. post '/' do
  57. content_type :json
  58. {
  59. response_type: 'in_channel',
  60. text: fig(params[:text])
  61. }.to_json
  62. end