Some checks failed
Lock Threads / action (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
- Add Logistics component with progress tracking - Add OrderDetail component for order information - Support data-driven steps and actions - Add blue color scale to widget SCSS - Fix node overflow and progress bar rendering issues - Add English translations for dashboard components Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
100 lines
2.7 KiB
Ruby
100 lines
2.7 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: channel_sms
|
|
#
|
|
# id :bigint not null, primary key
|
|
# phone_number :string not null
|
|
# provider :string default("default")
|
|
# provider_config :jsonb
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# account_id :integer not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_channel_sms_on_phone_number (phone_number) UNIQUE
|
|
#
|
|
|
|
class Channel::Sms < ApplicationRecord
|
|
include Channelable
|
|
|
|
self.table_name = 'channel_sms'
|
|
EDITABLE_ATTRS = [:phone_number, { provider_config: {} }].freeze
|
|
|
|
validates :phone_number, presence: true, uniqueness: true
|
|
# before_save :validate_provider_config
|
|
|
|
def name
|
|
'Sms'
|
|
end
|
|
|
|
# all this should happen in provider service . but hack mode on
|
|
def api_base_path
|
|
'https://messaging.bandwidth.com/api/v2'
|
|
end
|
|
|
|
def send_message(contact_number, message)
|
|
body = message_body(contact_number, message.outgoing_content)
|
|
body['media'] = message.attachments.map(&:download_url) if message.attachments.present?
|
|
|
|
send_to_bandwidth(body, message)
|
|
end
|
|
|
|
def send_text_message(contact_number, message_content)
|
|
body = message_body(contact_number, message_content)
|
|
send_to_bandwidth(body)
|
|
end
|
|
|
|
private
|
|
|
|
def message_body(contact_number, message_content)
|
|
{
|
|
'to' => contact_number,
|
|
'from' => phone_number,
|
|
'text' => message_content,
|
|
'applicationId' => provider_config['application_id']
|
|
}
|
|
end
|
|
|
|
def send_to_bandwidth(body, message = nil)
|
|
response = HTTParty.post(
|
|
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
|
|
basic_auth: bandwidth_auth,
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
body: body.to_json
|
|
)
|
|
|
|
if response.success?
|
|
response.parsed_response['id']
|
|
else
|
|
handle_error(response, message)
|
|
nil
|
|
end
|
|
end
|
|
|
|
def handle_error(response, message)
|
|
Rails.logger.error("[#{account_id}] Error sending SMS: #{response.parsed_response['description']}")
|
|
return if message.blank?
|
|
|
|
# https://dev.bandwidth.com/apis/messaging-apis/messaging/#tag/Messages/operation/createMessage
|
|
message.external_error = response.parsed_response['description']
|
|
message.status = :failed
|
|
message.save!
|
|
end
|
|
|
|
def bandwidth_auth
|
|
{ username: provider_config['api_key'], password: provider_config['api_secret'] }
|
|
end
|
|
|
|
# Extract later into provider Service
|
|
# let's revisit later
|
|
def validate_provider_config
|
|
response = HTTParty.post(
|
|
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
|
|
basic_auth: bandwidth_auth,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
)
|
|
errors.add(:provider_config, 'error setting up') unless response.success?
|
|
end
|
|
end
|