notification.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. class String
  2. def message_quote
  3. quote = self.split("\n")
  4. body_quote = ''
  5. quote.each do |line|
  6. body_quote = body_quote + '> ' + line + "\n"
  7. end
  8. body_quote
  9. end
  10. def word_wrap(*args)
  11. options = args.extract_options!
  12. unless args.blank?
  13. options[:line_width] = args[0] || 80
  14. end
  15. options.reverse_merge!(:line_width => 80)
  16. lines = self
  17. lines.split("\n").collect do |line|
  18. line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
  19. end * "\n"
  20. end
  21. end
  22. class Ticket::Observer::Notification < ActiveRecord::Observer
  23. observe :ticket, 'ticket::_article'
  24. @@event_buffer = []
  25. def self.transaction
  26. # puts '@@event_buffer'
  27. # puts @@event_buffer.inspect
  28. @@event_buffer.each { |event|
  29. if event[:name] == 'Ticket' && event[:type] == 'create'
  30. ticket = Ticket.find( event[:id] )
  31. # send new ticket notification to agents
  32. puts 'send new ticket notify to agent'
  33. send_notify(
  34. {
  35. :event => event,
  36. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  37. :subject => 'New Ticket (#{ticket.title})',
  38. :body => 'New Ticket (#{ticket.title}) in Group #{ticket.group.name}
  39. From: #{ticket.articles[-1].from}
  40. <snip>
  41. #{ticket.articles[-1].body}
  42. </snip>
  43. #{config.http_type}://#{config.fqdn}/ticket/zoom/#{ticket.id}/#{ticket.articles[-1].id}
  44. '
  45. },
  46. ticket,
  47. nil
  48. )
  49. # send new ticket notification to customers
  50. puts 'send new ticket notify to customer'
  51. send_notify(
  52. {
  53. :event => event,
  54. :recipient => 'customer', # group|owner|to_work_on|customer
  55. :subject => 'New Ticket has been created! (#{ticket.title})',
  56. :body => 'Thanks for your email. A new ticket has been created.
  57. You wrote:
  58. <snip>
  59. #{ticket.articles[-1].body}
  60. </snip>
  61. Your email will be answered by a human ASAP
  62. Have fun with Zammad! :-)
  63. Your Zammad Team
  64. '
  65. },
  66. ticket,
  67. nil
  68. )
  69. end
  70. # send follow up notification
  71. if event[:name] == 'Ticket::Article' && event[:type] == 'create'
  72. article = Ticket::Article.find( event[:id] )
  73. ticket = article.ticket
  74. # only send article notifications after init article is created (handled by ticket create event)
  75. next if ticket.articles.count >= 1
  76. puts 'send new ticket::article notify'
  77. if article.ticket_article_sender.name == 'Customer'
  78. send_notify(
  79. {
  80. :event => event,
  81. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  82. :subject => 'Follow Up (#{ticket.title})',
  83. :body => 'Follow Up (#{ticket.title}) in Group #{ticket.group.name}
  84. From: #{ticket.articles[-1].from}
  85. <snip>
  86. #{ticket.articles[-1].body}
  87. </snip>
  88. #{config.http_type}://#{config.fqdn}/ticket/zoom/#{ticket.id}/#{ticket.articles[-1].id}
  89. '
  90. },
  91. ticket,
  92. article
  93. )
  94. end
  95. # send new note notification to owner
  96. # if agent == created.id
  97. if article.ticket_article_sender.name == 'Agent' && article.created_by_id != article.ticket.owner_id
  98. send_notify(
  99. {
  100. :event => event,
  101. :recipient => 'owner', # group|owner|to_work_on
  102. :subject => 'Update (#{ticket.title})',
  103. :body => 'Update (#{ticket.title}) in Group #{ticket.group.name}
  104. From: #{ticket.articles[-1].from}
  105. <snip>
  106. #{ticket.articles[-1].body}
  107. </snip>
  108. #{config.http_type}://#{config.fqdn}/ticket/zoom/#{ticket.id}/#{ticket.articles[-1].id}
  109. '
  110. },
  111. ticket,
  112. article
  113. )
  114. end
  115. end
  116. }
  117. # reset buffer
  118. @@event_buffer = []
  119. end
  120. def self.send_notify(data, ticket, article)
  121. # find recipients
  122. recipients = []
  123. # group of agents to work on
  124. if data[:recipient] == 'group'
  125. recipients = ticket.agent_of_group()
  126. # owner
  127. elsif data[:recipient] == 'owner'
  128. if ticket.owner_id != 1
  129. recipients.push ticket.owner
  130. end
  131. # customer
  132. elsif data[:recipient] == 'customer'
  133. if ticket.customer_id != 1
  134. # temporarily disabled
  135. # recipients.push ticket.customer
  136. end
  137. # owner or group of agents to work on
  138. elsif data[:recipient] == 'to_work_on'
  139. if ticket.owner_id != 1
  140. recipients.push ticket.owner
  141. else
  142. recipients = ticket.agent_of_group()
  143. end
  144. end
  145. # prepare subject & body
  146. [:subject, :body].each { |key|
  147. data[key.to_sym].gsub!( /\#\{(.+?)\}/ ) { |s|
  148. # use quoted text
  149. callback = $1
  150. callback.gsub!( /.body$/ ) { |item|
  151. item = item + '.word_wrap( :line_width => 80 ).message_quote.chomp'
  152. }
  153. # use config params
  154. callback.gsub!( /^config.(.+?)$/ ) { |item|
  155. name = $1
  156. item = "Setting.get('#{$1}')"
  157. }
  158. # replace value
  159. s = eval callback
  160. }
  161. }
  162. # rebuild subject
  163. data[:subject] = ticket.subject_build( data[:subject] )
  164. # send notifications
  165. sender = Setting.get('notification_sender')
  166. recipients.each do |user|
  167. next if !user.email || user.email == ''
  168. a = Channel::IMAP.new
  169. subject = data[:subject]
  170. body = data[:body]
  171. message = a.send(
  172. {
  173. # :in_reply_to => self.in_reply_to,
  174. :from => sender,
  175. :to => user.email,
  176. :subject => subject,
  177. :body => body,
  178. },
  179. true
  180. )
  181. end
  182. end
  183. def after_create(record)
  184. # puts 'CREATED!!!!'
  185. # puts record.inspect
  186. e = {
  187. :name => record.class.name,
  188. :type => 'create',
  189. :data => record,
  190. :id => record.id,
  191. }
  192. @@event_buffer.push e
  193. end
  194. def before_update(record)
  195. puts 'before_update'
  196. current = record.class.find(record.id)
  197. # do not send anything if nothing has changed
  198. return if current.attributes == record.attributes
  199. # puts 'UPDATE!!!!!!!!'
  200. # puts 'current'
  201. # puts current.inspect
  202. # puts 'record'
  203. # puts record.inspect
  204. e = {
  205. :name => record.class.name,
  206. :type => 'update',
  207. :data => record,
  208. :id => record.id,
  209. }
  210. @@event_buffer.push e
  211. end
  212. def after_update(record)
  213. # puts 'after_update'
  214. # puts record.inspect
  215. # puts '-----'
  216. # puts @a.inspect
  217. # AuditTrail.new(record, "UPDATED")
  218. end
  219. end