Browse Source

Maintenance: Update dependency telegram-bot-ruby to v2.

Co-authored-by: Florian Liebe <fl@zammad.com>
renovatebot 1 year ago
parent
commit
48bee3ebfa

+ 8 - 8
Gemfile.lock

@@ -137,6 +137,7 @@ GEM
     aws-sigv4 (1.8.0)
       aws-eventstream (~> 1, >= 1.0.2)
     base64 (0.2.0)
+    bigdecimal (3.1.6)
     bindata (2.4.15)
     binding_of_caller (1.0.0)
       debug_inspector (>= 0.0.1)
@@ -218,7 +219,8 @@ GEM
       dry-types (>= 1.7, < 2)
       ice_nine (~> 0.11)
       zeitwerk (~> 2.6)
-    dry-types (1.7.1)
+    dry-types (1.7.2)
+      bigdecimal (~> 3.0)
       concurrent-ruby (~> 1.0)
       dry-core (~> 1.0)
       dry-inflector (~> 1.0)
@@ -245,16 +247,15 @@ GEM
       railties (>= 5.0.0)
     faker (3.2.3)
       i18n (>= 1.8.11, < 2)
-    faraday (2.7.12)
-      base64
-      faraday-net_http (>= 2.0, < 3.1)
-      ruby2_keywords (>= 0.0.4)
+    faraday (2.9.0)
+      faraday-net_http (>= 2.0, < 3.2)
     faraday-mashify (0.1.1)
       faraday (~> 2.0)
       hashie
     faraday-multipart (1.0.4)
       multipart-post (~> 2)
-    faraday-net_http (3.0.2)
+    faraday-net_http (3.1.0)
+      net-http
     ffi (1.15.5)
     ffi-compiler (1.0.1)
       ffi (>= 1.0.0)
@@ -576,7 +577,6 @@ GEM
     ruby-saml (1.15.0)
       nokogiri (>= 1.13.10)
       rexml
-    ruby2_keywords (0.0.5)
     rubyntlm (0.6.3)
     rubyzip (2.3.2)
     safety_net_attestation (0.4.0)
@@ -619,7 +619,7 @@ GEM
       activesupport (>= 5.2)
       sprockets (>= 3.0.0)
     tcr (0.4.0)
-    telegram-bot-ruby (1.0.0)
+    telegram-bot-ruby (2.0.0)
       dry-struct (~> 1.6)
       faraday (~> 2.0)
       faraday-multipart (~> 1.0)

+ 12 - 0
app/assets/javascripts/app/controllers/_channel/telegram.coffee

@@ -137,6 +137,12 @@ class BotAdd extends App.ControllerModal
     @load()
 
   onSubmit: (e) =>
+
+    # validate form using HTML5 validity check
+    element = $(e.target).closest('form').get(0)
+    if element && element.reportValidity && !element.reportValidity()
+      return false
+
     @formDisable(e)
     @ajax(
       id:   'telegram_app_verify'
@@ -183,6 +189,12 @@ class BotEdit extends App.ControllerModal
     @load()
 
   onSubmit: (e) =>
+
+    # validate form using HTML5 validity check
+    element = $(e.target).closest('form').get(0)
+    if element && element.reportValidity && !element.reportValidity()
+      return false
+
     @formDisable(e)
     params = @formParams()
     @channel.options = params

+ 13 - 13
app/jobs/communicate_telegram_job.rb

@@ -31,7 +31,7 @@ class CommunicateTelegramJob < ApplicationJob
     begin
       Telegram::Bot::Client.run(channel.options[:api_token]) do |bot|
         chat_id = ticket.preferences[:telegram][:chat_id]
-        result = bot.api.sendMessage(chat_id: chat_id, text: article.body)['result']
+        result = bot.api.sendMessage(chat_id: chat_id, text: article.body)
 
         article.attachments.each do |file|
           parts = file.filename.split(%r{^(.*)(\..+?)$})
@@ -57,27 +57,27 @@ class CommunicateTelegramJob < ApplicationJob
     Rails.logger.debug { "result info: #{result}" }
 
     # only private, group messages. channel messages do not have from key
-    if result['from'] && result['chat']
+    if result.from && result.chat
       # fill article with message info
-      article.from = "@#{result['from']['username']}"
-      article.to = "@#{result['chat']['username']}"
+      article.from = "@#{result.from.username}"
+      article.to = "@#{result.chat.username}"
 
       article.preferences['telegram'] = {
-        date:       result['date'],
-        from_id:    result['from']['id'],
-        chat_id:    result['chat']['id'],
-        message_id: result['message_id']
+        date:       result.date,
+        from_id:    result.from.id,
+        chat_id:    result.chat.id,
+        message_id: result.message_id
       }
     else
       # fill article with message info (telegram channel)
       article.from = "@#{me['username']}"
-      article.to = "#{result['chat']['title']} Channel"
+      article.to = "#{result.chat.title} Channel"
 
       article.preferences['telegram'] = {
-        date:       result['date'],
+        date:       result.date,
         from_id:    me['id'],
-        chat_id:    result['chat']['id'],
-        message_id: result['message_id']
+        chat_id:    result.chat.id,
+        message_id: result.message_id
       }
     end
 
@@ -86,7 +86,7 @@ class CommunicateTelegramJob < ApplicationJob
     article.preferences['delivery_status'] = 'success'
     article.preferences['delivery_status_date'] = Time.zone.now
 
-    article.message_id = "telegram.#{result['message_id']}.#{result['chat']['id']}"
+    article.message_id = "telegram.#{result.message_id}.#{result.chat.id}"
 
     article.save!
 

+ 10 - 10
lib/telegram_helper.rb

@@ -18,7 +18,7 @@ check token and return bot attributes of token
     result = nil
     begin
       Telegram::Bot::Client.run(token) do |bot|
-        result = bot.api.getMe['result']
+        result = bot.api.getMe
       end
     rescue
       raise Exceptions::UnprocessableEntity, 'invalid api token'
@@ -70,7 +70,7 @@ returns
     # verify token
     bot = check_token(token)
 
-    if !channel && bot_duplicate?(bot['id'])
+    if !channel && bot_duplicate?(bot.id)
       raise Exceptions::UnprocessableEntity, __('This bot already exists.')
     end
 
@@ -91,11 +91,11 @@ returns
                      end
 
     # set webhook / callback url for this bot @ telegram
-    callback_url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/channels_telegram_webhook/#{callback_token}?bid=#{bot['id']}"
+    callback_url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/channels_telegram_webhook/#{callback_token}?bid=#{bot.id}"
     set_webhook(token, callback_url)
 
     if !channel
-      channel = bot_by_bot_id(bot['id'])
+      channel = bot_by_bot_id(bot.id)
       if !channel
         channel = Channel.new
       end
@@ -103,10 +103,10 @@ returns
     channel.area = 'Telegram::Bot'
     channel.options = {
       bot:            {
-        id:         bot['id'],
-        username:   bot['username'],
-        first_name: bot['first_name'],
-        last_name:  bot['last_name'],
+        id:         bot.id,
+        username:   bot.username,
+        first_name: bot.first_name,
+        last_name:  bot.last_name,
       },
       callback_token: callback_token,
       callback_url:   callback_url,
@@ -795,10 +795,10 @@ returns
   def download_file(file_id)
     document = nil
     Telegram::Bot::Client.run(@token) do |bot|
-      document = bot.api.getFile(file_id: file_id)['result']
+      document = bot.api.getFile(file_id: file_id)
     end
 
-    url = "https://api.telegram.org/file/bot#{@token}/#{document['file_path']}"
+    url = "https://api.telegram.org/file/bot#{@token}/#{document.file_path}"
     UserAgent.get(
       url,
       {},

+ 10 - 10
spec/requests/integration/telegram_spec.rb

@@ -26,10 +26,10 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
       it 'valid token' do
 
         stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
 
         bot = TelegramHelper.check_token(token)
-        expect(bot['id']).to eq(bot_id)
+        expect(bot.id).to eq(bot_id)
       end
     end
 
@@ -40,7 +40,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
         Setting.set('http_type', 'http')
 
         stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
 
         expect do
           TelegramHelper.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!', goodbye: 'goodbye' })
@@ -54,7 +54,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
         Setting.set('fqdn', 'somehost.example.com:12345')
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
           .with(body: { 'url' => URI.encode_www_form(["https://somehost.example.com:12345/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}"]) })
           .to_return(status: 400, body: '{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443"}', headers: {})
@@ -70,7 +70,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
         Setting.set('fqdn', 'somehost.example.com')
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
           .with(body: { 'url' => URI.encode_www_form(["https://somehost.example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}"]) })
@@ -88,7 +88,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
         UserInfo.current_user_id = 1
 
         stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
 
         stub_request(:post, "https://api.telegram.org/bot#{token}/setWebhook")
           .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
@@ -107,14 +107,14 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
         Setting.set('fqdn', 'example.com')
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\",\"is_bot\":true}}", headers: {})
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
           .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
           .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
 
         stub_request(:post, "https://api.telegram.org:443/bot#{token2}/getMe")
-          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id2},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot2\"}}", headers: {})
+          .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id2},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot2\",\"is_bot\":true}}", headers: {})
 
         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}" })
@@ -424,7 +424,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
       %w[document documentthumb voice sticker stickerthumb video videothumb photo].each do |file|
         stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
           .with(body: { 'file_id' => "#{file}fileid" })
-          .to_return(status: 200, body: "{\"result\":{\"file_size\":123456,\"file_id\":\"#{file}fileid\",\"file_path\":\"documentfile\"}}", headers: {})
+          .to_return(status: 200, body: "{\"result\":{\"file_size\":123456,\"file_id\":\"#{file}fileid\",\"file_path\":\"documentfile\",\"file_unique_id\":\"file123\"}}", headers: {})
         stub_request(:get, "https://api.telegram.org/file/bot#{token}/#{file}file")
           .to_return(status: 200, body: "#{file}file", headers: {})
       end
@@ -432,7 +432,7 @@ RSpec.describe 'Telegram Webhook Integration', type: :request do
       [1, 2, 3].each do |id|
         stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
           .with(body: { 'file_id' => "photofileid#{id}" })
-          .to_return(status: 200, body: "{\"result\":{\"file_size\":3622849,\"file_id\":\"photofileid#{id}\",\"file_path\":\"photofile\"}}", headers: {})
+          .to_return(status: 200, body: "{\"result\":{\"file_size\":3622849,\"file_id\":\"photofileid#{id}\",\"file_path\":\"photofile\",\"file_unique_id\":\"file456\"}}", headers: {})
         stub_request(:get, "https://api.telegram.org/file/bot#{token}/photofile")
           .to_return(status: 200, body: 'photofile', headers: {})
       end