Fixes #2695 - Telegram not possible to close ticket via '/end'.

This commit is contained in:
Denny Bresch 2019-08-12 17:25:32 +02:00 committed by Thorsten Eckel
parent 848175a290
commit 92b38dcb75
6 changed files with 55 additions and 30 deletions

View file

@ -1,11 +1,11 @@
class Index extends App.ControllerSubContent class Index extends App.ControllerSubContent
requiredPermission: 'admin.channel_telegram' requiredPermission: 'admin.channel_telegram'
events: events:
'click .js-new': 'new' 'click .js-new': 'new'
'click .js-edit': 'edit' 'click .js-edit': 'edit'
'click .js-delete': 'delete' 'click .js-delete': 'delete'
'click .js-disable': 'disable' 'click .js-disable': 'disable'
'click .js-enable': 'enable' 'click .js-enable': 'enable'
constructor: -> constructor: ->
super super
@ -114,14 +114,14 @@ class BotAdd extends App.ControllerModal
content = $(App.view('telegram/bot_add')()) content = $(App.view('telegram/bot_add')())
createGroupSelection = (selected_id) -> createGroupSelection = (selected_id) ->
return App.UiElement.select.render( return App.UiElement.select.render(
name: 'group_id' name: 'group_id'
multiple: false multiple: false
limit: 100 limit: 100
null: false null: false
relation: 'Group' relation: 'Group'
nulloption: true nulloption: true
value: selected_id value: selected_id
class: 'form-control--small' class: 'form-control--small'
) )
content.find('.js-select').on('click', (e) => content.find('.js-select').on('click', (e) =>
@ -163,14 +163,14 @@ class BotEdit extends App.ControllerModal
createGroupSelection = (selected_id) -> createGroupSelection = (selected_id) ->
return App.UiElement.select.render( return App.UiElement.select.render(
name: 'group_id' name: 'group_id'
multiple: false multiple: false
limit: 100 limit: 100
null: false null: false
relation: 'Group' relation: 'Group'
nulloption: true nulloption: true
value: selected_id value: selected_id
class: 'form-control--small' class: 'form-control--small'
) )
content.find('.js-messagesGroup').replaceWith createGroupSelection(@channel.group_id) content.find('.js-messagesGroup').replaceWith createGroupSelection(@channel.group_id)

View file

@ -21,6 +21,14 @@
<input id="welcome" type="text" name="welcome" value="" placeholder="<%- @Ti('You are welcome! Just ask me something!') %>" class="form-control" required autocomplete="off"> <input id="welcome" type="text" name="welcome" value="" placeholder="<%- @Ti('You are welcome! Just ask me something!') %>" class="form-control" required autocomplete="off">
</div> </div>
</div> </div>
<div class="input form-group">
<div class="formGroup-label">
<label for="goodbye"><%- @T('Goodbye message') %> <span>*</span></label>
</div>
<div class="controls">
<input id="goodbye" type="text" name="goodbye" value="" placeholder="<%- @Ti('Have a nice day.') %>" class="form-control" required autocomplete="off">
</div>
</div>
<div class="input form-group"> <div class="input form-group">
<div class="formGroup-label"> <div class="formGroup-label">
<label for=""><%- @T('Choose the group in which messages will get added to.') %> <span>*</span></label> <label for=""><%- @T('Choose the group in which messages will get added to.') %> <span>*</span></label>

View file

@ -18,6 +18,14 @@
<input id="welcome" type="text" name="welcome" value="<%= @channel.options.welcome %>" placeholder="<%- @Ti('You are welcome! Just ask me something!') %>" class="form-control" required autocomplete="off"> <input id="welcome" type="text" name="welcome" value="<%= @channel.options.welcome %>" placeholder="<%- @Ti('You are welcome! Just ask me something!') %>" class="form-control" required autocomplete="off">
</div> </div>
</div> </div>
<div class="input form-group">
<div class="formGroup-label">
<label for="goodbye"><%- @T('Goodbye message') %> <span>*</span></label>
</div>
<div class="controls">
<input id="goodbye" type="text" name="goodbye" value="<%= @channel.options.goodbye %>" placeholder="<%- @Ti('Have a nice day.') %>" class="form-control" required autocomplete="off">
</div>
</div>
<div class="input form-group"> <div class="input form-group">
<div class="formGroup-label"> <div class="formGroup-label">
<label for=""><%- @T('Choose the group in which messages will get added to.') %> <span>*</span></label> <label for=""><%- @T('Choose the group in which messages will get added to.') %> <span>*</span></label>

View file

@ -57,7 +57,7 @@ class ChannelsTelegramController < ApplicationController
end end
def webhook 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']) channel = Telegram.bot_by_bot_id(params['bid'])
raise Exceptions::UnprocessableEntity, 'bot not found' if !channel raise Exceptions::UnprocessableEntity, 'bot not found' if !channel

View file

@ -109,6 +109,7 @@ returns
callback_url: callback_url, callback_url: callback_url,
api_token: token, api_token: token,
welcome: params[:welcome], welcome: params[:welcome],
goodbye: params[:goodbye],
} }
channel.group_id = group.id channel.group_id = group.id
channel.active = true channel.active = true
@ -654,9 +655,17 @@ returns
# find ticket and close it # find ticket and close it
elsif text.present? && text =~ %r{^/end} elsif text.present? && text =~ %r{^/end}
user = to_user(params) 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! ticket.save!
return if !channel.options[:goodbye]
message(params[:message][:chat][:id], channel.options[:goodbye])
return return
end end
@ -664,7 +673,7 @@ returns
# use transaction # use transaction
Transaction.execute(reset_user_id: true) do Transaction.execute(reset_user_id: true) do
user = to_user(params) user = to_user(params)
ticket = to_ticket(params, user, group_id, channel) ticket = to_ticket(params, user, group_id, channel)
to_article(params, user, ticket, channel) to_article(params, user, ticket, channel)
end end

View file

@ -35,7 +35,7 @@ RSpec.describe 'Telegram', type: :request do
Setting.set('http_type', 'http') Setting.set('http_type', 'http')
expect do 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) end.to raise_error(RuntimeError)
# try invalid port # try invalid port
@ -48,7 +48,7 @@ RSpec.describe 'Telegram', type: :request do
Setting.set('http_type', 'https') Setting.set('http_type', 'https')
Setting.set('fqdn', 'somehost.example.com:12345') Setting.set('fqdn', 'somehost.example.com:12345')
expect do 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) end.to raise_error(RuntimeError)
# try invalid host # try invalid host
@ -58,7 +58,7 @@ RSpec.describe 'Telegram', type: :request do
Setting.set('fqdn', 'somehost.example.com') Setting.set('fqdn', 'somehost.example.com')
expect do 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) end.to raise_error(RuntimeError)
# valid token, host and port # 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: {}) .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
Setting.set('fqdn', 'example.com') 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 UserInfo.current_user_id = nil
# start communication #1 # 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 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(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]}" 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 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}" }) .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: {}) .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 # start communication #1
callback_url1 = "/api/v1/channels_telegram_webhook/#{channel1.options[:callback_token]}?bid=#{channel1.options[:bot][:id]}" 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}" }) .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: {}) .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 # start communication #1
callback_url2 = "/api/v1/channels_telegram_webhook/#{channel2.options[:callback_token]}?bid=#{channel2.options[:bot][:id]}" callback_url2 = "/api/v1/channels_telegram_webhook/#{channel2.options[:callback_token]}?bid=#{channel2.options[:bot][:id]}"