Skip to content

Commit

Permalink
Merge pull request #76 from centosadmin/develop
Browse files Browse the repository at this point in the history
Release 0.7.2
  • Loading branch information
vladislav-yashin authored May 8, 2018
2 parents 085d5f0 + 7dad316 commit d78fbd4
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 62 additions & 0 deletions app/views/settings/_telegram_common.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,68 @@

<hr>

<h3>Tdlib proxy (SOCKS5)</h3>

<p>
<label>
<%= t 'telegram_common.settings.use_proxy' %>
</label>
<%= check_box_tag 'settings[use_proxy]', '1', @settings['use_proxy'] %>
</p>

<p>
<label for="settings_telegram_proxy_server">Server</label>
<%= text_field_tag 'settings[proxy_server]', @settings['proxy_server'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_port">Proxy port</label>
<%= text_field_tag 'settings[proxy_port]', @settings['proxy_port'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_user">Proxy user</label>
<%= text_field_tag 'settings[proxy_user]', @settings['proxy_user'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_password">Proxy password</label>
<%= text_field_tag 'settings[proxy_password]', @settings['proxy_password'], size: 50 %>
</p>

<hr>

<h3>Bot proxy</h3>

<p>
<label>
<%= t 'telegram_common.settings.use_proxy' %>
</label>
<%= check_box_tag 'settings[bot_use_proxy]', '1', @settings['bot_use_proxy'] %>
</p>

<p>
<label for="settings_telegram_proxy_server">Server</label>
<%= text_field_tag 'settings[bot_proxy_server]', @settings['bot_proxy_server'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_port">Proxy port</label>
<%= text_field_tag 'settings[bot_proxy_port]', @settings['bot_proxy_port'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_user">Proxy user</label>
<%= text_field_tag 'settings[bot_proxy_user]', @settings['bot_proxy_user'], size: 50 %>
</p>

<p>
<label for="settings_telegram_proxy_password">Proxy password</label>
<%= text_field_tag 'settings[bot_proxy_password]', @settings['bot_proxy_password'], size: 50 %>
</p>

<hr>

<div style="display: flex; align-items: center;">
<div style="flex: 1; padding: 0.5em;">
<h3>WebHooks</h3>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: Клиент авторизован
Expand Down
11 changes: 7 additions & 4 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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'
Expand Down
24 changes: 24 additions & 0 deletions lib/telegram_common/bot/faraday_adapter.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed lib/telegram_common/bot/router.rb
Empty file.
6 changes: 1 addition & 5 deletions lib/telegram_common/tdlib/create_chat.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 8 additions & 1 deletion lib/telegram_common/tdlib/dependency_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d78fbd4

Please sign in to comment.