telegram-bot-ruby icon indicating copy to clipboard operation
telegram-bot-ruby copied to clipboard

how to substitute the https://api.telegram.org end-point with proxy?

Open timfong888 opened this issue 9 years ago • 8 comments

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?

timfong888 avatar Jun 21 '16 23:06 timfong888

Unfortunately, there is no way to do it for now.

atipugin avatar Jun 22 '16 18:06 atipugin

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

aspotashev avatar Jul 24 '16 19:07 aspotashev

Could we create a pull request to enable the above but as a config to the method?

timfong888 avatar Jul 27 '16 18:07 timfong888

Now we need this!

fetsh avatar Apr 16 '18 12:04 fetsh

Looks like time has come :(

atipugin avatar Apr 16 '18 20:04 atipugin

here we are, guys from Russia)

rikitikitav1 avatar Apr 17 '18 07:04 rikitikitav1

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

  1. Using VPN or
  2. 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.

aspotashev avatar Apr 17 '18 10:04 aspotashev

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

lenferer avatar Apr 18 '18 16:04 lenferer

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.

github-actions[bot] avatar Feb 19 '23 15:02 github-actions[bot]