chore: Provider API prototype (#3112)

Enabling Support for Whatsapp via 360Dialog as a prototype for the provider APIs. 

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2021-10-05 23:35:06 +05:30
committed by GitHub
parent 40d0b2faf3
commit bd7aeba484
31 changed files with 506 additions and 60 deletions

View File

@@ -9,12 +9,18 @@ class Contacts::ContactableInboxesService
private
def get_contactable_inbox(inbox)
return twilio_contactable_inbox(inbox) if inbox.channel_type == 'Channel::TwilioSms'
return email_contactable_inbox(inbox) if inbox.channel_type == 'Channel::Email'
return api_contactable_inbox(inbox) if inbox.channel_type == 'Channel::Api'
return website_contactable_inbox(inbox) if inbox.channel_type == 'Channel::WebWidget'
nil
case inbox.channel_type
when 'Channel::TwilioSms'
twilio_contactable_inbox(inbox)
when 'Channel::Whatsapp'
whatsapp_contactable_inbox(inbox)
when 'Channel::Email'
email_contactable_inbox(inbox)
when 'Channel::Api'
api_contactable_inbox(inbox)
when 'Channel::WebWidget'
website_contactable_inbox(inbox)
end
end
def website_contactable_inbox(inbox)
@@ -39,6 +45,13 @@ class Contacts::ContactableInboxesService
{ source_id: @contact.email, inbox: inbox }
end
def whatsapp_contactable_inbox(inbox)
return unless @contact.phone_number
# Remove the plus since thats the format 360 dialog uses
{ source_id: @contact.phone_number.delete('+'), inbox: inbox }
end
def twilio_contactable_inbox(inbox)
return if @contact.phone_number.blank?

View File

@@ -0,0 +1,61 @@
# Find the various telegram payload samples here: https://core.telegram.org/bots/webhooks#testing-your-bot-with-updates
# https://core.telegram.org/bots/api#available-types
class Whatsapp::IncomingMessageService
pattr_initialize [:inbox!, :params!]
def perform
set_contact
return unless @contact
set_conversation
return if params[:messages].blank?
@message = @conversation.messages.create(
content: params[:messages].first.dig(:text, :body),
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
sender: @contact,
source_id: params[:messages].first[:id].to_s
)
@message.save!
end
private
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
end

View File

@@ -0,0 +1,11 @@
class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
private
def channel_class
Channel::Whatsapp
end
def perform_reply
channel.send_message(message.conversation.contact_inbox.source_id, message.content)
end
end