Fix: Instagram webhook test event (#5317)
This commit is contained in:
parent
747ce9c80f
commit
9c67814724
4 changed files with 81 additions and 72 deletions
|
@ -12,13 +12,6 @@ class Webhooks::InstagramEventsJob < ApplicationJob
|
|||
def perform(entries)
|
||||
@entries = entries
|
||||
|
||||
if @entries[0].key?(:changes)
|
||||
Rails.logger.info('Probably Test data.')
|
||||
# grab the test entry for the review app
|
||||
create_test_text
|
||||
return
|
||||
end
|
||||
|
||||
@entries.each do |entry|
|
||||
entry[:messaging].each do |messaging|
|
||||
send(@event_name, messaging) if event_name(messaging)
|
||||
|
@ -35,52 +28,4 @@ class Webhooks::InstagramEventsJob < ApplicationJob
|
|||
def message(messaging)
|
||||
::Instagram::MessageText.new(messaging).perform
|
||||
end
|
||||
|
||||
def create_test_text
|
||||
messenger_channel = Channel::FacebookPage.last
|
||||
@inbox = ::Inbox.find_by(channel: messenger_channel)
|
||||
return unless @inbox
|
||||
|
||||
@contact_inbox = @inbox.contact_inboxes.where(source_id: 'sender_username').first
|
||||
unless @contact_inbox
|
||||
@contact_inbox ||= @inbox.channel.create_contact_inbox(
|
||||
'sender_username', 'sender_username'
|
||||
)
|
||||
end
|
||||
@contact = @contact_inbox.contact
|
||||
|
||||
@conversation ||= Conversation.find_by(conversation_params) || build_conversation(conversation_params)
|
||||
|
||||
@message = @conversation.messages.create!(message_params)
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
additional_attributes: {
|
||||
type: 'instagram_direct_message'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def message_params
|
||||
{
|
||||
account_id: @conversation.account_id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
message_type: 'incoming',
|
||||
source_id: 'facebook_test_webhooks',
|
||||
content: 'This is a test message from facebook.',
|
||||
sender: @contact
|
||||
}
|
||||
end
|
||||
|
||||
def build_conversation(conversation_params)
|
||||
Conversation.create!(
|
||||
conversation_params.merge(
|
||||
contact_inbox_id: @contact_inbox.id
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
|
|||
end
|
||||
|
||||
def perform
|
||||
create_test_text
|
||||
instagram_id, contact_id = if agent_message_via_echo?
|
||||
[@messaging[:sender][:id], @messaging[:recipient][:id]]
|
||||
else
|
||||
|
@ -56,6 +57,10 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
|
|||
@inbox.contact_inboxes.where(source_id: ig_scope_id).empty? && @inbox.channel.instagram_id.present?
|
||||
end
|
||||
|
||||
def sent_via_test_webhook?
|
||||
@messaging[:sender][:id] == '12334' && @messaging[:recipient][:id] == '23245'
|
||||
end
|
||||
|
||||
def unsend_message
|
||||
message_to_delete = @inbox.messages.find_by(
|
||||
source_id: @messaging[:message][:mid]
|
||||
|
@ -68,4 +73,65 @@ class Instagram::MessageText < Instagram::WebhooksBaseService
|
|||
def create_message
|
||||
Messages::Instagram::MessageBuilder.new(@messaging, @inbox, outgoing_echo: agent_message_via_echo?).perform
|
||||
end
|
||||
|
||||
def create_test_text
|
||||
return unless sent_via_test_webhook?
|
||||
|
||||
Rails.logger.info('Probably Test data.')
|
||||
|
||||
messenger_channel = Channel::FacebookPage.last
|
||||
@inbox = ::Inbox.find_by(channel: messenger_channel)
|
||||
return unless @inbox
|
||||
|
||||
@contact = create_test_contact
|
||||
|
||||
@conversation ||= create_test_conversation(conversation_params)
|
||||
|
||||
@message = @conversation.messages.create!(test_message_params)
|
||||
end
|
||||
|
||||
def create_test_contact
|
||||
@contact_inbox = @inbox.contact_inboxes.where(source_id: @messaging[:sender][:id]).first
|
||||
unless @contact_inbox
|
||||
@contact_inbox ||= @inbox.channel.create_contact_inbox(
|
||||
'sender_username', 'sender_username'
|
||||
)
|
||||
end
|
||||
|
||||
@contact_inbox.contact
|
||||
end
|
||||
|
||||
def create_test_conversation(conversation_params)
|
||||
Conversation.find_by(conversation_params) || build_conversation(conversation_params)
|
||||
end
|
||||
|
||||
def test_message_params
|
||||
{
|
||||
account_id: @conversation.account_id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
message_type: 'incoming',
|
||||
source_id: @messaging[:message][:mid],
|
||||
content: @messaging[:message][:text],
|
||||
sender: @contact
|
||||
}
|
||||
end
|
||||
|
||||
def build_conversation(conversation_params)
|
||||
Conversation.create!(
|
||||
conversation_params.merge(
|
||||
contact_inbox_id: @contact_inbox.id
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
additional_attributes: {
|
||||
type: 'instagram_direct_message'
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -30,23 +30,20 @@ FactoryBot.define do
|
|||
entry do
|
||||
[
|
||||
{
|
||||
'id': 'instagram-message-id-123',
|
||||
'time': '2021-09-08T06:34:04+0000',
|
||||
'changes': [
|
||||
'time' => 1_661_141_837_537,
|
||||
'id' => '0',
|
||||
'messaging' => [
|
||||
{
|
||||
'field': 'messages',
|
||||
'value': {
|
||||
'event_type': 'TEXT',
|
||||
'event_timestamp': '1527459824',
|
||||
'event_data': {
|
||||
'message_id': 'vcvacopiufqwehfawdnb',
|
||||
'sender': {
|
||||
'username': 'sender_username'
|
||||
},
|
||||
'recipient': {
|
||||
'thread_id': 'faeoqiehrkbfadsfawd'
|
||||
}
|
||||
}
|
||||
'sender' => {
|
||||
'id' => '12334'
|
||||
},
|
||||
'recipient' => {
|
||||
'id' => '23245'
|
||||
},
|
||||
'timestamp' => '1527459824',
|
||||
'message' => {
|
||||
'mid' => 'random_mid',
|
||||
'text' => 'random_text'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -51,7 +51,8 @@ describe Webhooks::InstagramEventsJob do
|
|||
|
||||
instagram_inbox.reload
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
expect(instagram_inbox.messages.last.content).to eq('This is a test message from facebook.')
|
||||
expect(instagram_inbox.messages.last.content).to eq('random_text')
|
||||
expect(instagram_inbox.messages.last.source_id).to eq('random_mid')
|
||||
end
|
||||
|
||||
it 'handle instagram unsend message event' do
|
||||
|
|
Loading…
Reference in a new issue