Feat: twitter image support (#4429)
This commit is contained in:
@@ -7,7 +7,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
set_inbox
|
||||
ensure_contacts
|
||||
set_conversation
|
||||
@conversation.messages.create(
|
||||
@message = @conversation.messages.create(
|
||||
content: message_create_data['message_data']['text'],
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
@@ -15,10 +15,24 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
sender: @contact,
|
||||
source_id: direct_message_data['id']
|
||||
)
|
||||
attach_files
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def attach_files
|
||||
return if message_create_data['message_data']['attachment'].blank?
|
||||
|
||||
save_media
|
||||
@message
|
||||
end
|
||||
|
||||
def save_media_urls(file)
|
||||
@message.content_attributes[:media_url] = file['media_url']
|
||||
@message.content_attributes[:display_url] = file['display_url']
|
||||
@message.save
|
||||
end
|
||||
|
||||
def direct_message_events_params
|
||||
payload['direct_message_events']
|
||||
end
|
||||
@@ -39,6 +53,10 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
ENV.fetch('TWITTER_APP_ID', '')
|
||||
end
|
||||
|
||||
def media
|
||||
message_create_data['message_data']['attachment']['media']
|
||||
end
|
||||
|
||||
def users
|
||||
payload[:users]
|
||||
end
|
||||
@@ -73,4 +91,36 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
def outgoing_message?
|
||||
message_create_data['sender_id'] == @inbox.channel.profile_id
|
||||
end
|
||||
|
||||
def api_client
|
||||
@api_client ||= begin
|
||||
consumer = OAuth::Consumer.new(ENV.fetch('TWITTER_CONSUMER_KEY', nil), ENV.fetch('TWITTER_CONSUMER_SECRET', nil),
|
||||
{ site: 'https://api.twitter.com' })
|
||||
token = { oauth_token: @inbox.channel.twitter_access_token, oauth_token_secret: @inbox.channel.twitter_access_token_secret }
|
||||
OAuth::AccessToken.from_hash(consumer, token)
|
||||
end
|
||||
end
|
||||
|
||||
def save_media
|
||||
save_media_urls(media)
|
||||
response = api_client.get(media['media_url'], [])
|
||||
|
||||
temp_file = Tempfile.new('twitter_attachment')
|
||||
temp_file.binmode
|
||||
temp_file << response.body
|
||||
temp_file.rewind
|
||||
|
||||
return unless media['type'] == 'photo'
|
||||
|
||||
@message.attachments.new(
|
||||
account_id: @inbox.account_id,
|
||||
file_type: 'image',
|
||||
file: {
|
||||
io: temp_file,
|
||||
filename: 'twitter_attachment',
|
||||
content_type: media['type']
|
||||
}
|
||||
)
|
||||
@message.save
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,7 +43,7 @@ class Twitter::WebhookSubscribeService
|
||||
|
||||
def register_webhook
|
||||
register_response = twitter_client.register_webhook(url: twitter_url)
|
||||
Rails.logger.info "TWITTER_UNREGISTER_WEBHOOK: #{register_response.body}"
|
||||
Rails.logger.info "TWITTER_REGISTER_WEBHOOK: #{register_response.body}"
|
||||
end
|
||||
|
||||
def subscription?
|
||||
|
||||
Reference in New Issue
Block a user