From 92b38dcb75991b45e0cf578dc266f1cb7c5727f7 Mon Sep 17 00:00:00 2001 From: Denny Bresch Date: Mon, 12 Aug 2019 17:25:32 +0200 Subject: [PATCH] Fixes #2695 - Telegram not possible to close ticket via '/end'. --- .../app/controllers/_channel/telegram.coffee | 38 +++++++++---------- .../app/views/telegram/bot_add.jst.eco | 8 ++++ .../app/views/telegram/bot_edit.jst.eco | 8 ++++ .../channels_telegram_controller.rb | 2 +- lib/telegram.rb | 15 ++++++-- spec/requests/integration/telegram_spec.rb | 14 +++---- 6 files changed, 55 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_channel/telegram.coffee b/app/assets/javascripts/app/controllers/_channel/telegram.coffee index 24918f3c3..7b60054af 100644 --- a/app/assets/javascripts/app/controllers/_channel/telegram.coffee +++ b/app/assets/javascripts/app/controllers/_channel/telegram.coffee @@ -1,11 +1,11 @@ class Index extends App.ControllerSubContent requiredPermission: 'admin.channel_telegram' events: - 'click .js-new': 'new' - 'click .js-edit': 'edit' - 'click .js-delete': 'delete' - 'click .js-disable': 'disable' - 'click .js-enable': 'enable' + 'click .js-new': 'new' + 'click .js-edit': 'edit' + 'click .js-delete': 'delete' + 'click .js-disable': 'disable' + 'click .js-enable': 'enable' constructor: -> super @@ -114,14 +114,14 @@ class BotAdd extends App.ControllerModal content = $(App.view('telegram/bot_add')()) createGroupSelection = (selected_id) -> return App.UiElement.select.render( - name: 'group_id' - multiple: false - limit: 100 - null: false - relation: 'Group' + name: 'group_id' + multiple: false + limit: 100 + null: false + relation: 'Group' nulloption: true - value: selected_id - class: 'form-control--small' + value: selected_id + class: 'form-control--small' ) content.find('.js-select').on('click', (e) => @@ -163,14 +163,14 @@ class BotEdit extends App.ControllerModal createGroupSelection = (selected_id) -> return App.UiElement.select.render( - name: 'group_id' - multiple: false - limit: 100 - null: false - relation: 'Group' + name: 'group_id' + multiple: false + limit: 100 + null: false + relation: 'Group' nulloption: true - value: selected_id - class: 'form-control--small' + value: selected_id + class: 'form-control--small' ) content.find('.js-messagesGroup').replaceWith createGroupSelection(@channel.group_id) diff --git a/app/assets/javascripts/app/views/telegram/bot_add.jst.eco b/app/assets/javascripts/app/views/telegram/bot_add.jst.eco index 4812210d4..07c9860ba 100644 --- a/app/assets/javascripts/app/views/telegram/bot_add.jst.eco +++ b/app/assets/javascripts/app/views/telegram/bot_add.jst.eco @@ -21,6 +21,14 @@ +
+
+ +
+
+ +
+
diff --git a/app/assets/javascripts/app/views/telegram/bot_edit.jst.eco b/app/assets/javascripts/app/views/telegram/bot_edit.jst.eco index fe6468d9a..b90493f1e 100644 --- a/app/assets/javascripts/app/views/telegram/bot_edit.jst.eco +++ b/app/assets/javascripts/app/views/telegram/bot_edit.jst.eco @@ -18,6 +18,14 @@
+
+
+ +
+
+ +
+
diff --git a/app/controllers/channels_telegram_controller.rb b/app/controllers/channels_telegram_controller.rb index 1cd5edaab..b73cf280e 100644 --- a/app/controllers/channels_telegram_controller.rb +++ b/app/controllers/channels_telegram_controller.rb @@ -57,7 +57,7 @@ class ChannelsTelegramController < ApplicationController end def webhook - raise Exceptions::UnprocessableEntity, 'bot params missing' if params['bid'].blank? + raise Exceptions::UnprocessableEntity, 'bot id is missing' if params['bid'].blank? channel = Telegram.bot_by_bot_id(params['bid']) raise Exceptions::UnprocessableEntity, 'bot not found' if !channel diff --git a/lib/telegram.rb b/lib/telegram.rb index 59530714a..10f638d59 100644 --- a/lib/telegram.rb +++ b/lib/telegram.rb @@ -109,6 +109,7 @@ returns callback_url: callback_url, api_token: token, welcome: params[:welcome], + goodbye: params[:goodbye], } channel.group_id = group.id channel.active = true @@ -654,9 +655,17 @@ returns # find ticket and close it elsif text.present? && text =~ %r{^/end} user = to_user(params) - ticket = Ticket.where(customer_id: user.id).order(:updated_at).first - ticket.state = Ticket::State.find_by(name: 'closed') + + # get the last ticket of customer which is not closed yet, and close it + state_ids = Ticket::State.where(name: %w[closed merged removed]).pluck(:id) + possible_tickets = Ticket.where(customer_id: user.id).where.not(state_id: state_ids).order(:updated_at) + ticket = possible_tickets.find_each.find { |possible_ticket| possible_ticket.preferences[:channel_id] == channel.id } + ticket.state = Ticket::State.find_by(name: 'closed') ticket.save! + + return if !channel.options[:goodbye] + + message(params[:message][:chat][:id], channel.options[:goodbye]) return end @@ -664,7 +673,7 @@ returns # use transaction Transaction.execute(reset_user_id: true) do - user = to_user(params) + user = to_user(params) ticket = to_ticket(params, user, group_id, channel) to_article(params, user, ticket, channel) end diff --git a/spec/requests/integration/telegram_spec.rb b/spec/requests/integration/telegram_spec.rb index 3f6a638f0..cc280ce9c 100644 --- a/spec/requests/integration/telegram_spec.rb +++ b/spec/requests/integration/telegram_spec.rb @@ -35,7 +35,7 @@ RSpec.describe 'Telegram', type: :request do Setting.set('http_type', 'http') expect do - Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' }) + Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!', goodbye: 'goodbye' }) end.to raise_error(RuntimeError) # try invalid port @@ -48,7 +48,7 @@ RSpec.describe 'Telegram', type: :request do Setting.set('http_type', 'https') Setting.set('fqdn', 'somehost.example.com:12345') expect do - Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' }) + Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!', goodbye: 'goodbye' }) end.to raise_error(RuntimeError) # try invalid host @@ -58,7 +58,7 @@ RSpec.describe 'Telegram', type: :request do Setting.set('fqdn', 'somehost.example.com') expect do - Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' }) + Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!', goodbye: 'goodbye' }) end.to raise_error(RuntimeError) # valid token, host and port @@ -67,7 +67,7 @@ RSpec.describe 'Telegram', type: :request do .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {}) Setting.set('fqdn', 'example.com') - channel = Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' }) + channel = Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!', goodbye: 'goodbye' }) UserInfo.current_user_id = nil # start communication #1 @@ -76,7 +76,7 @@ RSpec.describe 'Telegram', type: :request do post '/api/v1/channels_telegram_webhook/not_existing', params: read_message('personal1_message_start'), as: :json expect(response).to have_http_status(:unprocessable_entity) - expect(json_response['error']).to eq('bot params missing') + expect(json_response['error']).to eq('bot id is missing') callback_url = "/api/v1/channels_telegram_webhook/not_existing?bid=#{channel.options[:bot][:id]}" post callback_url, params: read_message('personal1_message_start'), as: :json @@ -391,7 +391,7 @@ RSpec.describe 'Telegram', type: :request do .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id1}" }) .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {}) - channel1 = Telegram.create_or_update_channel(token1, { group_id: group1.id, welcome: 'hi!' }) + channel1 = Telegram.create_or_update_channel(token1, { group_id: group1.id, welcome: 'hi!', goodbye: 'goodbye' }) # start communication #1 callback_url1 = "/api/v1/channels_telegram_webhook/#{channel1.options[:callback_token]}?bid=#{channel1.options[:bot][:id]}" @@ -425,7 +425,7 @@ RSpec.describe 'Telegram', type: :request do .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id2}" }) .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {}) - channel2 = Telegram.create_or_update_channel(token2, { group_id: group2.id, welcome: 'hi!' }) + channel2 = Telegram.create_or_update_channel(token2, { group_id: group2.id, welcome: 'hi!', goodbye: 'goodbye' }) # start communication #1 callback_url2 = "/api/v1/channels_telegram_webhook/#{channel2.options[:callback_token]}?bid=#{channel2.options[:bot][:id]}"