slack.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. class NotificationFactory::Slack
  2. =begin
  3. result = NotificationFactory::Slack.template(
  4. template: 'ticket_update',
  5. locale: 'en-us',
  6. timezone: 'Europe/Berlin',
  7. objects: {
  8. recipient: User.find(2),
  9. ticket: Ticket.find(1)
  10. },
  11. )
  12. returns
  13. {
  14. subject: 'some subject',
  15. body: 'some body',
  16. }
  17. =end
  18. def self.template(data)
  19. if data[:templateInline]
  20. return NotificationFactory::Renderer.new(
  21. objects: data[:objects],
  22. locale: data[:locale],
  23. timezone: data[:timezone],
  24. template: data[:templateInline]
  25. ).render
  26. end
  27. template = NotificationFactory.template_read(
  28. locale: data[:locale] || Locale.default,
  29. template: data[:template],
  30. format: 'md',
  31. type: 'slack',
  32. )
  33. message_subject = NotificationFactory::Renderer.new(
  34. objects: data[:objects],
  35. locale: data[:locale],
  36. timezone: data[:timezone],
  37. template: template[:subject],
  38. escape: false
  39. ).render
  40. message_body = NotificationFactory::Renderer.new(
  41. objects: data[:objects],
  42. locale: data[:locale],
  43. timezone: data[:timezone],
  44. template: template[:body],
  45. escape: false
  46. ).render
  47. if !data[:raw]
  48. application_template = NotificationFactory.application_template_read(
  49. format: 'md',
  50. type: 'slack',
  51. )
  52. data[:objects][:message] = message_body
  53. data[:objects][:standalone] = data[:standalone]
  54. message_body = NotificationFactory::Renderer.new(
  55. objects: data[:objects],
  56. locale: data[:locale],
  57. timezone: data[:timezone],
  58. template: application_template,
  59. escape: false
  60. ).render
  61. end
  62. {
  63. subject: message_subject.strip!,
  64. body: message_body.strip!,
  65. }
  66. end
  67. end