Fixed issue #774 - Open communication with only a picture.
This commit is contained in:
parent
60a4f0062b
commit
93ba996de4
5 changed files with 227 additions and 21 deletions
108
lib/telegram.rb
108
lib/telegram.rb
|
@ -176,6 +176,14 @@ returns
|
||||||
message_id = params[key][:message_id]
|
message_id = params[key][:message_id]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if message_id
|
||||||
|
[:message, :edited_message].each { |key|
|
||||||
|
next if !params[key]
|
||||||
|
next if !params[key][:chat]
|
||||||
|
next if !params[key][:chat][:id]
|
||||||
|
message_id = "#{message_id}.#{params[key][:chat][:id]}"
|
||||||
|
}
|
||||||
|
end
|
||||||
if !message_id
|
if !message_id
|
||||||
message_id = params[:update_id]
|
message_id = params[:update_id]
|
||||||
end
|
end
|
||||||
|
@ -265,29 +273,54 @@ returns
|
||||||
Rails.logger.debug user.inspect
|
Rails.logger.debug user.inspect
|
||||||
Rails.logger.debug group_id.inspect
|
Rails.logger.debug group_id.inspect
|
||||||
|
|
||||||
|
# prepare title
|
||||||
|
title = '-'
|
||||||
|
[:text, :caption].each { |area|
|
||||||
|
next if !params[:message]
|
||||||
|
next if !params[:message][area]
|
||||||
|
title = params[:message][area]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if title == '-'
|
||||||
|
[:sticker, :photo, :document, :voice].each { |area|
|
||||||
|
begin
|
||||||
|
next if !params[:message]
|
||||||
|
next if !params[:message][area]
|
||||||
|
next if !params[:message][area][:emoji]
|
||||||
|
title = params[:message][area][:emoji]
|
||||||
|
break
|
||||||
|
rescue
|
||||||
|
# just go ahead
|
||||||
|
title
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
if title.length > 60
|
||||||
|
title = "#{title[0, 60]}..."
|
||||||
|
end
|
||||||
|
|
||||||
# find ticket or create one
|
# find ticket or create one
|
||||||
state_ids = Ticket::State.where(name: %w(closed merged removed)).pluck(:id)
|
state_ids = Ticket::State.where(name: %w(closed merged removed)).pluck(:id)
|
||||||
ticket = Ticket.where(customer_id: user.id).where.not(state_id: state_ids).order(:updated_at).first
|
ticket = Ticket.where(customer_id: user.id).where.not(state_id: state_ids).order(:updated_at).first
|
||||||
if ticket
|
if ticket
|
||||||
new_state = Ticket::State.find_by(name: 'new')
|
|
||||||
|
# check if title need to be updated
|
||||||
|
if ticket.title == '-'
|
||||||
|
ticket.title = title
|
||||||
|
end
|
||||||
|
new_state = Ticket::State.find_by(default_create: true)
|
||||||
if ticket.state_id != new_state.id
|
if ticket.state_id != new_state.id
|
||||||
ticket.state = Ticket::State.find_by(name: 'open')
|
ticket.state = Ticket::State.find_by(default_follow_up: true)
|
||||||
end
|
end
|
||||||
ticket.save!
|
ticket.save!
|
||||||
return ticket
|
return ticket
|
||||||
end
|
end
|
||||||
|
|
||||||
# prepare title
|
|
||||||
title = params[:message][:text]
|
|
||||||
if title.length > 60
|
|
||||||
title = "#{title[0, 60]}..."
|
|
||||||
end
|
|
||||||
|
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
group_id: group_id,
|
group_id: group_id,
|
||||||
title: title,
|
title: title,
|
||||||
state_id: Ticket::State.find_by(name: 'new').id,
|
state_id: Ticket::State.find_by(default_create: true).id,
|
||||||
priority_id: Ticket::Priority.find_by(name: '2 normal').id,
|
priority_id: Ticket::Priority.find_by(default_create: true).id,
|
||||||
customer_id: user.id,
|
customer_id: user.id,
|
||||||
preferences: {
|
preferences: {
|
||||||
channel_id: channel.id,
|
channel_id: channel.id,
|
||||||
|
@ -384,12 +417,12 @@ returns
|
||||||
|
|
||||||
# add document
|
# add document
|
||||||
if params[:message][:document]
|
if params[:message][:document]
|
||||||
thump = params[:message][:document][:thumb]
|
thumb = params[:message][:document][:thumb]
|
||||||
body = ' '
|
body = ' '
|
||||||
if thump
|
if thumb
|
||||||
width = thump[:width]
|
width = thumb[:width]
|
||||||
height = thump[:height]
|
height = thumb[:height]
|
||||||
result = download_file(thump['file_id'])
|
result = download_file(thumb['file_id'])
|
||||||
if !result.success? || !result.body
|
if !result.success? || !result.body
|
||||||
raise "Unable for download image from telegram: #{result.code}"
|
raise "Unable for download image from telegram: #{result.code}"
|
||||||
end
|
end
|
||||||
|
@ -441,6 +474,45 @@ returns
|
||||||
return article
|
return article
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if params[:message][:sticker]
|
||||||
|
emoji = params[:message][:sticker][:emoji]
|
||||||
|
thumb = params[:message][:sticker][:thumb]
|
||||||
|
body = ' '
|
||||||
|
if thumb
|
||||||
|
width = thumb[:width]
|
||||||
|
height = thumb[:height]
|
||||||
|
result = download_file(thumb['file_id'])
|
||||||
|
if !result.success? || !result.body
|
||||||
|
raise "Unable for download image from telegram: #{result.code}"
|
||||||
|
end
|
||||||
|
body = "<img style=\"width:#{width}px;height:#{height}px;\" src=\"data:image/png;base64,#{Base64.strict_encode64(result.body)}\">"
|
||||||
|
article.content_type = 'text/html'
|
||||||
|
elsif emoji
|
||||||
|
article.content_type = 'text/plain'
|
||||||
|
body = emoji
|
||||||
|
end
|
||||||
|
article.body = body
|
||||||
|
article.save!
|
||||||
|
|
||||||
|
if params[:message][:sticker][:file_id]
|
||||||
|
document_result = download_file(params[:message][:sticker][:file_id])
|
||||||
|
Store.remove(
|
||||||
|
object: 'Ticket::Article',
|
||||||
|
o_id: article.id,
|
||||||
|
)
|
||||||
|
Store.add(
|
||||||
|
object: 'Ticket::Article',
|
||||||
|
o_id: article.id,
|
||||||
|
data: document_result.body,
|
||||||
|
filename: params[:message][:sticker][:file_name] || 'sticker',
|
||||||
|
preferences: {
|
||||||
|
'Mime-Type' => params[:message][:sticker][:mime_type],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
end
|
||||||
|
return article
|
||||||
|
end
|
||||||
|
|
||||||
# text
|
# text
|
||||||
if params[:message][:text]
|
if params[:message][:text]
|
||||||
article.content_type = 'text/plain'
|
article.content_type = 'text/plain'
|
||||||
|
@ -520,10 +592,10 @@ returns
|
||||||
return ticket.state
|
return ticket.state
|
||||||
end
|
end
|
||||||
|
|
||||||
state = Ticket::State.find_by(name: 'new')
|
state = Ticket::State.find_by(default_create: true)
|
||||||
return state if !ticket
|
return state if !ticket
|
||||||
return ticket.state if ticket.state.name == 'new'
|
return ticket.state if ticket.state.id == state.id
|
||||||
Ticket::State.find_by(name: 'open')
|
Ticket::State.find_by(default_follow_up: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def download_file(file_id)
|
def download_file(file_id)
|
||||||
|
|
31
test/fixtures/telegram/personal4_message_content1.json
vendored
Normal file
31
test/fixtures/telegram/personal4_message_content1.json
vendored
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
{
|
||||||
|
"update_id":40000,
|
||||||
|
"message":{
|
||||||
|
"message_id":273,
|
||||||
|
"from":{
|
||||||
|
"id":3310000,
|
||||||
|
"first_name":"Roberto",
|
||||||
|
"last_name":"Blanco"
|
||||||
|
},
|
||||||
|
"chat":{
|
||||||
|
"id":3310000,
|
||||||
|
"first_name":"Roberto",
|
||||||
|
"last_name":"Blanco",
|
||||||
|
"type":"private"
|
||||||
|
},
|
||||||
|
"date":1487161566,
|
||||||
|
"sticker":{
|
||||||
|
"width":512,
|
||||||
|
"height":512,
|
||||||
|
"emoji":"💻",
|
||||||
|
"thumb":{
|
||||||
|
"file_id":"AAQDABO3-e4qAASs6ZOjJUT7tQ4lAAIC",
|
||||||
|
"file_size":4584,
|
||||||
|
"width":128,
|
||||||
|
"height":128
|
||||||
|
},
|
||||||
|
"file_id":"BQADAwAD0QIAAqbJWAAB8OkQqgtDQe0C",
|
||||||
|
"file_size":25974
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
test/fixtures/telegram/personal5_message_content1.json
vendored
Normal file
50
test/fixtures/telegram/personal5_message_content1.json
vendored
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
{
|
||||||
|
"update_id":50000,
|
||||||
|
"message":{
|
||||||
|
"message_id":15,
|
||||||
|
"from":{
|
||||||
|
"id":294302174,
|
||||||
|
"first_name":"Martin",
|
||||||
|
"username":"martini42"
|
||||||
|
},
|
||||||
|
"chat":{
|
||||||
|
"id":294302174,
|
||||||
|
"first_name":"Martin",
|
||||||
|
"username":"martini42",
|
||||||
|
"type":"private"
|
||||||
|
},
|
||||||
|
"date":1487295982,
|
||||||
|
"photo":[
|
||||||
|
{
|
||||||
|
"file_id":"AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABFq_5qQydQSuAAFkBQABAg",
|
||||||
|
"file_size":1265,
|
||||||
|
"width":90,
|
||||||
|
"height":79
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file_id":"AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABEvfeiPeNricAWQFAAEC",
|
||||||
|
"file_size":17208,
|
||||||
|
"width":320,
|
||||||
|
"height":282
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file_id":"AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABG1U3brRukt_AmQFAAEC",
|
||||||
|
"file_size":62404,
|
||||||
|
"width":800,
|
||||||
|
"height":705
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file_id":"AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABNQoaI8BwR_z_2MFAAEC",
|
||||||
|
"file_size":122459,
|
||||||
|
"width":1280,
|
||||||
|
"height":1128
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file_id":"AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABPxEqfVm-QlI_mMFAAEC",
|
||||||
|
"file_size":171271,
|
||||||
|
"width":2000,
|
||||||
|
"height":1762
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
19
test/fixtures/telegram/personal5_message_content2.json
vendored
Normal file
19
test/fixtures/telegram/personal5_message_content2.json
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"update_id":50001,
|
||||||
|
"message":{
|
||||||
|
"message_id":16,
|
||||||
|
"from":{
|
||||||
|
"id":294302174,
|
||||||
|
"first_name":"Martin",
|
||||||
|
"username":"martini42"
|
||||||
|
},
|
||||||
|
"chat":{
|
||||||
|
"id":294302174,
|
||||||
|
"first_name":"Martin",
|
||||||
|
"username":"martini42",
|
||||||
|
"type":"private"
|
||||||
|
},
|
||||||
|
"date":1487295982,
|
||||||
|
"text":"Hello, I need your Help"
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
|
||||||
Setting.set('http_type', 'https')
|
Setting.set('http_type', 'https')
|
||||||
Setting.set('fqdn', 'me.zammad.com')
|
Setting.set('fqdn', 'me.zammad.com')
|
||||||
Channel.where(area: 'Telegram::Bot').destroy_all
|
Channel.where(area: 'Telegram::Bot').destroy_all
|
||||||
|
UserInfo.current_user_id = 1
|
||||||
@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!' })
|
||||||
|
|
||||||
groups = Group.where(name: 'Users')
|
groups = Group.where(name: 'Users')
|
||||||
|
@ -27,9 +28,8 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
|
||||||
active: true,
|
active: true,
|
||||||
roles: roles,
|
roles: roles,
|
||||||
groups: groups,
|
groups: groups,
|
||||||
updated_by_id: 1,
|
|
||||||
created_by_id: 1,
|
|
||||||
)
|
)
|
||||||
|
UserInfo.current_user_id = nil
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -184,10 +184,44 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal('Can you help me with my feature?', ticket.title)
|
assert_equal('Can you help me with my feature?', ticket.title)
|
||||||
assert_equal('new', ticket.state.name)
|
assert_equal('new', ticket.state.name)
|
||||||
assert_equal(4, ticket.articles.count)
|
assert_equal(4, ticket.articles.count)
|
||||||
#assert_match(/ /i, ticket.articles.last.body)
|
|
||||||
assert_equal('text/html', ticket.articles.last.content_type)
|
assert_equal('text/html', ticket.articles.last.content_type)
|
||||||
assert_equal(1, ticket.articles.last.attachments.count)
|
assert_equal(1, ticket.articles.last.attachments.count)
|
||||||
|
|
||||||
|
# start communication #4 - with sticker
|
||||||
|
post callback_url, read_messaage('personal4_message_content1'), @headers
|
||||||
|
assert_response(200)
|
||||||
|
assert_equal(4, Ticket.count)
|
||||||
|
ticket = Ticket.last
|
||||||
|
assert_equal('💻', ticket.title)
|
||||||
|
assert_equal('new', ticket.state.name)
|
||||||
|
assert_equal(1, ticket.articles.count)
|
||||||
|
assert_match(/<img style="/i, ticket.articles.last.body)
|
||||||
|
assert_equal('text/html', ticket.articles.last.content_type)
|
||||||
|
assert_equal(1, ticket.articles.last.attachments.count)
|
||||||
|
|
||||||
|
# start communication #5 - with photo
|
||||||
|
post callback_url, read_messaage('personal5_message_content1'), @headers
|
||||||
|
assert_response(200)
|
||||||
|
assert_equal(5, Ticket.count)
|
||||||
|
ticket = Ticket.last
|
||||||
|
assert_equal('-', ticket.title)
|
||||||
|
assert_equal('new', ticket.state.name)
|
||||||
|
assert_equal(1, ticket.articles.count)
|
||||||
|
assert_match(/<img style="/i, ticket.articles.last.body)
|
||||||
|
assert_equal('text/html', ticket.articles.last.content_type)
|
||||||
|
assert_equal(0, ticket.articles.last.attachments.count)
|
||||||
|
|
||||||
|
post callback_url, read_messaage('personal5_message_content2'), @headers
|
||||||
|
assert_response(200)
|
||||||
|
assert_equal(5, Ticket.count)
|
||||||
|
ticket = Ticket.last
|
||||||
|
assert_equal('Hello, I need your Help', ticket.title)
|
||||||
|
assert_equal('new', ticket.state.name)
|
||||||
|
assert_equal(2, ticket.articles.count)
|
||||||
|
assert_match(/Hello, I need your Help/i, ticket.articles.last.body)
|
||||||
|
assert_equal('text/plain', ticket.articles.last.content_type)
|
||||||
|
assert_equal(0, ticket.articles.last.attachments.count)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_messaage(file)
|
def read_messaage(file)
|
||||||
|
|
Loading…
Reference in a new issue