Browse Source

Fixes #2379 - Zammad ignores group mapping on Telegram if same sending user.

Denny Bresch 5 years ago
parent
commit
1db27646d5

+ 1 - 1
app/controllers/channels_telegram_controller.rb

@@ -57,7 +57,7 @@ class ChannelsTelegramController < ApplicationController
   end
 
   def webhook
-    raise Exceptions::UnprocessableEntity, 'bot param missing' if params['bid'].blank?
+    raise Exceptions::UnprocessableEntity, 'bot params missing' if params['bid'].blank?
 
     channel = Telegram.bot_by_bot_id(params['bid'])
     raise Exceptions::UnprocessableEntity, 'bot not found' if !channel

+ 1 - 1
app/models/channel/driver/telegram.rb

@@ -9,7 +9,7 @@ class Channel::Driver::Telegram
     {
       adapter: 'telegram',
       auth: {
-        api_key:       api_key
+        api_key: api_key
       },
     },
     telegram_attributes,

+ 7 - 6
lib/telegram.rb

@@ -24,7 +24,7 @@ check token and return bot attributes of token
 
 =begin
 
-set webhool for bot
+set webhook for bot
 
   success = Telegram.set_webhook('token', callback_url)
 
@@ -80,7 +80,7 @@ returns
       raise 'Group invalid!'
     end
 
-    # generate randam callback token
+    # generate random callback token
     callback_token = if Rails.env.test?
                        'callback_token'
                      else
@@ -312,10 +312,11 @@ returns
     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
+    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  }
 
+    if ticket
       # check if title need to be updated
       if ticket.title == '-'
         ticket.title = title
@@ -628,7 +629,7 @@ returns
       params.delete(:edited_channel_post) # discard unused :edited_channel_post hash
     end
 
-    # prevent multible update
+    # prevent multiple update
     if !params[:edited_message]
       return if Ticket::Article.find_by(message_id: Telegram.message_id(params))
     end

+ 91 - 4
spec/requests/integration/telegram_spec.rb

@@ -8,8 +8,8 @@ RSpec.describe 'Telegram', type: :request do
       Ticket.destroy_all
 
       # configure telegram channel
-      token = 'valid_token'
-      bot_id = 123_456_789
+      token    = 'valid_token'
+      bot_id   = 123_456_789
       group_id = Group.find_by(name: 'Users').id
 
       UserInfo.current_user_id = 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 param missing')
+      expect(json_response['error']).to eq('bot params 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
@@ -240,7 +240,7 @@ RSpec.describe 'Telegram', type: :request do
       expect(ticket.articles.last.content_type).to eq('text/html')
       expect(ticket.articles.last.attachments.count).to eq(1)
 
-      # isend channel message 2
+      # send channel message 2
       post callback_url, params: read_message('channel1_message_content2'), as: :json
       expect(response).to have_http_status(:ok)
       expect(Ticket.count).to eq(3)
@@ -360,6 +360,93 @@ RSpec.describe 'Telegram', type: :request do
       expect(ticket.articles.last.attachments.count).to eq(0)
     end
 
+    it 'with two bots and different groups' do
+      Ticket.destroy_all
+
+      # configure telegram channel
+      token1 = 'valid_token1'
+      token2 = 'valid_token2'
+
+      bot_id1 = 123_456_789
+      bot_id2 = 987_654_321
+
+      group1 = create(:group)
+      group2 = create(:group)
+
+      UserInfo.current_user_id = 1
+      Channel.where(area: 'Telegram::Bot').destroy_all
+
+      Setting.set('http_type', 'https')
+      Setting.set('fqdn', 'example.com')
+
+      # channel 1 - try valid token
+      stub_request(:post, "https://api.telegram.org/bot#{token1}/getMe")
+        .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id1},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot1\"}}", headers: {})
+
+      bot1 = Telegram.check_token(token1)
+      expect(bot1['id']).to eq(bot_id1)
+
+      # channel 1 - valid token, host and port
+      stub_request(:post, "https://api.telegram.org:443/bot#{token1}/setWebhook")
+        .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!' })
+
+      # start communication #1
+      callback_url1 = "/api/v1/channels_telegram_webhook/#{channel1.options[:callback_token]}?bid=#{channel1.options[:bot][:id]}"
+      post callback_url1, params: read_message('personal1_message_start'), as: :json
+      expect(response).to have_http_status(:ok)
+
+      # send message1
+      post callback_url1, params: read_message('personal1_message_content1'), as: :json
+      expect(response).to have_http_status(:ok)
+      expect(Ticket.count).to eq(1)
+      ticket1 = Ticket.last
+      expect(ticket1.title).to eq('Hello, I need your Help')
+      expect(ticket1.state.name).to eq('new')
+      expect(ticket1.articles.count).to eq(1)
+      expect(ticket1.articles.first.body).to eq('Hello, I need your Help')
+      expect(ticket1.articles.first.content_type).to eq('text/plain')
+
+      expect(ticket1.articles.first.from).to eq('Test Firstname Test Lastname')
+      expect(ticket1.articles.first.to).to eq('@ChrispressoBot1')
+
+      # channel 2 - try valid token
+      UserInfo.current_user_id = 1
+      stub_request(:post, "https://api.telegram.org/bot#{token2}/getMe")
+        .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id2},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot2\"}}", headers: {})
+
+      bot2 = Telegram.check_token(token2)
+      expect(bot2['id']).to eq(bot_id2)
+
+      # channel 2 - valid token, host and port
+      stub_request(:post, "https://api.telegram.org:443/bot#{token2}/setWebhook")
+        .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!' })
+
+      # start communication #1
+      callback_url2 = "/api/v1/channels_telegram_webhook/#{channel2.options[:callback_token]}?bid=#{channel2.options[:bot][:id]}"
+      post callback_url2, params: read_message('personal3_message_start'), as: :json
+      expect(response).to have_http_status(:ok)
+
+      # send message2
+      post callback_url2, params: read_message('personal3_message_content1'), as: :json
+      expect(response).to have_http_status(:ok)
+      expect(Ticket.count).to eq(2)
+      ticket2 = Ticket.last
+      expect(ticket2.title).to eq('Can you help me with my feature?')
+      expect(ticket2.state.name).to eq('new')
+      expect(ticket2.articles.count).to eq(1)
+      expect(ticket2.articles.first.body).to eq('Can you help me with my feature?')
+      expect(ticket2.articles.first.content_type).to eq('text/plain')
+
+      expect(ticket2.articles.first.from).to eq('Test Firstname2 Test Lastname2')
+      expect(ticket2.articles.first.to).to eq('@ChrispressoBot2')
+    end
+
     def read_message(file)
       JSON.parse(File.read(Rails.root.join('test', 'data', 'telegram', "#{file}.json")))
     end

+ 1 - 1
test/data/telegram/channel1_message_content1.json

@@ -15,7 +15,7 @@
     "entities": [
       {"offset": 0,
        "length": 6,
-       "type": "bot_command"      
+       "type": "bot_command"
       }
     ]
   }

+ 1 - 1
test/data/telegram/channel2_message_content1.json

@@ -15,7 +15,7 @@
     "entities": [
       {"offset": 0,
        "length": 6,
-       "type": "bot_command"      
+       "type": "bot_command"
       }
     ]
   }