Feature: Twitter DM Integration (#451)

An initial version of twitter integration

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Pranav Raj S
2020-02-03 00:39:00 +05:30
committed by GitHub
parent a287c86bc4
commit a9c304f1ef
20 changed files with 406 additions and 49 deletions

View File

@@ -3,7 +3,7 @@ class Api::V1::WebhooksController < ApplicationController
skip_before_action :set_current_user
skip_before_action :check_subscription
before_action :login_from_basic_auth
before_action :login_from_basic_auth, only: [:chargebee]
def chargebee
chargebee_consumer.consume
head :ok
@@ -12,6 +12,18 @@ class Api::V1::WebhooksController < ApplicationController
head :ok
end
def twitter_crc
render json: { response_token: "sha256=#{$twitter.generate_crc(params[:crc_token])}" }
end
def twitter_events
twitter_consumer.consume
head :ok
rescue StandardError => e
Raven.capture_exception(e)
head :ok
end
private
def login_from_basic_auth
@@ -21,6 +33,10 @@ class Api::V1::WebhooksController < ApplicationController
end
def chargebee_consumer
@consumer ||= ::Webhooks::Chargebee.new(params)
@chargebee_consumer ||= ::Webhooks::Chargebee.new(params)
end
def twitter_consumer
@twitter_consumer ||= ::Webhooks::Twitter.new(params)
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -27,6 +27,13 @@
class="source-badge user--online"
:style="statusStyle"
></div>
<img
v-if="badge === 'Channel::TwitterProfile' && status !== ''"
id="badge"
class="source-badge"
:style="badgeStyle"
src="~dashboard/assets/images/twitter-badge.png"
/>
</div>
</template>
<script>

View File

@@ -0,0 +1,49 @@
# == Schema Information
#
# Table name: channel_twitter_profiles
#
# id :bigint not null, primary key
# name :string
# twitter_access_token :string not null
# twitter_access_token_secret :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# profile_id :string not null
#
class Channel::TwitterProfile < ApplicationRecord
self.table_name = 'channel_twitter_profiles'
validates :account_id, presence: true
validates :profile_id, uniqueness: { scope: :account_id }
has_one_attached :avatar
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
before_destroy :unsubscribe
def name
'Twitter'
end
def create_contact_inbox(profile_id, name)
ActiveRecord::Base.transaction do
contact = inbox.account.contacts.create!(name: name)
::ContactInbox.create!(
contact_id: contact.id,
inbox_id: inbox.id,
source_id: profile_id
)
rescue StandardError => e
Rails.logger e
end
end
private
def unsubscribe
# to implement
end
end

View File

@@ -82,7 +82,12 @@ class Message < ApplicationRecord
end
def send_reply
::Facebook::SendReplyService.new(message: self).perform
channel_name = conversation.inbox.channel.class.to_s
if channel_name == 'Channel::FacebookPage'
::Facebook::SendReplyService.new(message: self).perform
elsif channel_name == 'Channel::TwitterProfile'
::Twitter::SendReplyService.new(message: self).perform
end
end
def reopen_conversation

View File

@@ -0,0 +1,24 @@
class Twitter::SendReplyService
pattr_initialize [:message!]
def perform
return if message.private
return if inbox.channel.class.to_s != 'Channel::TwitterProfile'
return unless outgoing_message_from_chatwoot?
$twitter.send_direct_message(
recipient_id: contact_inbox.source_id,
message: message.content
)
end
private
def outgoing_message_from_chatwoot?
message.outgoing?
end
delegate :contact_inbox, to: :conversation
delegate :conversation, to: :message
delegate :inbox, to: :conversation
end