diff --git a/CHANGELOG.md b/CHANGELOG.md index 3383b49..2b1ce8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.7.2 + +* Increase timeout in CreateChat command +* Add proxy support for tdlib +* Add proxy support for bot + # 0.7.1 * Use tdlib-ruby 0.9.0 diff --git a/app/views/settings/_telegram_common.html.erb b/app/views/settings/_telegram_common.html.erb index af7ebb1..0ea3fff 100644 --- a/app/views/settings/_telegram_common.html.erb +++ b/app/views/settings/_telegram_common.html.erb @@ -53,6 +53,68 @@
+

Tdlib proxy (SOCKS5)

+ +

+ + <%= check_box_tag 'settings[use_proxy]', '1', @settings['use_proxy'] %> +

+ +

+ + <%= text_field_tag 'settings[proxy_server]', @settings['proxy_server'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[proxy_port]', @settings['proxy_port'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[proxy_user]', @settings['proxy_user'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[proxy_password]', @settings['proxy_password'], size: 50 %> +

+ +
+ +

Bot proxy

+ +

+ + <%= check_box_tag 'settings[bot_use_proxy]', '1', @settings['bot_use_proxy'] %> +

+ +

+ + <%= text_field_tag 'settings[bot_proxy_server]', @settings['bot_proxy_server'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[bot_proxy_port]', @settings['bot_proxy_port'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[bot_proxy_user]', @settings['bot_proxy_user'], size: 50 %> +

+ +

+ + <%= text_field_tag 'settings[bot_proxy_password]', @settings['bot_proxy_password'], size: 50 %> +

+ +
+

WebHooks

diff --git a/config/locales/en.yml b/config/locales/en.yml index 9b5c325..325c3a3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -36,6 +36,7 @@ en: bot_deinit: Deinitialize bot get_updates_hint: bundle exec rake telegram_common:bot web_hooks_warning: Need https to setup WebHooks + use_proxy: Use proxy client: authorize: success: "Client is authorized" diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 72c3db2..ca16194 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -36,6 +36,7 @@ ru: bot_deinit: Деинициализировать бота get_updates_hint: bundle exec rake telegram_common:bot web_hooks_warning: Нужен https протокол, чтобы настроить WebHooks + use_proxy: Использовать прокси client: authorize: success: Клиент авторизован diff --git a/init.rb b/init.rb index 736bc40..ba1924a 100644 --- a/init.rb +++ b/init.rb @@ -2,9 +2,6 @@ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir) -PHANTOMJS_CONFIG = File.expand_path('../config/phantom-proxy.js', __FILE__) -TELEGRAM_CLI_LOG = Logger.new(Rails.root.join(log_dir, 'telegram-cli.log')) - require 'telegram/bot' # Rails 5.1/Rails 4 @@ -16,6 +13,12 @@ Dir.glob(File.dirname(__FILE__) + paths).each do |file| require_dependency file end + + Faraday::Adapter.register_middleware redmine_telegram_common: TelegramCommon::Bot::FaradayAdapter + + Telegram::Bot.configure do |config| + config.adapter = :redmine_telegram_common + end end Rails.application.config.eager_load_paths += Dir.glob("#{Rails.application.config.root}/plugins/redmine_telegram_common/{lib,app/workers,app/models,app/controllers}") @@ -29,7 +32,7 @@ Redmine::Plugin.register :redmine_telegram_common do name 'Redmine Telegram Common plugin' description 'This is a plugin for other Redmine Telegram plugins' - version '0.7.1' + version '0.7.2' url 'https://github.com/centosadmin/redmine_telegram_common' author 'Southbridge' author_url 'https://github.com/centosadmin' diff --git a/lib/telegram_common/bot/faraday_adapter.rb b/lib/telegram_common/bot/faraday_adapter.rb new file mode 100644 index 0000000..59d7dd3 --- /dev/null +++ b/lib/telegram_common/bot/faraday_adapter.rb @@ -0,0 +1,24 @@ +module TelegramCommon + class Bot::FaradayAdapter < Faraday::Adapter::NetHttp + def net_http_connection(env) + if proxy = fetch_proxy_from_settings + Net::HTTP::Proxy(proxy[:server], proxy[:port], proxy[:user], proxy[:password]) + else + Net::HTTP + end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)) + end + + private + + def fetch_proxy_from_settings + settings = Setting.plugin_redmine_telegram_common + return unless settings['bot_use_proxy'] + { + server: settings['bot_proxy_server'], + port: settings['bot_proxy_port'], + user: settings['bot_proxy_user'], + password: settings['bot_proxy_password'] + } + end + end +end diff --git a/lib/telegram_common/bot/router.rb b/lib/telegram_common/bot/router.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/telegram_common/tdlib/create_chat.rb b/lib/telegram_common/tdlib/create_chat.rb index afd1b83..bc67829 100644 --- a/lib/telegram_common/tdlib/create_chat.rb +++ b/lib/telegram_common/tdlib/create_chat.rb @@ -1,19 +1,15 @@ module TelegramCommon::Tdlib class CreateChat < Command def call(title, user_ids) - @client.on_ready(timeout: 5) do |client| + @client.on_ready(timeout: 10) do |client| user_ids.each do |id| client.broadcast_and_receive('@type' => 'getUser', 'user_id' => id) end - sleep 1 - chat = client.broadcast_and_receive('@type' => 'createNewBasicGroupChat', 'title' => title, 'user_ids' => user_ids) - sleep 1 - client.broadcast_and_receive('@type' => 'toggleBasicGroupAdministrators', 'basic_group_id' => chat.dig('type', 'basic_group_id'), 'everyone_is_administrator' => false) diff --git a/lib/telegram_common/tdlib/dependency_providers.rb b/lib/telegram_common/tdlib/dependency_providers.rb index 7b677a9..f427704 100644 --- a/lib/telegram_common/tdlib/dependency_providers.rb +++ b/lib/telegram_common/tdlib/dependency_providers.rb @@ -10,7 +10,14 @@ def client database_directory: Rails.root.join('tmp', 'redmine_telegram_common', 'tdlib', 'db').to_s, files_directory: Rails.root.join('tmp', 'redmine_telegram_common', 'tdlib', 'files').to_s, } - TD::Client.new(config) + proxy = { + '@type' => 'proxySocks5', + 'server' => settings['proxy_server'], + 'port' => settings['proxy_port'], + 'username' => settings['proxy_user'], + 'password' => settings['proxy_password'] + } + TD::Client.new(**(settings['use_proxy'] ? { proxy: proxy } : {}), **config) end end