Skip to content

Commit

Permalink
fix create channel return true without connect/disconnect param
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Sep 11, 2024
1 parent 25ed37c commit 3fca3d5
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions app/services/whatsapp/unoapi_webhook_setup_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def perform(whatsapp_channel)
whatsapp_channel.provider_config.delete('disconnect')
return disconnect(whatsapp_channel)
end
return unless whatsapp_channel.provider_config['connect']
return true unless whatsapp_channel.provider_config['connect']

whatsapp_channel.provider_config.delete('connect')
whatsapp_channel.provider_config.delete('disconnect')
Expand All @@ -29,29 +29,7 @@ def disconnect(whatsapp_channel)
def connect(whatsapp_channel)
phone_number = whatsapp_channel.provider_config['business_account_id']
Rails.logger.debug { "Connecting #{phone_number} from unoapi" }
body = {
ignoreGroupMessages: whatsapp_channel.provider_config['ignore_group_messages'],
ignoreBroadcastStatuses: whatsapp_channel.provider_config['ignore_broadcast_statuses'],
ignoreBroadcastMessages: whatsapp_channel.provider_config['ignore_broadcast_messages'],
ignoreHistoryMessages: whatsapp_channel.provider_config['ignore_history_messages'],
ignoreOwnMessages: whatsapp_channel.provider_config['ignore_own_messages'],
ignoreYourselfMessages: whatsapp_channel.provider_config['ignore_yourself_messages'],
sendConnectionStatus: whatsapp_channel.provider_config['send_connection_status'],
notifyFailedMessages: whatsapp_channel.provider_config['notify_failed_messages'],
composingMessage: whatsapp_channel.provider_config['composing_message'],
rejectCalls: whatsapp_channel.provider_config['reject_calls'],
messageCallsWebhook: whatsapp_channel.provider_config['message_calls_webhook'],
webhooks: [
sendNewMessages: whatsapp_channel.provider_config['webhook_send_new_messages'],
id: 'default',
urlAbsolute: "#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/whatsapp/#{phone_number}",
token: whatsapp_channel.provider_config['webhook_verify_token'],
header: :Authorization
],
sendReactionAsReply: whatsapp_channel.provider_config['send_reaction_as_reply'],
sendProfilePicture: whatsapp_channel.provider_config['send_profile_picture'],
authToken: whatsapp_channel.provider_config['api_key']
}
body = params(provider_config, phone_number)
response = HTTParty.post("#{url(whatsapp_channel)}/register", headers: headers(whatsapp_channel), body: body.to_json)
Rails.logger.debug { "Response #{response}" }
return send_message(whatsapp_channel) if response.success?
Expand Down Expand Up @@ -90,4 +68,32 @@ def headers(whatsapp_channel)
'Content-Type': 'application/json'
}
end

# rubocop:disable Metrics/MethodLength
def params(provider_config, phone_number)
{
ignoreGroupMessages: provider_config['ignore_group_messages'],
ignoreBroadcastStatuses: provider_config['ignore_broadcast_statuses'],
ignoreBroadcastMessages: provider_config['ignore_broadcast_messages'],
ignoreHistoryMessages: provider_config['ignore_history_messages'],
ignoreOwnMessages: provider_config['ignore_own_messages'],
ignoreYourselfMessages: provider_config['ignore_yourself_messages'],
sendConnectionStatus: provider_config['send_connection_status'],
notifyFailedMessages: provider_config['notify_failed_messages'],
composingMessage: provider_config['composing_message'],
rejectCalls: provider_config['reject_calls'],
messageCallsWebhook: provider_config['message_calls_webhook'],
webhooks: [
sendNewMessages: provider_config['webhook_send_new_messages'],
id: 'default',
urlAbsolute: "#{ENV.fetch('FRONTEND_URL', nil)}/webhooks/whatsapp/#{phone_number}",
token: provider_config['webhook_verify_token'],
header: :Authorization
],
sendReactionAsReply: provider_config['send_reaction_as_reply'],
sendProfilePicture: provider_config['send_profile_picture'],
authToken: provider_config['api_key']
}
end
# rubocop:enable Metrics/MethodLength
end

0 comments on commit 3fca3d5

Please sign in to comment.