Feature: Create conversations from Tweets (#470)

* Feature: Add tweets to conversations
This commit is contained in:
Pranav Raj S
2020-02-09 15:47:48 +05:30
committed by GitHub
parent 3465ebefd1
commit 272c481464
29 changed files with 333 additions and 68 deletions

View File

@@ -22,8 +22,8 @@ class Facebook::SendReplyService
end
def outgoing_message_from_chatwoot?
# messages sent directly from chatwoot won't have fb_id.
message.outgoing? && !message.fb_id
# messages sent directly from chatwoot won't have source_id.
message.outgoing? && !message.source_id
end
# def reopen_lock

View File

@@ -3,22 +3,60 @@ class Twitter::SendReplyService
def perform
return if message.private
return if message.source_id
return if inbox.channel.class.to_s != 'Channel::TwitterProfile'
return unless outgoing_message_from_chatwoot?
$twitter.send_direct_message(
send_reply
end
private
def twitter_client
Twitty::Facade.new do |config|
config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY', nil)
config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET', nil)
config.access_token = channel.twitter_access_token
config.access_token_secret = channel.twitter_access_token_secret
config.base_url = 'https://api.twitter.com'
config.environment = ENV.fetch('TWITTER_ENVIRONMENT', '')
end
end
def conversation_type
conversation.additional_attributes['type']
end
def screen_name
"@#{additional_attributes ? additional_attributes['screen_name'] : ''} "
end
def send_direct_message
twitter_client.send_direct_message(
recipient_id: contact_inbox.source_id,
message: message.content
)
end
private
def send_tweet_reply
twitter_client.send_tweet_reply(
reply_to_tweet_id: conversation.additional_attributes['tweet_id'],
tweet: screen_name + message.content
)
end
def send_reply
conversation_type == 'tweet' ? send_tweet_reply : send_direct_message
end
def outgoing_message_from_chatwoot?
message.outgoing?
end
delegate :additional_attributes, to: :contact
delegate :contact, to: :conversation
delegate :contact_inbox, to: :conversation
delegate :conversation, to: :message
delegate :inbox, to: :conversation
delegate :channel, to: :inbox
end

View File

@@ -0,0 +1,69 @@
class Twitter::TweetParserService < Twitter::WebhooksBaseService
pattr_initialize [:payload]
def perform
set_inbox
find_or_create_contact(user)
set_conversation
@conversation.messages.create(
account_id: @inbox.account_id,
contact_id: @contact.id,
content: tweet_text,
inbox_id: @inbox.id,
message_type: message_type,
source_id: tweet_data['id'].to_s
)
end
private
def message_type
user['id'] == profile_id ? :outgoing : :incoming
end
def tweet_text
tweet_data['truncated'] ? tweet_data['extended_tweet']['full_text'] : tweet_data['text']
end
def tweet_create_events_params
payload['tweet_create_events']
end
def tweet_data
tweet_create_events_params.first
end
def user
tweet_data['user']
end
def parent_tweet_id
tweet_data['in_reply_to_status_id_str'].nil? ? tweet_data['id'].to_s : tweet_data['in_reply_to_status_id_str']
end
def conversation_params
{
account_id: @inbox.account_id,
inbox_id: @inbox.id,
contact_id: @contact.id,
contact_inbox_id: @contact_inbox.id,
additional_attributes: {
type: 'tweet',
tweet_id: parent_tweet_id,
tweet_source: tweet_data['source']
}
}
end
def set_conversation
tweet_conversations = @contact_inbox.conversations.where("additional_attributes ->> 'tweet_id' = ?", parent_tweet_id)
@conversation = tweet_conversations.first
return if @conversation
tweet_message = @inbox.messages.find_by(source_id: parent_tweet_id)
@conversation = tweet_message.conversation if tweet_message
return if @conversation
@conversation = ::Conversation.create!(conversation_params)
end
end

View File

@@ -5,6 +5,17 @@ class Twitter::WebhooksBaseService
payload[:for_user_id]
end
def additional_contact_attributes(user)
{
screen_name: user['screen_name'],
location: user['location'],
url: user['url'],
description: user['description'],
followers_count: user['followers_count'],
friends_count: user['friends_count']
}
end
def set_inbox
twitter_profile = ::Channel::TwitterProfile.find_by(profile_id: profile_id)
@inbox = ::Inbox.find_by!(channel: twitter_profile)
@@ -15,7 +26,9 @@ class Twitter::WebhooksBaseService
@contact = @contact_inbox.contact if @contact_inbox
return if @contact
@contact_inbox = @inbox.channel.create_contact_inbox(user['id'], user['name'])
@contact_inbox = @inbox.channel.create_contact_inbox(
user['id'], user['name'], additional_contact_attributes(user)
)
@contact = @contact_inbox.contact
avatar_resource = LocalResource.new(user['profile_image_url'])
@contact.avatar.attach(