feat: Line Channel (#2904)

- Ability to configure line bots as a channel in chatwoot
- Receive a message sent to the line bot in chatwoot
- Ability to reply to line users from chatwoot

fixes: #2738
This commit is contained in:
Sojan Jose
2021-09-11 01:31:17 +05:30
committed by GitHub
parent 671c5c931f
commit 0a38632f14
37 changed files with 581 additions and 56 deletions

View File

@@ -51,6 +51,7 @@ class Account < ApplicationRecord
has_many :web_widgets, dependent: :destroy, class_name: '::Channel::WebWidget'
has_many :email_channels, dependent: :destroy, class_name: '::Channel::Email'
has_many :api_channels, dependent: :destroy, class_name: '::Channel::Api'
has_many :line_channels, dependent: :destroy, class_name: '::Channel::Line'
has_many :telegram_channels, dependent: :destroy, class_name: '::Channel::Telegram'
has_many :canned_responses, dependent: :destroy
has_many :webhooks, dependent: :destroy

View File

@@ -18,22 +18,15 @@
#
class Channel::Api < ApplicationRecord
include Channelable
self.table_name = 'channel_api'
EDITABLE_ATTRS = [:webhook_url].freeze
validates :account_id, presence: true
belongs_to :account
has_secure_token :identifier
has_secure_token :hmac_token
has_one :inbox, as: :channel, dependent: :destroy
def name
'API'
end
def has_24_hour_messaging_window?
false
end
end

View File

@@ -16,25 +16,20 @@
#
class Channel::Email < ApplicationRecord
include Channelable
self.table_name = 'channel_email'
EDITABLE_ATTRS = [:email].freeze
validates :account_id, presence: true
belongs_to :account
validates :email, uniqueness: true
validates :forward_to_email, uniqueness: true
has_one :inbox, as: :channel, dependent: :destroy
before_validation :ensure_forward_to_email, on: :create
def name
'Email'
end
def has_24_hour_messaging_window?
false
end
private
def ensure_forward_to_email

View File

@@ -17,15 +17,12 @@
#
class Channel::FacebookPage < ApplicationRecord
self.table_name = 'channel_facebook_pages'
include Channelable
include Reauthorizable
validates :account_id, presence: true
validates :page_id, uniqueness: { scope: :account_id }
belongs_to :account
self.table_name = 'channel_facebook_pages'
has_one :inbox, as: :channel, dependent: :destroy
validates :page_id, uniqueness: { scope: :account_id }
after_create_commit :subscribe
before_destroy :unsubscribe

View File

@@ -0,0 +1,39 @@
# == Schema Information
#
# Table name: channel_line
#
# id :bigint not null, primary key
# line_channel_secret :string not null
# line_channel_token :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# line_channel_id :string not null
#
# Indexes
#
# index_channel_line_on_line_channel_id (line_channel_id) UNIQUE
#
class Channel::Line < ApplicationRecord
include Channelable
self.table_name = 'channel_line'
EDITABLE_ATTRS = [:line_channel_id, :line_channel_secret, :line_channel_token].freeze
validates :line_channel_id, uniqueness: true, presence: true
validates :line_channel_secret, presence: true
validates :line_channel_token, presence: true
def name
'LINE'
end
def client
@client ||= Line::Bot::Client.new do |config|
config.channel_id = line_channel_id
config.channel_secret = line_channel_secret
config.channel_token = line_channel_token
end
end
end

View File

@@ -15,14 +15,12 @@
#
class Channel::Telegram < ApplicationRecord
include Channelable
self.table_name = 'channel_telegram'
EDITABLE_ATTRS = [:bot_token].freeze
has_one :inbox, as: :channel, dependent: :destroy
belongs_to :account
before_validation :ensure_valid_bot_token, on: :create
validates :account_id, presence: true
validates :bot_token, presence: true, uniqueness: true
before_save :setup_telegram_webhook
@@ -30,10 +28,6 @@ class Channel::Telegram < ApplicationRecord
'Telegram'
end
def has_24_hour_messaging_window?
false
end
def telegram_api_url
"https://api.telegram.org/bot#{bot_token}"
end

View File

@@ -17,19 +17,16 @@
#
class Channel::TwilioSms < ApplicationRecord
include Channelable
self.table_name = 'channel_twilio_sms'
validates :account_id, presence: true
validates :account_sid, presence: true
validates :auth_token, presence: true
validates :phone_number, uniqueness: { scope: :account_id }, presence: true
enum medium: { sms: 0, whatsapp: 1 }
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
def name
medium == 'sms' ? 'Twilio SMS' : 'Whatsapp'
end

View File

@@ -16,13 +16,11 @@
#
class Channel::TwitterProfile < ApplicationRecord
include Channelable
self.table_name = 'channel_twitter_profiles'
validates :account_id, presence: true
validates :profile_id, uniqueness: { scope: :account_id }
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
before_destroy :unsubscribe
@@ -30,10 +28,6 @@ class Channel::TwitterProfile < ApplicationRecord
'Twitter'
end
def has_24_hour_messaging_window?
false
end
def create_contact_inbox(profile_id, name, additional_attributes)
ActiveRecord::Base.transaction do
contact = inbox.account.contacts.create!(additional_attributes: additional_attributes, name: name)

View File

@@ -25,7 +25,9 @@
#
class Channel::WebWidget < ApplicationRecord
include Channelable
include FlagShihTzu
self.table_name = 'channel_web_widgets'
EDITABLE_ATTRS = [:website_url, :widget_color, :welcome_title, :welcome_tagline, :reply_time, :pre_chat_form_enabled,
{ pre_chat_form_options: [:pre_chat_message, :require_email] },
@@ -34,8 +36,6 @@ class Channel::WebWidget < ApplicationRecord
validates :website_url, presence: true
validates :widget_color, presence: true
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
has_secure_token :website_token
has_secure_token :hmac_token
@@ -50,10 +50,6 @@ class Channel::WebWidget < ApplicationRecord
'Website'
end
def has_24_hour_messaging_window?
false
end
def web_widget_script
"
<script>

View File

@@ -0,0 +1,12 @@
module Channelable
extend ActiveSupport::Concern
included do
validates :account_id, presence: true
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
end
def has_24_hour_messaging_window?
false
end
end

View File

@@ -93,6 +93,15 @@ class Inbox < ApplicationRecord
}
end
def webhook_url
case channel_type
when 'Channel::TwilioSMS'
"#{ENV['FRONTEND_URL']}/twilio/callback"
when 'Channel::Line'
"#{ENV['FRONTEND_URL']}/webhooks/line/#{channel.line_channel_id}"
end
end
private
def delete_round_robin_agents