Skip to content

add custom fetch#2091

Open
BataevDaniil wants to merge 3 commits into
telegraf:v4from
BataevDaniil:custom-fetch
Open

add custom fetch#2091
BataevDaniil wants to merge 3 commits into
telegraf:v4from
BataevDaniil:custom-fetch

Conversation

@BataevDaniil

@BataevDaniil BataevDaniil commented Apr 10, 2026

Copy link
Copy Markdown

This need for use proxy example https://proxy.com/telegram
And replace use fetch-node on undici
And add custom header
And etc

import { fetch } from 'undici'

  bot = new Telegraf(token, {
    telegram: {
      customFetch: (url, options) => {
        url.host = 'proxy.com'
        url.pathname = `/telegram${url.pathname}`

        if (options.signal) {
          const newSignal = new AbortController()
          const oldSignal = options.signal
          oldSignal.addEventListener('abort', () => {
            newSignal.abort()
          })
          options.signal = newSignal.signal
        }

        return fetch(url, {
          ...options,
          duplex: options.body ? 'half' : undefined,
          headers: {
            ...options.headers,
            Authorization: `Basic ${getEnv('PROXY_AUTH_TOKEN')}`,
          },
        })
      },
    },
  })

@MKRhere

MKRhere commented Apr 10, 2026

Copy link
Copy Markdown
Member

This is a nice, focused change.

Could you make two changes for me?

  1. Rename customFetch to just fetch.
  2. Add a @deprecated JSDoc to agent explaining to use fetch instead. The agent feature will be removed in future major versions of Telegraf in favour of custom fetch, because Telegraf will transition away from the node-fetch package to native platform fetch.

I have one more concern though. Using typeof fetch will most likely conflict if you try to pass native fetch from Node/Bun/Deno to Telegraf. But maybe this does not need to be addressed as of this PR. I might already have a solution for this in another branch.

@BataevDaniil

BataevDaniil commented Apr 10, 2026

Copy link
Copy Markdown
Author

I did what you asked.

And added the necessary minimum for fetch based on what is already in use.
@MKRhere

@siakinnik

Copy link
Copy Markdown

Hi! Upstream is dead since last year, so I've started a community fork telegraf-hardened. I'd love to have your PR there: https://github.com/siakinnik/telegraf-hardened/issues/1

@Leask

Leask commented May 5, 2026

Copy link
Copy Markdown

Covered by #2092 via commit 327d7d3: the branch adds a supported telegram.fetch option, verifies globalThis.fetch type compatibility, and tests both Bot API calls and URL attachments through custom fetch.

Tracking PR: #2092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants