feat: Support for Whatsapp Cloud API (#4938)

Ability to configure Whatsapp Cloud API Inboxes

fixes: #4712
This commit is contained in:
Sojan Jose
2022-07-06 21:45:03 +02:00
committed by GitHub
parent 4375a7646e
commit a6c609f43d
27 changed files with 999 additions and 229 deletions

View File

@@ -0,0 +1,110 @@
# Mostly modeled after the intial implementation of the service based on 360 Dialog
# https://docs.360dialog.com/whatsapp-api/whatsapp-api/media
# https://developers.facebook.com/docs/whatsapp/api/media/
class Whatsapp::IncomingMessageBaseService
pattr_initialize [:inbox!, :params!]
def perform
processed_params
set_contact
return unless @contact
set_conversation
return if @processed_params[:messages].blank?
@message = @conversation.messages.build(
content: message_content(@processed_params[:messages].first),
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
sender: @contact,
source_id: @processed_params[:messages].first[:id].to_s
)
attach_files
@message.save!
end
private
def processed_params
@processed_params ||= params
end
def message_content(message)
# TODO: map interactive messages back to button messages in chatwoot
message.dig(:text, :body) ||
message.dig(:button, :text) ||
message.dig(:interactive, :button_reply, :title) ||
message.dig(:interactive, :list_reply, :title)
end
def account
@account ||= inbox.account
end
def set_contact
contact_params = @processed_params[:contacts]&.first
return if contact_params.blank?
contact_inbox = ::ContactBuilder.new(
source_id: contact_params[:wa_id],
inbox: inbox,
contact_attributes: { name: contact_params.dig(:profile, :name), phone_number: "+#{@processed_params[:messages].first[:from]}" }
).perform
@contact_inbox = contact_inbox
@contact = contact_inbox.contact
end
def conversation_params
{
account_id: @inbox.account_id,
inbox_id: @inbox.id,
contact_id: @contact.id,
contact_inbox_id: @contact_inbox.id
}
end
def set_conversation
@conversation = @contact_inbox.conversations.last
return if @conversation
@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)
:file
end
def message_type
@processed_params[:messages].first[:type]
end
def attach_files
return if %w[text button interactive].include?(message_type)
attachment_payload = @processed_params[:messages].first[message_type.to_sym]
attachment_file = download_attachment_file(attachment_payload)
@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.original_filename,
content_type: attachment_file.content_type
}
)
end
def download_attachment_file(attachment_payload)
Down.download(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers)
end
end

View File

@@ -1,100 +1,5 @@
# https://docs.360dialog.com/whatsapp-api/whatsapp-api/media
# https://developers.facebook.com/docs/whatsapp/api/media/
class Whatsapp::IncomingMessageService
pattr_initialize [:inbox!, :params!]
def perform
set_contact
return unless @contact
set_conversation
return if params[:messages].blank?
@message = @conversation.messages.build(
content: message_content(params[:messages].first),
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
sender: @contact,
source_id: params[:messages].first[:id].to_s
)
attach_files
@message.save!
end
private
def message_content(message)
# TODO: map interactive messages back to button messages in chatwoot
message.dig(:text, :body) ||
message.dig(:button, :text) ||
message.dig(:interactive, :button_reply, :title) ||
message.dig(:interactive, :list_reply, :title)
end
def account
@account ||= inbox.account
end
def set_contact
contact_params = params[:contacts]&.first
return if contact_params.blank?
contact_inbox = ::ContactBuilder.new(
source_id: contact_params[:wa_id],
inbox: inbox,
contact_attributes: { name: contact_params.dig(:profile, :name), phone_number: "+#{params[:messages].first[:from]}" }
).perform
@contact_inbox = contact_inbox
@contact = contact_inbox.contact
end
def conversation_params
{
account_id: @inbox.account_id,
inbox_id: @inbox.id,
contact_id: @contact.id,
contact_inbox_id: @contact_inbox.id
}
end
def set_conversation
@conversation = @contact_inbox.conversations.last
return if @conversation
@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)
:file
end
def attach_files
return if %w[text button interactive].include?(message_type)
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.original_filename,
content_type: attachment_file.content_type
}
)
end
def message_type
params[:messages].first[:type]
end
class Whatsapp::IncomingMessageService < Whatsapp::IncomingMessageBaseService
end

View File

@@ -0,0 +1,15 @@
# https://docs.360dialog.com/whatsapp-api/whatsapp-api/media
# https://developers.facebook.com/docs/whatsapp/api/media/
class Whatsapp::IncomingMessageWhatsappCloudService < Whatsapp::IncomingMessageBaseService
private
def processed_params
@processed_params ||= params[:entry].first['changes'].first['value']
end
def download_attachment_file(attachment_payload)
url_response = HTTParty.get(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers)
Down.download(url_response.parsed_response['url'], headers: inbox.channel.api_headers)
end
end

View File

@@ -0,0 +1,29 @@
#######################################
# To create a whatsapp provider
# - Inherit this as the base class.
# - Implement `send_message` method in your child class.
# - Implement `send_template_message` method in your child class.
# - Implement `sync_templates` method in your child class.
# - Implement `validate_provider_config` method in your child class.
# - Use Childclass.new(whatsapp_channel: channel).perform.
######################################
class Whatsapp::Providers::BaseService
pattr_initialize [:whatsapp_channel!]
def send_message(_phone_number, _message)
raise 'Overwrite this method in child class'
end
def send_template(_phone_number, _template_info)
raise 'Overwrite this method in child class'
end
def sync_template
raise 'Overwrite this method in child class'
end
def validate_provider_config
raise 'Overwrite this method in child class'
end
end

View File

@@ -0,0 +1,112 @@
class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseService
def send_message(phone_number, message)
if message.attachments.present?
send_attachment_message(phone_number, message)
else
send_text_message(phone_number, message)
end
end
def send_template(phone_number, template_info)
response = HTTParty.post(
"#{api_base_path}/messages",
headers: api_headers,
body: {
to: phone_number,
template: template_body_parameters(template_info),
type: 'template'
}.to_json
)
process_response(response)
end
def sync_templates
response = HTTParty.get("#{api_base_path}/configs/templates", headers: api_headers)
whatsapp_channel.update(message_templates: response['waba_templates'], message_templates_last_updated: Time.now.utc) if response.success?
end
def validate_provider_config?
response = HTTParty.post(
"#{api_base_path}/configs/webhook",
headers: { 'D360-API-KEY': whatsapp_channel.provider_config['api_key'], 'Content-Type': 'application/json' },
body: {
url: "#{ENV['FRONTEND_URL']}/webhooks/whatsapp/#{whatsapp_channel.phone_number}"
}.to_json
)
response.success?
end
def api_headers
{ 'D360-API-KEY' => whatsapp_channel.provider_config['api_key'], 'Content-Type' => 'application/json' }
end
def media_url(media_id)
"#{api_base_path}/media/#{media_id}"
end
private
def api_base_path
# provide the environment variable when testing against sandbox : 'https://waba-sandbox.360dialog.io/v1'
ENV.fetch('360DIALOG_BASE_URL', 'https://waba.360dialog.io/v1')
end
def send_text_message(phone_number, message)
response = HTTParty.post(
"#{api_base_path}/messages",
headers: api_headers,
body: {
to: phone_number,
text: { body: message.content },
type: 'text'
}.to_json
)
process_response(response)
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.download_url
response = HTTParty.post(
"#{api_base_path}/messages",
headers: api_headers,
body: {
'to' => phone_number,
'type' => type,
type.to_s => {
'link': attachment_url,
'caption': message.content
}
}.to_json
)
process_response(response)
end
def process_response(response)
if response.success?
response['messages'].first['id']
else
Rails.logger.error response.body
nil
end
end
def template_body_parameters(template_info)
{
name: template_info[:name],
namespace: template_info[:namespace],
language: {
policy: 'deterministic',
code: template_info[:lang_code]
},
components: [{
type: 'body',
parameters: template_info[:parameters]
}]
}
end
end

View File

@@ -0,0 +1,112 @@
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
def send_message(phone_number, message)
if message.attachments.present?
send_attachment_message(phone_number, message)
else
send_text_message(phone_number, message)
end
end
def send_template(phone_number, template_info)
response = HTTParty.post(
"#{phone_id_path}/messages",
headers: api_headers,
body: {
messaging_product: 'whatsapp',
to: phone_number,
template: template_body_parameters(template_info),
type: 'template'
}.to_json
)
process_response(response)
end
def sync_templates
response = HTTParty.get("#{business_account_path}/message_templates?access_token=#{whatsapp_channel.provider_config['api_key']}")
whatsapp_channel.update(message_templates: response['data'], message_templates_last_updated: Time.now.utc) if response.success?
end
def validate_provider_config?
response = HTTParty.get("#{business_account_path}/message_templates?access_token=#{whatsapp_channel.provider_config['api_key']}")
response.success?
end
def api_headers
{ 'Authorization' => "Bearer #{whatsapp_channel.provider_config['api_key']}", 'Content-Type' => 'application/json' }
end
def media_url(media_id)
"https://graph.facebook.com/v13.0/#{media_id}"
end
private
# TODO: See if we can unify the API versions and for both paths and make it consistent with out facebook app API versions
def phone_id_path
"https://graph.facebook.com/v13.0/#{whatsapp_channel.provider_config['phone_number_id']}"
end
def business_account_path
"https://graph.facebook.com/v14.0/#{whatsapp_channel.provider_config['business_account_id']}"
end
def send_text_message(phone_number, message)
response = HTTParty.post(
"#{phone_id_path}/messages",
headers: api_headers,
body: {
messaging_product: 'whatsapp',
to: phone_number,
text: { body: message.content },
type: 'text'
}.to_json
)
process_response(response)
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.download_url
response = HTTParty.post(
"#{phone_id_path}/messages",
headers: api_headers,
body: {
messaging_product: 'whatsapp',
'to' => phone_number,
'type' => type,
type.to_s => {
'link': attachment_url,
'caption': message.content
}
}.to_json
)
process_response(response)
end
def process_response(response)
if response.success?
response['messages'].first['id']
else
Rails.logger.error response.body
nil
end
end
def template_body_parameters(template_info)
{
name: template_info[:name],
language: {
policy: 'deterministic',
code: template_info[:lang_code]
},
components: [{
type: 'body',
parameters: template_info[:parameters]
}]
}
end
end

View File

@@ -43,7 +43,7 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
# An example template may look like "Your package has been shipped. It will be delivered in {{1}} business days.
# We want to iterate over these templates with our message body and see if we can fit it to any of the templates
# Then we use regex to parse the template varibles and convert them into the proper payload
channel.message_templates.each do |template|
channel.message_templates&.each do |template|
match_obj = template_match_object(template)
next if match_obj.blank?