Compare commits

...
Author SHA1 Message Date
Muhsin Keloth f4b9092ac2 chore: add whatsapp typing indicators 2025-04-30 13:25:37 +05:30
4 changed files with 35 additions and 0 deletions
+1
View File
@@ -54,6 +54,7 @@ class Channel::Whatsapp < ApplicationRecord
delegate :send_message, to: :provider_service
delegate :send_template, to: :provider_service
delegate :send_typing_indicator, to: :provider_service
delegate :sync_templates, to: :provider_service
delegate :media_url, to: :provider_service
delegate :api_headers, to: :provider_service
@@ -18,9 +18,22 @@ class Conversations::TypingStatusManager
case params[:typing_status]
when 'on'
trigger_typing_event(CONVERSATION_TYPING_ON, params[:is_private])
send_whatsapp_typing_indicator if conversation.inbox.whatsapp? && conversation.inbox.channel.provider == 'whatsapp_cloud'
when 'off'
trigger_typing_event(CONVERSATION_TYPING_OFF, params[:is_private])
end
# Return the head :ok response from the controller
end
private
def send_whatsapp_typing_indicator
last_incoming_message = @conversation.messages&.incoming&.last
return if last_incoming_message.blank?
conversation.inbox.channel.send_typing_indicator(
last_incoming_message.conversation.contact_inbox.source_id,
last_incoming_message.source_id
)
end
end
@@ -23,6 +23,10 @@ class Whatsapp::Providers::BaseService
raise 'Overwrite this method in child class'
end
def send_typing_indicator(_phone_number, _message_id)
raise 'Overwrite this method in child class'
end
def validate_provider_config
raise 'Overwrite this method in child class'
end
@@ -157,4 +157,21 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
process_response(response)
end
def send_typing_indicator(_phone_number, message_id)
response = HTTParty.post(
"#{phone_id_path}/messages",
headers: api_headers,
body: {
messaging_product: 'whatsapp',
status: 'read',
message_id: message_id,
typing_indicator: {
type: 'text'
}
}.to_json
)
process_response(response)
end
end