Response Object (res)¶
The res instance sends custom HTTP responses from webhook and Webapp commands.
It lets your bot behave like an HTTP API or web endpoint. You can return JSON, HTML, XML, plain text, redirects, and rendered templates (EJS).
Once a response is sent with res, command execution ends.
Availability
The res instance is available in webhook commands and Webapp commands — not in normal Telegram message commands.
Response Methods¶
set(key, value)¶
Sets HTTP headers on the response.
Headers can be chained.
status(code)¶
Sets the HTTP response status code.
You can chain this with other methods.
send(body)¶
Sends a response with any content type.
res.send("Hello World")
res.send({ message: "Success", data: { id: 1 } })
res.status(201).send("Resource created")
The content type is auto-detected.
json(obj)¶
Sends a JSON response with application/json content type.
This is the most common method for API-style responses.
html(content)¶
Sends an HTML response.
If EJS tags are detected, the content is automatically rendered.
With EJS rendering:
res.html(`
<h1>Welcome <%= user.first_name %></h1>
<p>Your ID: <%= user.id %></p>
<% if (user.premium) { %>
<div class='premium'>Premium User</div>
<% } %>
`)
xml(content)¶
Sends an XML response with application/xml content type.
text(content)¶
Sends a plain text response.
EJS templates are auto-rendered if detected.
res.text("This is plain text")
const textTemplate = "Hello <%= name %>, welcome!"
res.text(textTemplate)
redirect(url)¶
Redirects the request to another URL or command.
Redirect responses are sent immediately.
render(commandPath, options)¶
Renders another command's output as the response.
Content type is detected automatically based on the target.
With options:
Rendering an HTML command:
For advanced res.render() usage, see the res.render() Guide.
Notes¶
- Sending a response ends execution
- Headers, status, and body can be chained
- EJS rendering works automatically for HTML and text
- If no response is sent, a default 2xx response is returned
- The
userobject in templates is available in user-based webhooks, not in public Webapp URLs