Proxy Support
grammY lets you configure a number of things about how network requests are performed. This includes injecting a custom payload into every request, which can be used to install a proxy agent. Check out the Api in the grammY API Reference.
In Deno, here is how you would use an http proxy:
import { Bot } from "npm:grammy";
const client = Deno.createHttpClient({
proxy: { url: "http://host:port/" },
});
const bot = new Bot("", {
client: {
baseFetchConfig: {
// @ts-ignore
client,
},
},
});2
3
4
5
6
7
8
9
10
11
12
13
Note that you need to run this with the
-flag.-unstable
In Node.js, here is how you would use a proxy with the socks package (npm):
import { Bot } from "grammy";
import { SocksProxyAgent } from "socks-proxy-agent";
const socksAgent = new SocksProxyAgent("socks5://host:port", {
timeout: 60_000, // close stalled connections after 60 seconds
});
const bot = new Bot("", {
client: {
baseFetchConfig: {
agent: socksAgent,
compress: true,
},
},
});2
3
4
5
6
7
8
9
10
11
12
13
14
15
Be sure to specify the timeout option for the agent. It causes the connection to be dropped if no data was sent or received for the given number of milliseconds. This is important because connections can die silently, for example when the SSH tunnel behind a SOCKS proxy (ssh ) loses its connection. grammY has its own timeout of 500 seconds which cancels the request, but it is unable to drop the underlying connections. Setting this option fixes that.
Note that specifying
compress:is an optional performance optimization. It has nothing to do with proxy support. It is part of the default value fortrue base, so if you still want it, you should specify it again.Fetch Config
Getting a proxy to work can be difficult. Contact us in the Telegram chat if you run into issues, or if you need grammY to support further configuration options. We also have a Russian Telegram chat.