TelegramBotClient
allows you to use a proxy for Bot API connections. This guide covers using three different proxy solutions.
If you are in a country, such as Iran, where HTTP and SOCKS proxy connections to Telegram servers are blocked, consider using a VPN, using Tor Network, or hosting your bot in other jurisdictions.
You can configure HttpClient
with WebProxy
and pass it to the Bot client.
{{#include ../../Examples/4/Proxy.cs:usings}}
{{#include ../../Examples/4/Proxy.cs:http-proxy-client}}
As of .NET 6, SocketsHttpHandler
is able to use Socks4, Socks4a and Socks5 proxies!
{{#include ../../Examples/4/Proxy.cs:usings}}
{{#include ../../Examples/4/Proxy.cs:socks-proxy-client}}
Warning: Use for Testing only!
Do not use this method in a production environment as it has high network latency and poor bandwidth.
Using Tor, a developer can avoid network restrictions while debugging and testing the code before a production release.
-
Install Tor Browser
-
Open the
torcc
file with a text editor (Found inTor Browser\Browser\TorBrowser\Data\Tor
) -
Add the following lines: (configurations are described below)
EntryNodes {NL} ExitNodes {NL} StrictNodes 1 SocksPort 127.0.0.1:9050
-
Look at the Socks5 proxy example above.
-
Start the Tor Browser
Usage:
{{#include ../../Examples/4/Proxy.cs:usings}}
{{#include ../../Examples/4/Proxy.cs:tor-proxy-client}}
Note that Tor has to be active at all times for the bot to work.
EntryNodes {NL}
ExitNodes {NL}
StrictNodes 1
These three lines make sure you use nodes from the Netherlands as much as possible to reduce latency.
SocksPort 127.0.0.1:9050
This line tells tor to listen on port 9050 for any socks connections. You can change the port to anything you want (9050 is just the default), only make sure to use the same port in your code.