microsoft_teams.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Webhook::PreDefined::MicrosoftTeams < Webhook::PreDefined
  3. def name
  4. __('Microsoft Teams Notifications')
  5. end
  6. # rubocop:disable Lint/InterpolationCheck
  7. def custom_payload
  8. {
  9. type: 'message',
  10. attachments: [
  11. {
  12. contentType: 'application/vnd.microsoft.card.adaptive',
  13. contentUrl: nil,
  14. content: {
  15. '$schema': 'http://adaptivecards.io/schemas/adaptive-card.json',
  16. type: 'AdaptiveCard',
  17. version: '1.0',
  18. body: [
  19. {
  20. type: 'TextBlock',
  21. text: '#{ticket.title}',
  22. color: '#{ticket.current_state_color}',
  23. weight: 'bolder',
  24. size: 'large',
  25. wrap: true
  26. },
  27. {
  28. type: 'TextBlock',
  29. text: '#{notification.changes}',
  30. wrap: true
  31. },
  32. {
  33. type: 'TextBlock',
  34. text: '#{notification.body}',
  35. wrap: true
  36. },
  37. {
  38. type: 'ActionSet',
  39. actions: [
  40. {
  41. type: 'Action.OpenUrl',
  42. title: '#{config.ticket_hook}#{ticket.number}',
  43. url: '#{notification.link}'
  44. }
  45. ]
  46. }
  47. ]
  48. }
  49. }
  50. ]
  51. }
  52. end
  53. # rubocop:enable Lint/InterpolationCheck
  54. def post_replace(hash, tracks)
  55. hash['attachments'].first['content']['body'].first['color'] = state_color(tracks[:ticket])
  56. hash
  57. end
  58. private
  59. def state_color(ticket)
  60. return 'attention' if ticket.escalation_at && ticket.escalation_at < Time.zone.now
  61. case ticket.state.state_type.name
  62. when 'new', 'open'
  63. return 'warning'
  64. when 'closed'
  65. return 'good'
  66. when 'pending reminder'
  67. return 'warning' if ticket.pending_time && ticket.pending_time < Time.zone.now
  68. end
  69. 'default'
  70. end
  71. end