Fixes #2695 - Telegram not possible to close ticket via '/end'.
This commit is contained in:
parent
848175a290
commit
92b38dcb75
6 changed files with 55 additions and 30 deletions
|
@ -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>
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
|
||||||
|
# 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.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
|
||||||
|
|
||||||
|
|
|
@ -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]}"
|
||||||
|
|
Loading…
Reference in a new issue