2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
class ChannelsSmsController < ApplicationController
|
|
|
|
prepend_before_action -> { authentication_check && authorize! }, except: [:webhook]
|
2018-10-16 08:45:15 +00:00
|
|
|
skip_before_action :verify_csrf_token, only: [:webhook]
|
|
|
|
|
|
|
|
def index
|
|
|
|
assets = {}
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
account_channel_ids: channels_data('Sms::Account', assets),
|
2018-10-16 08:45:15 +00:00
|
|
|
notification_channel_ids: channels_data('Sms::Notification', assets),
|
2018-12-19 17:31:51 +00:00
|
|
|
config: channels_config,
|
|
|
|
assets: assets
|
2018-10-16 08:45:15 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
def show
|
|
|
|
model_show_render(Channel, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
model_create_render(Channel, channel_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
model_update_render(Channel, channel_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable
|
|
|
|
channel.update!(active: true)
|
|
|
|
render json: channel
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
|
|
|
channel.update!(active: false)
|
|
|
|
render json: channel
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
channel.destroy!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
2018-10-16 08:45:15 +00:00
|
|
|
def test
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Missing parameter options.adapter') if params[:options][:adapter].blank?
|
2018-10-16 08:45:15 +00:00
|
|
|
|
|
|
|
driver = Channel.driver_class(params[:options][:adapter])
|
|
|
|
resp = driver.new.send(params[:options], test_options)
|
|
|
|
|
|
|
|
render json: { success: resp }
|
|
|
|
rescue => e
|
|
|
|
render json: { error: e.inspect, error_human: e.message }
|
|
|
|
end
|
|
|
|
|
|
|
|
def webhook
|
|
|
|
raise Exceptions::UnprocessableEntity, 'token param missing' if params['token'].blank?
|
|
|
|
|
2021-08-20 03:36:30 +00:00
|
|
|
ApplicationHandleInfo.in_context('sms') do
|
|
|
|
channel = nil
|
|
|
|
Channel.where(active: true, area: 'Sms::Account').each do |local_channel|
|
|
|
|
next if local_channel.options[:webhook_token] != params['token']
|
|
|
|
|
|
|
|
channel = local_channel
|
|
|
|
end
|
|
|
|
if !channel
|
|
|
|
render(
|
|
|
|
json: { message: 'channel not found' },
|
|
|
|
status: :not_found
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
content_type, content = channel.process(params.permit!.to_h)
|
|
|
|
send_data content, type: content_type
|
2018-10-16 08:45:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-19 09:39:51 +00:00
|
|
|
def channel
|
|
|
|
@channel ||= Channel.lookup(id: params[:id])
|
|
|
|
end
|
|
|
|
|
2018-10-16 08:45:15 +00:00
|
|
|
def test_options
|
|
|
|
params.permit(:recipient, :message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def channel_params
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Missing area params') if params[:area].blank?
|
2020-03-19 09:39:51 +00:00
|
|
|
if ['Sms::Notification', 'Sms::Account'].exclude?(params[:area])
|
2018-10-16 08:45:15 +00:00
|
|
|
raise "Invalid area '#{params[:area]}'!"
|
|
|
|
end
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Missing options params') if params[:options].blank?
|
|
|
|
raise __('Missing options.adapter params') if params[:options][:adapter].blank?
|
2018-10-16 08:45:15 +00:00
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def channels_config
|
|
|
|
list = []
|
2020-02-18 19:51:31 +00:00
|
|
|
Dir.glob(Rails.root.join('app/models/channel/driver/sms/*.rb')).each do |path|
|
2018-10-16 08:45:15 +00:00
|
|
|
filename = File.basename(path)
|
2021-08-16 14:52:36 +00:00
|
|
|
next if !Channel.driver_class("sms/#{filename}").const_defined?(:NAME)
|
|
|
|
|
2018-10-16 08:45:15 +00:00
|
|
|
list.push Channel.driver_class("sms/#{filename}").definition
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
def channels_data(area, assets)
|
|
|
|
channel_ids = []
|
|
|
|
Channel.where(area: area).each do |channel|
|
|
|
|
assets = channel.assets(assets)
|
|
|
|
channel_ids.push(channel.id)
|
|
|
|
end
|
|
|
|
channel_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|