From 93ba996de461021379dbe3c88e232b6ee88a45cd Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 22 Feb 2017 07:11:57 +0100 Subject: [PATCH] Fixed issue #774 - Open communication with only a picture. --- lib/telegram.rb | 108 +++++++++++++++--- .../telegram/personal4_message_content1.json | 31 +++++ .../telegram/personal5_message_content1.json | 50 ++++++++ .../telegram/personal5_message_content2.json | 19 +++ test/integration/telegram_controller_test.rb | 40 ++++++- 5 files changed, 227 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/telegram/personal4_message_content1.json create mode 100644 test/fixtures/telegram/personal5_message_content1.json create mode 100644 test/fixtures/telegram/personal5_message_content2.json diff --git a/lib/telegram.rb b/lib/telegram.rb index b5d34ce88..3f0a09a3b 100644 --- a/lib/telegram.rb +++ b/lib/telegram.rb @@ -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 = "" + 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) diff --git a/test/fixtures/telegram/personal4_message_content1.json b/test/fixtures/telegram/personal4_message_content1.json new file mode 100644 index 000000000..820e0550a --- /dev/null +++ b/test/fixtures/telegram/personal4_message_content1.json @@ -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 + } + } +} \ No newline at end of file diff --git a/test/fixtures/telegram/personal5_message_content1.json b/test/fixtures/telegram/personal5_message_content1.json new file mode 100644 index 000000000..cd44ed58b --- /dev/null +++ b/test/fixtures/telegram/personal5_message_content1.json @@ -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 + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/telegram/personal5_message_content2.json b/test/fixtures/telegram/personal5_message_content2.json new file mode 100644 index 000000000..d1aa6873f --- /dev/null +++ b/test/fixtures/telegram/personal5_message_content2.json @@ -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" + } +} \ No newline at end of file diff --git a/test/integration/telegram_controller_test.rb b/test/integration/telegram_controller_test.rb index 45128bfcb..2acc4496b 100644 --- a/test/integration/telegram_controller_test.rb +++ b/test/integration/telegram_controller_test.rb @@ -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(/