diff --git a/Gemfile b/Gemfile index 957784f55..9f4d3cf40 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem 'jbuilder' gem 'kaminari' gem 'responders' gem 'rest-client' +gem 'telephone_number' gem 'time_diff' gem 'tzinfo-data' gem 'valid_email2' diff --git a/Gemfile.lock b/Gemfile.lock index bf9a33cfc..577676cf5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -448,6 +448,7 @@ GEM faraday inflecto virtus + telephone_number (1.4.6) thor (0.20.3) thread_safe (0.3.6) time_diff (0.3.0) @@ -560,6 +561,7 @@ DEPENDENCIES spring spring-watcher-listen telegram-bot-ruby + telephone_number time_diff twilio-ruby (~> 5.32.0) twitty! diff --git a/app/builders/contact_builder.rb b/app/builders/contact_builder.rb index 70b994ac2..9bb3ef3ac 100644 --- a/app/builders/contact_builder.rb +++ b/app/builders/contact_builder.rb @@ -21,13 +21,14 @@ class ContactBuilder phone_number: contact_attributes[:phone_number], email: contact_attributes[:email], identifier: contact_attributes[:identifier], - additional_attributes: contact_attributes[:identifier] + additional_attributes: contact_attributes[:additional_attributes] ) contact_inbox = ::ContactInbox.create!( contact_id: contact.id, inbox_id: inbox.id, source_id: source_id ) + ::ContactAvatarJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url] contact_inbox rescue StandardError => e diff --git a/app/builders/messages/outgoing/normal_builder.rb b/app/builders/messages/outgoing/normal_builder.rb index 3b44fcd0e..1b15f24c0 100644 --- a/app/builders/messages/outgoing/normal_builder.rb +++ b/app/builders/messages/outgoing/normal_builder.rb @@ -15,7 +15,6 @@ class Messages::Outgoing::NormalBuilder def perform @message = @conversation.messages.build(message_params) - @message.save if @attachments.present? @attachments.each do |uploaded_attachment| attachment = @message.attachments.new( @@ -24,8 +23,8 @@ class Messages::Outgoing::NormalBuilder ) attachment.file.attach(uploaded_attachment) end - @message.save end + @message.save @message end diff --git a/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb b/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb index c3d6554fd..c0c121900 100644 --- a/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb +++ b/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb @@ -2,13 +2,15 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::BaseControlle before_action :authorize_request def create - authenticate_twilio - build_inbox - setup_webhooks - rescue Twilio::REST::TwilioError => e - render_could_not_create_error(e.message) - rescue StandardError => e - render_could_not_create_error(e.message) + ActiveRecord::Base.transaction do + authenticate_twilio + build_inbox + setup_webhooks if @twilio_channel.sms? + rescue Twilio::REST::TwilioError => e + render_could_not_create_error(e.message) + rescue StandardError => e + render_could_not_create_error(e.message) + end end private @@ -26,25 +28,30 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::BaseControlle ::Twilio::WebhookSetupService.new(inbox: @inbox).perform end + def phone_number + medium == 'sms' ? permitted_params[:phone_number] : "whatsapp:#{permitted_params[:phone_number]}" + end + + def medium + permitted_params[:medium] + end + def build_inbox - ActiveRecord::Base.transaction do - twilio_sms = current_account.twilio_sms.create( - account_sid: permitted_params[:account_sid], - auth_token: permitted_params[:auth_token], - phone_number: permitted_params[:phone_number] - ) - @inbox = current_account.inboxes.create( - name: permitted_params[:name], - channel: twilio_sms - ) - rescue StandardError => e - render_could_not_create_error(e.message) - end + @twilio_channel = current_account.twilio_sms.create!( + account_sid: permitted_params[:account_sid], + auth_token: permitted_params[:auth_token], + phone_number: phone_number, + medium: medium + ) + @inbox = current_account.inboxes.create( + name: permitted_params[:name], + channel: @twilio_channel + ) end def permitted_params params.require(:twilio_channel).permit( - :account_id, :phone_number, :account_sid, :auth_token, :name + :account_id, :phone_number, :account_sid, :auth_token, :name, :medium ) end end diff --git a/app/controllers/twilio/callback_controller.rb b/app/controllers/twilio/callback_controller.rb index f6cb5356c..44dcc9b6f 100644 --- a/app/controllers/twilio/callback_controller.rb +++ b/app/controllers/twilio/callback_controller.rb @@ -23,7 +23,9 @@ class Twilio::CallbackController < ApplicationController :FromZip, :Body, :ToCountry, - :FromState + :FromState, + :MediaUrl0, + :MediaContentType0 ) end end diff --git a/app/javascript/dashboard/assets/images/channels/whatsapp.png b/app/javascript/dashboard/assets/images/channels/whatsapp.png new file mode 100644 index 000000000..547ff675e Binary files /dev/null and b/app/javascript/dashboard/assets/images/channels/whatsapp.png differ diff --git a/app/javascript/dashboard/components/widgets/Thumbnail.vue b/app/javascript/dashboard/components/widgets/Thumbnail.vue index 01cd7f76b..9110939ea 100644 --- a/app/javascript/dashboard/components/widgets/Thumbnail.vue +++ b/app/javascript/dashboard/components/widgets/Thumbnail.vue @@ -33,6 +33,14 @@ :style="badgeStyle" src="~dashboard/assets/images/twitter-badge.png" /> + +