Fixed issue #774 - Open communication with only a picture.

This commit is contained in:
Martin Edenhofer 2017-02-22 07:11:57 +01:00
parent 60a4f0062b
commit 93ba996de4
5 changed files with 227 additions and 21 deletions

View file

@ -176,6 +176,14 @@ returns
message_id = params[key][:message_id]
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
message_id = params[:update_id]
end
@ -265,29 +273,54 @@ returns
Rails.logger.debug user.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
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
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
ticket.state = Ticket::State.find_by(name: 'open')
ticket.state = Ticket::State.find_by(default_follow_up: true)
end
ticket.save!
return ticket
end
# prepare title
title = params[:message][:text]
if title.length > 60
title = "#{title[0, 60]}..."
end
ticket = Ticket.new(
group_id: group_id,
title: title,
state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id,
state_id: Ticket::State.find_by(default_create: true).id,
priority_id: Ticket::Priority.find_by(default_create: true).id,
customer_id: user.id,
preferences: {
channel_id: channel.id,
@ -384,12 +417,12 @@ returns
# add document
if params[:message][:document]
thump = params[:message][:document][:thumb]
thumb = params[:message][:document][:thumb]
body = ' '
if thump
width = thump[:width]
height = thump[:height]
result = download_file(thump['file_id'])
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
@ -441,6 +474,45 @@ returns
return article
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
if params[:message][:text]
article.content_type = 'text/plain'
@ -520,10 +592,10 @@ returns
return ticket.state
end
state = Ticket::State.find_by(name: 'new')
state = Ticket::State.find_by(default_create: true)
return state if !ticket
return ticket.state if ticket.state.name == 'new'
Ticket::State.find_by(name: 'open')
return ticket.state if ticket.state.id == state.id
Ticket::State.find_by(default_follow_up: true)
end
def download_file(file_id)

View 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
}
}
}

View 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
}
]
}
}

View 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"
}
}

View file

@ -14,6 +14,7 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
Setting.set('http_type', 'https')
Setting.set('fqdn', 'me.zammad.com')
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!' })
groups = Group.where(name: 'Users')
@ -27,9 +28,8 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
active: true,
roles: roles,
groups: groups,
updated_by_id: 1,
created_by_id: 1,
)
UserInfo.current_user_id = nil
end
@ -184,10 +184,44 @@ class TelegramControllerTest < ActionDispatch::IntegrationTest
assert_equal('Can you help me with my feature?', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal(4, ticket.articles.count)
#assert_match(/ /i, ticket.articles.last.body)
assert_equal('text/html', ticket.articles.last.content_type)
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
def read_messaage(file)