Feat: Support MMS in SMS Channel ( Bandwidth ) (#4046)
Ability to send and receive MMS to bandwidth channel fixes: #3961
This commit is contained in:
@@ -3,7 +3,7 @@ module FileTypeHelper
|
||||
def file_type(content_type)
|
||||
return :image if image_file?(content_type)
|
||||
return :video if video_file?(content_type)
|
||||
return :audio if content_type.include?('audio/')
|
||||
return :audio if content_type&.include?('audio/')
|
||||
|
||||
:file
|
||||
end
|
||||
|
||||
@@ -293,7 +293,7 @@ export default {
|
||||
if (this.isAWhatsappChannel) {
|
||||
return MESSAGE_MAX_LENGTH.TWILIO_WHATSAPP;
|
||||
}
|
||||
if (this.isATwilioSMSChannel) {
|
||||
if (this.isASmsInbox) {
|
||||
return MESSAGE_MAX_LENGTH.TWILIO_SMS;
|
||||
}
|
||||
if (this.isATwitterInbox) {
|
||||
@@ -310,7 +310,7 @@ export default {
|
||||
this.isAWhatsappChannel ||
|
||||
this.isAPIInbox ||
|
||||
this.isAnEmailChannel ||
|
||||
this.isATwilioSMSChannel ||
|
||||
this.isASmsInbox ||
|
||||
this.isATelegramChannel
|
||||
);
|
||||
},
|
||||
|
||||
@@ -44,6 +44,9 @@ export default {
|
||||
const { medium: medium = '' } = this.inbox;
|
||||
return this.isATwilioChannel && medium === 'sms';
|
||||
},
|
||||
isASmsInbox() {
|
||||
return this.channelType === INBOX_TYPES.SMS || this.isATwilioSMSChannel;
|
||||
},
|
||||
isATwilioWhatsappChannel() {
|
||||
const { medium: medium = '' } = this.inbox;
|
||||
return this.isATwilioChannel && medium === 'whatsapp';
|
||||
|
||||
@@ -62,6 +62,18 @@ describe('inboxMixin', () => {
|
||||
expect(wrapper.vm.isAWebWidgetInbox).toBe(true);
|
||||
});
|
||||
|
||||
it('isASmsInbox returns true if channel type is sms', () => {
|
||||
const Component = {
|
||||
render() {},
|
||||
mixins: [inboxMixin],
|
||||
data() {
|
||||
return { inbox: { channel_type: 'Channel::Sms' } };
|
||||
},
|
||||
};
|
||||
const wrapper = shallowMount(Component);
|
||||
expect(wrapper.vm.isASmsInbox).toBe(true);
|
||||
});
|
||||
|
||||
it('isATwilioChannel returns true if channel type is Twilio', () => {
|
||||
const Component = {
|
||||
render() {},
|
||||
@@ -94,6 +106,7 @@ describe('inboxMixin', () => {
|
||||
const wrapper = shallowMount(Component);
|
||||
expect(wrapper.vm.isATwilioChannel).toBe(true);
|
||||
expect(wrapper.vm.isATwilioSMSChannel).toBe(true);
|
||||
expect(wrapper.vm.isASmsInbox).toBe(true);
|
||||
});
|
||||
|
||||
it('isATwilioWhatsappChannel returns true if channel type is Twilio and medium is whatsapp', () => {
|
||||
|
||||
@@ -48,6 +48,10 @@ class Attachment < ApplicationRecord
|
||||
file.attached? ? url_for(file) : ''
|
||||
end
|
||||
|
||||
def download_url
|
||||
file.attached? ? rails_storage_proxy_url(file) : ''
|
||||
end
|
||||
|
||||
def thumb_url
|
||||
if file.attached? && file.representable?
|
||||
url_for(file.representation(resize: '250x250'))
|
||||
|
||||
@@ -33,35 +33,38 @@ class Channel::Sms < ApplicationRecord
|
||||
'https://messaging.bandwidth.com/api/v2'
|
||||
end
|
||||
|
||||
# Extract later into provider Service
|
||||
def send_message(phone_number, message)
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
else
|
||||
send_text_message(phone_number, message.content)
|
||||
end
|
||||
def send_message(contact_number, message)
|
||||
body = message_body(contact_number, message.content)
|
||||
body['media'] = message.attachments.map(&:download_url) if message.attachments.present?
|
||||
|
||||
send_to_bandwidth(body)
|
||||
end
|
||||
|
||||
def send_text_message(contact_number, message)
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
|
||||
basic_auth: bandwidth_auth,
|
||||
headers: { 'Content-Type' => 'application/json' },
|
||||
body: {
|
||||
'to' => contact_number,
|
||||
'from' => phone_number,
|
||||
'text' => message,
|
||||
'applicationId' => provider_config['application_id']
|
||||
}.to_json
|
||||
)
|
||||
|
||||
response.success? ? response.parsed_response['id'] : nil
|
||||
def send_text_message(contact_number, message_content)
|
||||
body = message_body(contact_number, message_content)
|
||||
send_to_bandwidth(body)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_attachment_message(phone_number, message)
|
||||
# fix me
|
||||
def message_body(contact_number, message_content)
|
||||
{
|
||||
'to' => contact_number,
|
||||
'from' => phone_number,
|
||||
'text' => message_content,
|
||||
'applicationId' => provider_config['application_id']
|
||||
}
|
||||
end
|
||||
|
||||
def send_to_bandwidth(body)
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
|
||||
basic_auth: bandwidth_auth,
|
||||
headers: { 'Content-Type' => 'application/json' },
|
||||
body: body.to_json
|
||||
)
|
||||
|
||||
response.success? ? response.parsed_response['id'] : nil
|
||||
end
|
||||
|
||||
def bandwidth_auth
|
||||
|
||||
@@ -14,6 +14,8 @@ class Sms::IncomingMessageService
|
||||
sender: @contact,
|
||||
source_id: params[:id]
|
||||
)
|
||||
attach_files
|
||||
@message.save!
|
||||
end
|
||||
|
||||
private
|
||||
@@ -22,6 +24,10 @@ class Sms::IncomingMessageService
|
||||
@account ||= @inbox.account
|
||||
end
|
||||
|
||||
def channel
|
||||
@channel ||= @inbox.channel
|
||||
end
|
||||
|
||||
def phone_number
|
||||
params[:from]
|
||||
end
|
||||
@@ -63,4 +69,28 @@ class Sms::IncomingMessageService
|
||||
phone_number: phone_number
|
||||
}
|
||||
end
|
||||
|
||||
def attach_files
|
||||
return if params[:media].blank?
|
||||
|
||||
params[:media].each do |media_url|
|
||||
# we don't need to process this files since chatwoot doesn't support it
|
||||
next if media_url.end_with? '.smil'
|
||||
|
||||
attachment_file = Down.download(
|
||||
media_url,
|
||||
http_basic_authentication: [channel.provider_config['api_key'], channel.provider_config['api_secret']]
|
||||
)
|
||||
|
||||
@message.attachments.new(
|
||||
account_id: @message.account_id,
|
||||
file_type: file_type(attachment_file.content_type),
|
||||
file: {
|
||||
io: attachment_file,
|
||||
filename: attachment_file,
|
||||
content_type: attachment_file.content_type
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user