2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2017-02-15 02:35:22 +00:00
|
|
|
|
|
|
|
class ChannelsTelegramController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action -> { authentication_check && authorize! }, except: [:webhook]
|
2017-02-15 12:29:25 +00:00
|
|
|
skip_before_action :verify_csrf_token, only: [:webhook]
|
2017-02-15 02:35:22 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
assets = {}
|
|
|
|
channel_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Channel.where(area: 'Telegram::Bot').order(:id).each do |channel|
|
2017-02-15 02:35:22 +00:00
|
|
|
assets = channel.assets(assets)
|
|
|
|
channel_ids.push channel.id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-02-15 02:35:22 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
assets: assets,
|
2017-02-15 02:35:22 +00:00
|
|
|
channel_ids: channel_ids
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add
|
|
|
|
begin
|
|
|
|
channel = Telegram.create_or_update_channel(params[:api_token], params)
|
|
|
|
rescue => e
|
|
|
|
raise Exceptions::UnprocessableEntity, e.message
|
|
|
|
end
|
|
|
|
render json: channel
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Telegram::Bot')
|
|
|
|
begin
|
|
|
|
channel = Telegram.create_or_update_channel(params[:api_token], params, channel)
|
|
|
|
rescue => e
|
|
|
|
raise Exceptions::UnprocessableEntity, e.message
|
|
|
|
end
|
|
|
|
render json: channel
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Telegram::Bot')
|
|
|
|
channel.active = true
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Telegram::Bot')
|
|
|
|
channel.active = false
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Telegram::Bot')
|
|
|
|
channel.destroy
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def webhook
|
2019-08-12 15:25:32 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'bot id is missing' if params['bid'].blank?
|
2017-02-15 02:35:22 +00:00
|
|
|
|
|
|
|
channel = Telegram.bot_by_bot_id(params['bid'])
|
|
|
|
raise Exceptions::UnprocessableEntity, 'bot not found' if !channel
|
|
|
|
|
|
|
|
if channel.options[:callback_token] != params['callback_token']
|
|
|
|
raise Exceptions::UnprocessableEntity, 'invalid callback token'
|
|
|
|
end
|
|
|
|
|
|
|
|
telegram = Telegram.new(channel.options[:api_token])
|
2019-10-21 14:50:45 +00:00
|
|
|
begin
|
|
|
|
telegram.to_group(params, channel.group_id, channel)
|
|
|
|
rescue Exceptions::UnprocessableEntity => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
end
|
2017-02-15 02:35:22 +00:00
|
|
|
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|