Skip to content

preRequest

This hook is called before sending a request to the Telegram Bot API (allowing you to modify the parameters being sent).

Parameters

  • method - API method name
  • params - API method parameters

IMPORTANT

You must return the context from the hook handler!

Example

ts
import { 
Bot
} from "gramio";
const
bot
= new
Bot
(
process
.
env
.
BOT_TOKEN
as string).
preRequest
((
context
) => {
if (
context
.
method
=== "sendMessage") {
context
.
params
.
text
= "changed parameters";
} return
context
;
});
bot
.
start
();

Add hook only to specified API methods

ts
bot.preRequest("sendMessage", (context) => {
    context.params.text = "modified text";

    return context;
});
// or array
bot.preRequest(["sendMessage", "sendPhoto"], (context) => {
    if (context.method === "sendMessage") {
        context.params.text = "modified text";
    } else {
        context.params.caption = "this is a photo caption from sendPhoto method";
    }

    return context;
});