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 "https://deno.land/x/grammy@v1.30.0/mod.ts";
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({
hostname: host, // put in the proxy host
port: port, // put in the proxy port
});
const bot = new Bot("", {
client: {
baseFetchConfig: {
agent: socksAgent,
compress: true,
},
},
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.