2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2021-08-16 14:52:36 +00:00
|
|
|
class Channel::Driver::Sms::Twilio < Channel::Driver::Sms::Base
|
2018-10-16 08:45:15 +00:00
|
|
|
NAME = 'sms/twilio'.freeze
|
|
|
|
|
|
|
|
def fetchable?(_channel)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def send(options, attr, _notification = false)
|
|
|
|
Rails.logger.info "Sending SMS to recipient #{attr[:recipient]}"
|
|
|
|
|
|
|
|
return true if Setting.get('import_mode')
|
|
|
|
|
|
|
|
Rails.logger.info "Backend sending Twilio SMS to #{attr[:recipient]}"
|
|
|
|
begin
|
2021-08-16 14:52:36 +00:00
|
|
|
send_create(options, attr)
|
2018-10-16 08:45:15 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue => e
|
2021-07-09 11:24:41 +00:00
|
|
|
Rails.logger.debug { "Twilio error: #{e.inspect}" }
|
2018-10-16 08:45:15 +00:00
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-16 14:52:36 +00:00
|
|
|
def send_create(options, attr)
|
|
|
|
return if Setting.get('developer_mode')
|
|
|
|
|
|
|
|
result = api(options).messages.create(
|
|
|
|
from: options[:sender],
|
|
|
|
to: attr[:recipient],
|
|
|
|
body: attr[:message],
|
|
|
|
)
|
|
|
|
|
|
|
|
raise result.error_message if result&.error_code&.positive?
|
|
|
|
end
|
|
|
|
|
2018-10-16 08:45:15 +00:00
|
|
|
def process(_options, attr, channel)
|
|
|
|
Rails.logger.info "Receiving SMS frim recipient #{attr[:From]}"
|
|
|
|
|
|
|
|
# prevent already created articles
|
2020-08-03 08:35:43 +00:00
|
|
|
if Ticket::Article.exists?(message_id: attr[:SmsMessageSid])
|
2021-07-12 08:09:02 +00:00
|
|
|
require 'twilio-ruby' # Only load this gem when it is really used
|
2018-10-16 08:45:15 +00:00
|
|
|
return ['application/xml; charset=UTF-8;', Twilio::TwiML::MessagingResponse.new.to_s]
|
|
|
|
end
|
|
|
|
|
2021-08-16 14:52:36 +00:00
|
|
|
user = user_by_mobile(attr[:From])
|
2018-10-16 08:45:15 +00:00
|
|
|
UserInfo.current_user_id = user.id
|
|
|
|
|
2021-08-16 14:52:36 +00:00
|
|
|
process_ticket(attr, channel, user)
|
|
|
|
|
|
|
|
require 'twilio-ruby' # Only load this gem when it is really used
|
|
|
|
['application/xml; charset=UTF-8;', Twilio::TwiML::MessagingResponse.new.to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_ticket(attr, channel, user)
|
|
|
|
title = cut_title(attr[:Body])
|
|
|
|
ticket = Ticket.new(
|
|
|
|
group_id: channel.group_id,
|
|
|
|
title: title,
|
|
|
|
state_id: Ticket::State.find_by(default_create: true).id,
|
|
|
|
priority_id: Ticket::Priority.find_by(default_create: true).id,
|
|
|
|
customer_id: user.id,
|
|
|
|
preferences: {
|
|
|
|
channel_id: channel.id,
|
|
|
|
sms: {
|
|
|
|
AccountSid: attr['AccountSid'],
|
|
|
|
From: attr['From'],
|
|
|
|
To: attr['To'],
|
2018-10-16 08:45:15 +00:00
|
|
|
}
|
2021-08-16 14:52:36 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
ticket.save!
|
|
|
|
ticket
|
|
|
|
end
|
2018-10-16 08:45:15 +00:00
|
|
|
|
2021-08-16 14:52:36 +00:00
|
|
|
def create_article(attr, channel, ticket)
|
2018-10-16 08:45:15 +00:00
|
|
|
Ticket::Article.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket.id,
|
|
|
|
type: article_type_sms,
|
|
|
|
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
|
|
|
|
body: attr[:Body],
|
|
|
|
from: attr[:From],
|
|
|
|
to: attr[:To],
|
|
|
|
message_id: attr[:SmsMessageSid],
|
2018-10-16 08:45:15 +00:00
|
|
|
content_type: 'text/plain',
|
2018-12-19 17:31:51 +00:00
|
|
|
preferences: {
|
2018-10-16 08:45:15 +00:00
|
|
|
channel_id: channel.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
sms: {
|
2018-10-16 08:45:15 +00:00
|
|
|
AccountSid: attr['AccountSid'],
|
2018-12-19 17:31:51 +00:00
|
|
|
From: attr['From'],
|
|
|
|
To: attr['To'],
|
2018-10-16 08:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.definition
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'twilio',
|
|
|
|
adapter: 'sms/twilio',
|
|
|
|
account: [
|
2021-09-20 10:47:05 +00:00
|
|
|
{ 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 },
|
2018-10-16 08:45:15 +00:00
|
|
|
{ name: 'options::account_id', display: 'Account SID', tag: 'input', type: 'text', limit: 200, null: false, placeholder: 'XXXXXX' },
|
|
|
|
{ name: 'options::token', display: 'Token', tag: 'input', type: 'text', limit: 200, null: false },
|
|
|
|
{ name: 'options::sender', display: 'Sender', tag: 'input', type: 'text', limit: 200, null: false, placeholder: '+491710000000' },
|
|
|
|
{ name: 'group_id', display: 'Destination Group', tag: 'select', null: false, relation: 'Group', nulloption: true, filter: { active: true } },
|
|
|
|
],
|
|
|
|
notification: [
|
|
|
|
{ name: 'options::account_id', display: 'Account SID', tag: 'input', type: 'text', limit: 200, null: false, placeholder: 'XXXXXX' },
|
|
|
|
{ name: 'options::token', display: 'Token', tag: 'input', type: 'text', limit: 200, null: false },
|
|
|
|
{ name: 'options::sender', display: 'Sender', tag: 'input', type: 'text', limit: 200, null: false, placeholder: '+491710000000' },
|
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def api(options)
|
2021-07-05 13:38:40 +00:00
|
|
|
require 'twilio-ruby' # Only load this gem when it is really used.
|
2018-10-16 08:45:15 +00:00
|
|
|
@api ||= ::Twilio::REST::Client.new options[:account_id], options[:token]
|
|
|
|
end
|
|
|
|
end
|