chore: Handle attachments in Whatsapp Channel (#3299)

send and receive attachments in 360Dialog WhatsApp channels
This commit is contained in:
Sojan Jose
2021-11-11 11:33:48 +04:00
committed by GitHub
parent b3e313a200
commit a4c87f2052
33 changed files with 422 additions and 45 deletions

View File

@@ -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

View File

@@ -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