Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added http proxy support #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ end
* connection_keep_alive (optional, default is 30):
* connection_force_retry (optional, default is true):
* prefix (optional, default ''): prefix path for telegram messages.
* proxy_addr (optional): http proxy address
* proxy_port (optional, default 1080): http proxy port
* proxy_user (optional): http proxy user
* proxy_pass (optional, default ''): http proxy password

## Usage

Expand Down
42 changes: 27 additions & 15 deletions lib/telegram_bot_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def initialize(app, &_block)
raise ArgumentError.new("Config error: host can't be null || empty.") if @config.host.nil? || @config.host.empty?
raise ArgumentError.new("Config error: token can't be null || empty.") if @config.token.nil? || @config.token.empty?

# proxy settings
@def_options={}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

if ! @config.proxy_addr.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/NegatedIf: Favor unless over if for negative conditions.
Layout/SpaceAfterNot: Do not leave space between ! and its argument.

@def_options[:http_proxyaddr][email protected]_addr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

@def_options[:http_proxyport]=(@config.proxy_port || 1080).to_i

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

if ! @config.proxy_user.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/NegatedIf: Favor unless over if for negative conditions.
Layout/SpaceAfterNot: Do not leave space between ! and its argument.

@def_options[:http_proxyuser][email protected]_user

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

@def_options[:http_proxypass][email protected]_pass || ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end
# initialize persistent connection to telegram
self.class.persistent_connection_adapter pool_size: (@config.connection_pool_size || 2),
keep_alive: (@config.connection_keep_alive || 30),
Expand Down Expand Up @@ -80,7 +90,7 @@ def start_get_updates_thread
# start a new thread
Thread.new do
# the initial offset is always 0
@offset = 0
@offset = 0
# wait 5 seconds to don't risk to post message too early when the app is not still up
sleep 5
# loop forever
Expand All @@ -104,8 +114,8 @@ def start_get_updates_thread
def call(env)
dup._call(env)
end
def _call(env)

def _call(env)
# retrieve the request object
request = Rack::Request.new(env)

Expand Down Expand Up @@ -200,7 +210,7 @@ def _call(env)
@app.call(env)
end
end

# build command based on message
def get_command(params)
if params['message']
Expand All @@ -224,29 +234,29 @@ def get_command(params)
elsif params['inline_query']
return '/inline_query'
end
end
end

def process_hash_message(message, params)
if (message.include?(:multiple) && message[:multiple].is_a?(Array))
message[:multiple].each { |item| process_json_message(item, params) }
elsif (message.include?('multiple') && message['multiple'].is_a?(Array))
message['multiple'].each { |item| process_json_message(item, params) }
else
process_json_message(message, params)
process_json_message(message, params)
end
end

def process_json_message(message, params)
message[:chat_id] = params.message.chat.id unless message.include?(:chat_id) || message.include?('chat_id')

['reply_markup', :reply_markup].each do |item|
message[item] = message[item].to_json if message.include?(item)
end

['photo', :photo, 'audio', :audio, 'video', :video].each do |item|
message[item] = File.new(message[item]) if message.include?(item)
end

if message.include?(:text) || message.include?('text')
send_to_telegram('sendMessage', message)
elsif (message.include?(:latitude) and message.include?(:longitude)) || (message.include?('latitude') and message.include?('longitude'))
Expand All @@ -261,13 +271,15 @@ def process_json_message(message, params)
# TODO: invalid query
end
end

def send_to_telegram(path, query)
log_debug("Sending to chat: #{path} - #{query}")
self.class.post("/bot#{@config.token}/#{path}", query: query)
options=@def_options.clone

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

options[:query]=query

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.

self.class.post("/bot#{@config.token}/#{path}", options)
# TODO check response error and return response
end

def log_error(exception)
message = "Error: #{exception.message}\n#{exception.backtrace.join("\n")}\n"
log(:error, message)
Expand All @@ -280,7 +292,7 @@ def log_info(message)
def log_debug(message)
log(:debug, message)
end

# TODO: to fix env
def log(level, message)
#return if @env.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram_bot_middleware/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class TelegramBotMiddleware
VERSION = "0.3.2"
VERSION = "0.3.3"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

end