From 473b4159991f03fa9a06d5bdd348f0369ce37e51 Mon Sep 17 00:00:00 2001 From: kravetsone <57632712+kravetsone@users.noreply.github.com> Date: Sun, 24 Nov 2024 20:09:00 +0300 Subject: [PATCH] chore: add onResponse and onResponseError --- docs/hooks/on-response-error.md | 34 ++++++++++++++++++++++++++++++- docs/hooks/on-response.md | 36 ++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/docs/hooks/on-response-error.md b/docs/hooks/on-response-error.md index 4640904..a1ae1fe 100644 --- a/docs/hooks/on-response-error.md +++ b/docs/hooks/on-response-error.md @@ -1 +1,33 @@ -# TODO +# onResponseError + +This hook called `after receiving a error response` from Telegram Bot API. + +## Parameters + +[TelegramError](https://jsr.io/@gramio/core@latest/doc/~/TelegramError) + + + +## Example + +```ts twoslash +import { Bot } from "gramio"; + +const bot = new Bot(process.env.BOT_TOKEN as string).onResponseError( + (context) => { + console.log("Error for", context.method, context.message); + } +); +``` + +### Add hook only to specified API methods + +```ts +bot.onResponseError("sendMessage", (context) => { + console.log("Error for sendMessage", context.message); +}); +// or array +bot.onResponseError(["sendMessage", "sendPhoto"], (context) => { + console.log("Error for", context.method, context.message); +}); +``` diff --git a/docs/hooks/on-response.md b/docs/hooks/on-response.md index 4640904..39f7459 100644 --- a/docs/hooks/on-response.md +++ b/docs/hooks/on-response.md @@ -1 +1,35 @@ -# TODO +# onResponse + +This hook called `after receiving a successful response` from Telegram Bot API. + +## Parameters + +Object with: + +- method - API method name +- params - API method params +- response - response + + + +## Example + +```ts twoslash +import { Bot } from "gramio"; + +const bot = new Bot(process.env.BOT_TOKEN as string).onResponse((context) => { + console.log("response for", context.method, context.response); +}); +``` + +### Add hook only to specified API methods + +```ts +bot.onResponse("sendMessage", (context) => { + console.log("response for sendMessage", context.response); +}); +// or array +bot.preRequest(["sendMessage", "sendPhoto"], (context) => { + console.log("response for", context.method, context.response); +}); +```