chore: Handle attachments in Whatsapp Channel (#3299)
send and receive attachments in 360Dialog WhatsApp channels
This commit is contained in:
@@ -228,7 +228,7 @@ export default {
|
||||
return (
|
||||
this.isAWebWidgetInbox ||
|
||||
this.isAFacebookInbox ||
|
||||
this.isATwilioWhatsappChannel ||
|
||||
this.isAWhatsappChannel ||
|
||||
this.isAPIInbox ||
|
||||
this.isAnEmailChannel ||
|
||||
this.isATwilioSMSChannel ||
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
<div class="wizard-body small-12 medium-9 columns height-auto">
|
||||
<page-header
|
||||
:header-title="$t('INBOX_MGMT.ADD.AUTH.TITLE')"
|
||||
:header-content="$t('INBOX_MGMT.ADD.AUTH.DESC')"
|
||||
:header-content="
|
||||
useInstallationName(
|
||||
$t('INBOX_MGMT.ADD.AUTH.DESC'),
|
||||
globalConfig.installationName
|
||||
)
|
||||
"
|
||||
/>
|
||||
<div class="row channels">
|
||||
<channel-item
|
||||
@@ -21,12 +26,14 @@ import ChannelItem from 'dashboard/components/widgets/ChannelItem';
|
||||
import router from '../../../index';
|
||||
import PageHeader from '../SettingsSubPageHeader';
|
||||
import { mapGetters } from 'vuex';
|
||||
import globalConfigMixin from 'shared/mixins/globalConfigMixin';
|
||||
|
||||
export default {
|
||||
components: {
|
||||
ChannelItem,
|
||||
PageHeader,
|
||||
},
|
||||
mixins: [globalConfigMixin],
|
||||
data() {
|
||||
return {
|
||||
enabledFeatures: {},
|
||||
|
||||
@@ -43,7 +43,7 @@ class Channel::Telegram < ApplicationRecord
|
||||
response = HTTParty.get("#{telegram_api_url}/getUserProfilePhotos", query: { user_id: user_id })
|
||||
return nil unless response.success?
|
||||
|
||||
photos = response.parsed_response['result']['photos']
|
||||
photos = response.parsed_response.dig('result', 'photos')
|
||||
return if photos.blank?
|
||||
|
||||
get_telegram_file_path(photos.first.last['file_id'])
|
||||
|
||||
@@ -36,15 +36,19 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
|
||||
# Extract later into provider Service
|
||||
def send_message(phone_number, message)
|
||||
HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
|
||||
body: {
|
||||
to: phone_number,
|
||||
text: { body: message },
|
||||
type: 'text'
|
||||
}.to_json
|
||||
)
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
else
|
||||
send_text_message(phone_number, message)
|
||||
end
|
||||
end
|
||||
|
||||
def media_url(media_id)
|
||||
"#{api_base_path}/media/#{media_id}"
|
||||
end
|
||||
|
||||
def api_headers
|
||||
{ 'D360-API-KEY' => provider_config['api_key'], 'Content-Type' => 'application/json' }
|
||||
end
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
@@ -53,6 +57,36 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def send_text_message(phone_number, message)
|
||||
HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
|
||||
body: {
|
||||
to: phone_number,
|
||||
text: { body: message.content },
|
||||
type: 'text'
|
||||
}.to_json
|
||||
)
|
||||
end
|
||||
|
||||
def send_attachment_message(phone_number, message)
|
||||
attachment = message.attachments.first
|
||||
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
||||
attachment_url = attachment.file_url
|
||||
HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
|
||||
body: {
|
||||
'to' => phone_number,
|
||||
'type' => type,
|
||||
type.to_s => {
|
||||
'link': attachment_url,
|
||||
'caption': message.content
|
||||
}
|
||||
}.to_json
|
||||
)
|
||||
end
|
||||
|
||||
# Extract later into provider Service
|
||||
def validate_provider_config
|
||||
response = HTTParty.post(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Find the various telegram payload samples here: https://core.telegram.org/bots/webhooks#testing-your-bot-with-updates
|
||||
# https://core.telegram.org/bots/api#available-types
|
||||
# https://docs.360dialog.com/whatsapp-api/whatsapp-api/media
|
||||
# https://developers.facebook.com/docs/whatsapp/api/media/
|
||||
|
||||
class Whatsapp::IncomingMessageService
|
||||
pattr_initialize [:inbox!, :params!]
|
||||
@@ -12,7 +12,7 @@ class Whatsapp::IncomingMessageService
|
||||
|
||||
return if params[:messages].blank?
|
||||
|
||||
@message = @conversation.messages.create(
|
||||
@message = @conversation.messages.build(
|
||||
content: params[:messages].first.dig(:text, :body),
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
@@ -20,6 +20,7 @@ class Whatsapp::IncomingMessageService
|
||||
sender: @contact,
|
||||
source_id: params[:messages].first[:id].to_s
|
||||
)
|
||||
attach_files
|
||||
@message.save!
|
||||
end
|
||||
|
||||
@@ -58,4 +59,31 @@ class Whatsapp::IncomingMessageService
|
||||
|
||||
@conversation = ::Conversation.create!(conversation_params)
|
||||
end
|
||||
|
||||
def file_content_type(file_type)
|
||||
return :image if %w[image sticker].include?(file_type)
|
||||
return :audio if %w[audio voice].include?(file_type)
|
||||
return :video if ['video'].include?(file_type)
|
||||
|
||||
'document'
|
||||
end
|
||||
|
||||
def attach_files
|
||||
message_type = params[:messages].first[:type]
|
||||
return if message_type == 'text'
|
||||
|
||||
attachment_payload = params[:messages].first[message_type.to_sym]
|
||||
attachment_file = Down.download(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers)
|
||||
|
||||
@message.content ||= attachment_payload[:caption]
|
||||
@message.attachments.new(
|
||||
account_id: @message.account_id,
|
||||
file_type: file_content_type(message_type),
|
||||
file: {
|
||||
io: attachment_file,
|
||||
filename: attachment_file,
|
||||
content_type: attachment_file.content_type
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,6 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
|
||||
end
|
||||
|
||||
def perform_reply
|
||||
channel.send_message(message.conversation.contact_inbox.source_id, message.content)
|
||||
channel.send_message(message.conversation.contact_inbox.source_id, message)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user