message_bird.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Channel::Driver::Sms::MessageBird < Channel::Driver::Sms::Base
  3. NAME = 'sms/message_bird'.freeze
  4. def fetchable?(_channel)
  5. false
  6. end
  7. def deliver(options, attr, _notification = false)
  8. Rails.logger.info "Sending SMS to recipient #{attr[:recipient]}"
  9. return true if Setting.get('import_mode')
  10. Rails.logger.info "Backend sending MessageBird SMS to #{attr[:recipient]}"
  11. begin
  12. send_create(options, attr)
  13. true
  14. rescue => e
  15. Rails.logger.error { "MessageBird error: #{e.inspect}" }
  16. raise e
  17. end
  18. end
  19. def send_create(options, attr)
  20. return if Setting.get('developer_mode')
  21. api(options).message_create(options[:sender], attr[:recipient], attr[:message])
  22. end
  23. def process(_options, attr, channel)
  24. Rails.logger.info "Receiving SMS frim recipient #{attr['originator']}"
  25. # prevent already created articles
  26. if attr['message_id'].present? && Ticket::Article.exists?(message_id: attr['message_id'])
  27. return [:json, {}]
  28. end
  29. # find sender
  30. user = user_by_mobile(attr['originator'])
  31. UserInfo.current_user_id = user.id
  32. process_ticket(attr, channel, user)
  33. [:json, {}]
  34. end
  35. def create_ticket(attr, channel, user)
  36. title = cut_title(attr['incomingMessage'])
  37. ticket = Ticket.new(
  38. group_id: channel.group_id,
  39. title: title,
  40. state_id: Ticket::State.find_by(default_create: true).id,
  41. priority_id: Ticket::Priority.find_by(default_create: true).id,
  42. customer_id: user.id,
  43. preferences: {
  44. channel_id: channel.id,
  45. sms: {
  46. originator: attr['originator'],
  47. recipient: attr['recipient'],
  48. }
  49. }
  50. )
  51. ticket.save!
  52. ticket
  53. end
  54. def create_article(attr, channel, ticket)
  55. Ticket::Article.create!(
  56. ticket_id: ticket.id,
  57. type: article_type_sms,
  58. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  59. body: attr['incomingMessage'],
  60. from: attr['originator'],
  61. to: attr['recipient'],
  62. message_id: attr['message_id'],
  63. content_type: 'text/plain',
  64. preferences: {
  65. channel_id: channel.id,
  66. sms: {
  67. reference: attr['reference'],
  68. }
  69. }
  70. )
  71. end
  72. def self.definition
  73. {
  74. name: 'MessageBird',
  75. adapter: 'sms/message_bird',
  76. account: [
  77. { name: 'options::webhook_token', display: __('Webhook Token'), tag: 'input', type: 'text', limit: 200, null: false, default: Digest::MD5.hexdigest(SecureRandom.uuid), disabled: true, readonly: true },
  78. { name: 'options::token', display: __('Token'), tag: 'input', type: 'text', limit: 255, null: false },
  79. { name: 'options::sender', display: __('Sender'), tag: 'input', type: 'text', limit: 200, null: false, placeholder: '+491710000000' },
  80. { name: 'group_id', display: __('Destination Group'), tag: 'tree_select', null: false, relation: 'Group', nulloption: true, filter: { active: true } },
  81. ],
  82. notification: [
  83. { name: 'options::token', display: __('Token'), tag: 'input', type: 'text', limit: 255, null: false },
  84. { name: 'options::sender', display: __('Sender'), tag: 'input', type: 'text', limit: 200, null: false, placeholder: '+491710000000' },
  85. ],
  86. }
  87. end
  88. private
  89. def api(options)
  90. require 'messagebird'
  91. @api ||= ::MessageBird::Client.new(options[:token])
  92. end
  93. end