telegram-bot-ruby
telegram-bot-ruby copied to clipboard
how to substitute the https://api.telegram.org end-point with proxy?
Some clients for other end-points allow me to change the end-point being called with a proxy so that I can instrument each call to the API.
I looked at the conn method but it's private so I cannot access it.
Thoughts?
Unfortunately, there is no way to do it for now.
You can monkey patch like this:
--- /dev/null
+++ b/config/initializers/replace_telegram_api_url.rb
@@ -0,0 +1,16 @@
+module Telegram
+ module Bot
+ class Api
+ def conn
+ url = ENV['TELEGRAM_BOT_API_URL']
+ url ||= 'https://api.telegram.org'
+
+ @conn ||= Faraday.new(url: url) do |faraday|
+ faraday.request :multipart
+ faraday.request :url_encoded
+ faraday.adapter Telegram::Bot.configuration.adapter
+ end
+ end
+ end
+ end
+end
Could we create a pull request to enable the above but as a config to the method?
Now we need this!
Looks like time has come :(
here we are, guys from Russia)
Hey guys,
I don't get how changing the server URL could help fight censorship in Russia. Or are you trying to use an SSH tunnel as a proxy?
For me, replacing real Telegram server (at api.telegram.org) with a mock (at localhost) was necessary in order to run integration tests against the full stack of my Telegram bot.
If you are connecting to a real Telegram but suffer from regional censorship, you can overcome it by
- Using VPN or
- Using another type of proxy, e.g. SOCKS5. However Faraday does not support SOCKS5 out of the box. So the easiest way is going to be VPN.
Hey guys,
This https://github.com/atipugin/telegram-bot-ruby/pull/180 resolve problem with proxy.
Or you can add to your project same code
require 'socksify'
require 'socksify/http'
module Faraday
class Adapter
class NetHttp
def net_http_connection(env)
proxy = {uri: 'https://host:port',
user: login,
password: password,
socks: true}
if proxy
if proxy[:socks]
env[:ssl] = {verify: true}
sock_proxy(proxy)
else
env[:ssl] = {verify: false}
http_proxy(proxy)
end
else
Net::HTTP
end.new(env[:url].host, env[:url].port)
end
private
def sock_proxy(proxy)
proxy_uri = URI.parse(proxy[:uri])
TCPSocket.socks_username = proxy[:user] if proxy[:user]
TCPSocket.socks_password = proxy[:password] if proxy[:password]
Net::HTTP::SOCKSProxy(proxy_uri.host, proxy_uri.port)
end
def http_proxy(proxy)
proxy_uri = URI.parse(proxy[:uri])
Net::HTTP::Proxy(proxy_uri.host,
proxy_uri.port,
proxy_uri.user,
proxy_uri.password)
end
end
end
end
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.