feat: added input_select type message support for whatsapp (#6886)

- Added input_select message type support for Whatsapp Cloud API and Whatsapp 360dialog.

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Chamath K.B. Attanayaka
2023-06-09 15:20:45 +05:30
committed by GitHub
parent c8fac08e0b
commit c715e396f0
5 changed files with 208 additions and 1 deletions

View File

@@ -26,4 +26,54 @@ class Whatsapp::Providers::BaseService
def validate_provider_config
raise 'Overwrite this method in child class'
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.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 => 'Choose an item', 'sections' => sections }
create_payload('list', message.content, JSON.generate(json_hash))
end
end

View File

@@ -2,6 +2,8 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
def send_message(phone_number, 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
@@ -112,4 +114,20 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
}]
}
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)
end
end

View File

@@ -2,6 +2,8 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
def send_message(phone_number, 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
@@ -129,4 +131,21 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
}]
}
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)
end
end