Initial Commit

Co-authored-by: Subin <subinthattaparambil@gmail.com>
Co-authored-by: Manoj <manojmj92@gmail.com>
Co-authored-by: Nithin <webofnithin@gmail.com>
This commit is contained in:
Pranav Raj Sreepuram
2019-08-14 15:18:44 +05:30
commit 2a34255e0b
537 changed files with 27318 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
class Integrations::Facebook::DeliveryStatus
def initialize(params)
@params = params
end
def perform
update_message_status
end
private
def sender_id
@params.sender['id']
end
def conversation
@conversation ||= Conversation.find_by(sender_id: sender_id)
end
def update_message_status
conversation.user_last_seen_at = @params.at
conversation.save!
end
end

View File

@@ -0,0 +1,43 @@
class Integrations::Facebook::MessageCreator
attr_reader :response
def initialize(response)
@response = response
end
def perform
#begin
if outgoing_message_via_echo?
create_outgoing_message
else
create_incoming_message
end
#rescue => e
#Raven.capture_exception(e)
#end
end
private
def outgoing_message_via_echo?
response.echo? && !response.sent_from_chatwoot_app?
#this means that it is an outgoing message from page, but not sent from chatwoot.
#User can send from fb page directly on mobile messenger, so this case should be handled as outgoing message
end
def create_outgoing_message
FacebookPage.where(page_id: response.sender_id).each do |page|
mb = Messages::Outgoing::EchoBuilder.new(response, page.inbox, true)
mb.perform
end
end
def create_incoming_message
FacebookPage.where(page_id: response.recipient_id).each do |page|
mb = Messages::IncomingMessageBuilder.new(response, page.inbox)
mb.perform
end
end
end

View File

@@ -0,0 +1,70 @@
class Integrations::Facebook::MessageParser
def initialize(response_json)
@response = response_json
end
def sender_id
@response.sender["id"]
end
def recipient_id
@response.recipient["id"]
end
def time_stamp
@response.sent_at
end
def content
@response.text
end
def sequence
@response.seq
end
def attachments
@response.attachments
end
def identifier
@response.id
end
def echo?
@response.echo?
end
def app_id
@response.app_id
end
def sent_from_chatwoot_app?
app_id && app_id == ENV['fb_app_id'].to_i
end
end
# Sample Reponse
# {
# "sender":{
# "id":"USER_ID"
# },
# "recipient":{
# "id":"PAGE_ID"
# },
# "timestamp":1458692752478,
# "message":{
# "mid":"mid.1457764197618:41d102a3e1ae206a38",
# "seq":73,
# "text":"hello, world!",
# "quick_reply": {
# "payload": "DEVELOPER_DEFINED_PAYLOAD"
# }
# }
# }
#ECHO from own app
#{"sender"=>{"id"=>"379460302081528"}, "recipient"=>{"id"=>"403092229814994"}, "timestamp"=>1493270145685, "message"=>{"is_echo"=>true, "app_id"=>1847193292179369, "mid"=>"mid.$cAAED7rlYNsVh3xtylVbrdYqH8iEp", "seq"=>167221, "text"=>"sad"}}

View File

@@ -0,0 +1,61 @@
class Integrations::Widget::IncomingMessageBuilder
# params = {
# contact_id: 1,
# inbox_id: 1,
# content: "Hello world"
# }
attr_accessor :options, :message
def initialize(options)
@options = options
end
def perform
ActiveRecord::Base.transaction do
build_conversation
build_message
end
end
private
def inbox
@inbox ||= Inbox.find(options[:inbox_id])
end
def contact
@contact ||= Contact.find(options[:contact_id])
end
def build_conversation
@conversation ||=
if (conversation = Conversation.find_by(conversation_params))
conversation
else
Conversation.create!(conversation_params)
end
end
def build_message
@message = @conversation.messages.new(message_params)
@message.save!
end
def conversation_params
{
account_id: inbox.account_id,
inbox_id: inbox.id,
sender_id: options[:contact_id]
}
end
def message_params
{
account_id: @conversation.account_id,
inbox_id: @conversation.inbox_id,
message_type: 0,
content: options[:content],
}
end
end

View File

@@ -0,0 +1,50 @@
class Integrations::Widget::OutgoingMessageBuilder
# params = {
# user_id: 1,
# inbox_id: 1,
# content: "Hello world",
# conversation_id: 2
# }
attr_accessor :options, :message
def initialize(options)
@options = options
end
def perform
ActiveRecord::Base.transaction do
build_conversation
build_message
end
end
private
def inbox
@inbox ||= Inbox.find(options[:inbox_id])
end
def user
@user ||= Contact.find(options[:user_id])
end
def build_conversation
@conversation ||= Conversation.find(options[:conversation_id])
end
def build_message
@message = @conversation.messages.new(message_params)
@message.save!
end
def message_params
{
account_id: @conversation.account_id,
inbox_id: @conversation.inbox_id,
message_type: 1,
content: options[:content],
user_id: user.id
}
end
end