slack.rb 1.9 KB

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