Restructure omni services and add Chatwoot research snapshot
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
#######################################
|
||||
# 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, _message)
|
||||
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
|
||||
|
||||
def error_message
|
||||
raise 'Overwrite this method in child class'
|
||||
end
|
||||
|
||||
def process_response(response, message)
|
||||
parsed_response = response.parsed_response
|
||||
if response.success? && parsed_response['error'].blank?
|
||||
parsed_response['messages'].first['id']
|
||||
else
|
||||
handle_error(response, message)
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def handle_error(response, message)
|
||||
Rails.logger.error response.body
|
||||
return if message.blank?
|
||||
|
||||
# https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/#sample-response
|
||||
error_message = error_message(response)
|
||||
return if error_message.blank?
|
||||
|
||||
message.external_error = error_message
|
||||
message.status = :failed
|
||||
message.save!
|
||||
end
|
||||
|
||||
def create_buttons(items)
|
||||
buttons = []
|
||||
items.each do |item|
|
||||
button = { :type => 'reply', 'reply' => { 'id' => item['value'], 'title' => item['title'] } }
|
||||
buttons << button
|
||||
end
|
||||
buttons
|
||||
end
|
||||
|
||||
def create_rows(items)
|
||||
rows = []
|
||||
items.each do |item|
|
||||
row = { 'id' => item['value'], 'title' => item['title'] }
|
||||
rows << row
|
||||
end
|
||||
rows
|
||||
end
|
||||
|
||||
def create_payload(type, message_content, action)
|
||||
{
|
||||
'type': type,
|
||||
'body': {
|
||||
'text': message_content
|
||||
},
|
||||
'action': action
|
||||
}
|
||||
end
|
||||
|
||||
def create_payload_based_on_items(message)
|
||||
if message.content_attributes['items'].length <= 3
|
||||
create_button_payload(message)
|
||||
else
|
||||
create_list_payload(message)
|
||||
end
|
||||
end
|
||||
|
||||
def create_button_payload(message)
|
||||
buttons = create_buttons(message.content_attributes['items'])
|
||||
json_hash = { 'buttons' => buttons }
|
||||
create_payload('button', message.outgoing_content, JSON.generate(json_hash))
|
||||
end
|
||||
|
||||
def create_list_payload(message)
|
||||
rows = create_rows(message.content_attributes['items'])
|
||||
section1 = { 'rows' => rows }
|
||||
sections = [section1]
|
||||
json_hash = { :button => I18n.t('conversations.messages.whatsapp.list_button_label'), 'sections' => sections }
|
||||
create_payload('list', message.outgoing_content, JSON.generate(json_hash))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,128 @@
|
||||
class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseService
|
||||
def send_message(phone_number, message)
|
||||
@message = message
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
elsif message.content_type == 'input_select'
|
||||
send_interactive_text_message(phone_number, message)
|
||||
else
|
||||
send_text_message(phone_number, message)
|
||||
end
|
||||
end
|
||||
|
||||
def send_template(phone_number, template_info, message)
|
||||
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, message)
|
||||
end
|
||||
|
||||
def sync_templates
|
||||
# ensuring that channels with wrong provider config wouldn't keep trying to sync templates
|
||||
whatsapp_channel.mark_message_templates_updated
|
||||
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.fetch('FRONTEND_URL', nil)}/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.outgoing_content },
|
||||
type: 'text'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
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'
|
||||
type_content = {
|
||||
'link': attachment.download_url
|
||||
}
|
||||
type_content['caption'] = message.outgoing_content unless %w[audio sticker].include?(type)
|
||||
type_content['filename'] = attachment.file.filename if type == 'document'
|
||||
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: api_headers,
|
||||
body: {
|
||||
'to' => phone_number,
|
||||
'type' => type,
|
||||
type.to_s => type_content
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
end
|
||||
|
||||
def error_message(response)
|
||||
# {"meta": {"success": false, "http_code": 400, "developer_message": "errro-message", "360dialog_trace_id": "someid"}}
|
||||
response.parsed_response.dig('meta', 'developer_message')
|
||||
end
|
||||
|
||||
def template_body_parameters(template_info)
|
||||
{
|
||||
name: template_info[:name],
|
||||
namespace: template_info[:namespace],
|
||||
language: {
|
||||
policy: 'deterministic',
|
||||
code: template_info[:lang_code]
|
||||
},
|
||||
components: template_info[:parameters]
|
||||
}
|
||||
end
|
||||
|
||||
def send_interactive_text_message(phone_number, message)
|
||||
payload = create_payload_based_on_items(message)
|
||||
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: api_headers,
|
||||
body: {
|
||||
to: phone_number,
|
||||
interactive: payload,
|
||||
type: 'interactive'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,207 @@
|
||||
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
|
||||
def send_message(phone_number, message)
|
||||
@message = message
|
||||
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
elsif message.content_type == 'input_select'
|
||||
send_interactive_text_message(phone_number, message)
|
||||
else
|
||||
send_text_message(phone_number, message)
|
||||
end
|
||||
end
|
||||
|
||||
def send_template(phone_number, template_info, message)
|
||||
template_body = template_body_parameters(template_info)
|
||||
|
||||
request_body = {
|
||||
messaging_product: 'whatsapp',
|
||||
recipient_type: 'individual', # Only individual messages supported (not group messages)
|
||||
to: phone_number,
|
||||
type: 'template',
|
||||
template: template_body
|
||||
}
|
||||
|
||||
response = HTTParty.post(
|
||||
"#{phone_id_path}/messages",
|
||||
headers: api_headers,
|
||||
body: request_body.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
end
|
||||
|
||||
def sync_templates
|
||||
# ensuring that channels with wrong provider config wouldn't keep trying to sync templates
|
||||
whatsapp_channel.mark_message_templates_updated
|
||||
templates = fetch_whatsapp_templates("#{business_account_path}/message_templates?access_token=#{whatsapp_channel.provider_config['api_key']}")
|
||||
whatsapp_channel.update(message_templates: templates, message_templates_last_updated: Time.now.utc) if templates.present?
|
||||
end
|
||||
|
||||
def fetch_whatsapp_templates(url)
|
||||
response = HTTParty.get(url)
|
||||
return [] unless response.success?
|
||||
|
||||
next_url = next_url(response)
|
||||
|
||||
return response['data'] + fetch_whatsapp_templates(next_url) if next_url.present?
|
||||
|
||||
response['data']
|
||||
end
|
||||
|
||||
def next_url(response)
|
||||
response['paging'] ? response['paging']['next'] : ''
|
||||
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 create_csat_template(template_config)
|
||||
csat_template_service.create_template(template_config)
|
||||
end
|
||||
|
||||
def delete_csat_template(template_name = nil)
|
||||
template_name ||= CsatTemplateNameService.csat_template_name(whatsapp_channel.inbox.id)
|
||||
csat_template_service.delete_template(template_name)
|
||||
end
|
||||
|
||||
def get_template_status(template_name)
|
||||
csat_template_service.get_template_status(template_name)
|
||||
end
|
||||
|
||||
def media_url(media_id)
|
||||
"#{api_base_path}/v13.0/#{media_id}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def csat_template_service
|
||||
@csat_template_service ||= Whatsapp::CsatTemplateService.new(whatsapp_channel)
|
||||
end
|
||||
|
||||
def api_base_path
|
||||
ENV.fetch('WHATSAPP_CLOUD_BASE_URL', 'https://graph.facebook.com')
|
||||
end
|
||||
|
||||
# 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
|
||||
"#{api_base_path}/v13.0/#{whatsapp_channel.provider_config['phone_number_id']}"
|
||||
end
|
||||
|
||||
def business_account_path
|
||||
"#{api_base_path}/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',
|
||||
context: whatsapp_reply_context(message),
|
||||
to: phone_number,
|
||||
text: { body: message.outgoing_content },
|
||||
type: 'text'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
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'
|
||||
type_content = {
|
||||
'link': attachment.download_url
|
||||
}
|
||||
type_content['caption'] = message.outgoing_content unless %w[audio sticker].include?(type)
|
||||
type_content['filename'] = attachment.file.filename if type == 'document'
|
||||
response = HTTParty.post(
|
||||
"#{phone_id_path}/messages",
|
||||
headers: api_headers,
|
||||
body: {
|
||||
:messaging_product => 'whatsapp',
|
||||
:context => whatsapp_reply_context(message),
|
||||
'to' => phone_number,
|
||||
'type' => type,
|
||||
type.to_s => type_content
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
end
|
||||
|
||||
def error_message(response)
|
||||
# https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/#sample-response
|
||||
response.parsed_response&.dig('error', 'message')
|
||||
end
|
||||
|
||||
def template_body_parameters(template_info)
|
||||
template_body = {
|
||||
name: template_info[:name],
|
||||
language: {
|
||||
policy: 'deterministic',
|
||||
code: template_info[:lang_code]
|
||||
}
|
||||
}
|
||||
|
||||
# Enhanced template parameters structure
|
||||
# Note: Legacy format support (simple parameter arrays) has been removed
|
||||
# in favor of the enhanced component-based structure that supports
|
||||
# headers, buttons, and authentication templates.
|
||||
#
|
||||
# Expected payload format from frontend:
|
||||
# {
|
||||
# processed_params: {
|
||||
# body: { '1': 'John', '2': '123 Main St' },
|
||||
# header: {
|
||||
# media_url: 'https://...',
|
||||
# media_type: 'image',
|
||||
# media_name: 'filename.pdf' # Optional, for document templates only
|
||||
# },
|
||||
# buttons: [{ type: 'url', parameter: 'otp123456' }]
|
||||
# }
|
||||
# }
|
||||
# This gets transformed into WhatsApp API component format:
|
||||
# [
|
||||
# { type: 'body', parameters: [...] },
|
||||
# { type: 'header', parameters: [...] },
|
||||
# { type: 'button', sub_type: 'url', parameters: [...] }
|
||||
# ]
|
||||
template_body[:components] = template_info[:parameters] || []
|
||||
|
||||
template_body
|
||||
end
|
||||
|
||||
def whatsapp_reply_context(message)
|
||||
reply_to = message.content_attributes[:in_reply_to_external_id]
|
||||
return nil if reply_to.blank?
|
||||
|
||||
{
|
||||
message_id: reply_to
|
||||
}
|
||||
end
|
||||
|
||||
def send_interactive_text_message(phone_number, message)
|
||||
payload = create_payload_based_on_items(message)
|
||||
|
||||
response = HTTParty.post(
|
||||
"#{phone_id_path}/messages",
|
||||
headers: api_headers,
|
||||
body: {
|
||||
messaging_product: 'whatsapp',
|
||||
to: phone_number,
|
||||
interactive: payload,
|
||||
type: 'interactive'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
process_response(response, message)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user