A Slack Bot that pulls Pixiv information and posts the full image(s) into Slack, with iOS shortcuts.
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.

83 lines
1.7 KiB

  1. module SlackMathbot
  2. class App
  3. cattr_accessor :hooks
  4. include SlackMathbot::Hooks::Hello
  5. include SlackMathbot::Hooks::Message
  6. def initialize
  7. SlackMathbot.configure do |config|
  8. config.token = ENV['SLACK_API_TOKEN'] || fail("Missing ENV['SLACK_API_TOKEN'].")
  9. end
  10. Slack.configure do |config|
  11. config.token = SlackMathbot.config.token
  12. end
  13. end
  14. def config
  15. SlackMathbot.config
  16. end
  17. def self.instance
  18. @instance ||= SlackMathbot::App.new
  19. end
  20. def run
  21. auth!
  22. start!
  23. end
  24. def stop!
  25. client.stop
  26. end
  27. private
  28. def logger
  29. @logger ||= begin
  30. $stdout.sync = true
  31. Logger.new(STDOUT)
  32. end
  33. end
  34. def start!
  35. loop do
  36. client.start
  37. @client = nil
  38. end
  39. end
  40. def client
  41. @client ||= begin
  42. client = Slack.realtime
  43. hooks.each do |hook|
  44. client.on hook do |data|
  45. begin
  46. send hook, data
  47. rescue StandardError => e
  48. logger.error e
  49. begin
  50. Slack.chat_postMessage(channel: data['channel'], text: e.message) if data.key?('channel')
  51. rescue
  52. # ignore
  53. end
  54. end
  55. end
  56. end
  57. client
  58. end
  59. end
  60. def auth!
  61. auth = Slack.auth_test
  62. SlackMathbot.configure do |config|
  63. config.url = auth['url']
  64. config.team = auth['team']
  65. config.user = auth['user']
  66. config.team_id = auth['team_id']
  67. config.user_id = auth['user_id']
  68. end
  69. logger.info "Welcome '#{SlackMathbot.config.user}' to the '#{SlackMathbot.config.team}' team at #{SlackMathbot.config.url}."
  70. end
  71. end
  72. end