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
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)

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">
</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="formGroup-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">
</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="formGroup-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
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

View file

@ -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

View file

@ -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]}"