Skip to main content
Home

@smol/gram@3.0.0
Built and signed on GitHub Actions

Tiny (smol) Telegram Bot API client library with TypeScript types

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (3.0.0)

Main Telegram Bot API client module.

To begin, initialize your bot with initTgBot.

Examples

Reply to private commands.

const bot = initTgBot({ botToken: "YOUR_TOKEN" });

for await (const update of bot.listUpdates()) {
  if (update.message) {
    const { chat, text, message_id, from } = update.message;

    if (chat.type === "private" && text?.startsWith("/start")) {
      await bot.sendMessage({
        chat_id: chat.id,
        reply_parameters: { message_id },
        text: `Hello, ${from?.first_name}!`
      });
    }
  }
}

Say hello when added to a group.

const bot = initTgBot({ botToken: "YOUR_TOKEN" });

for await (const update of bot.listUpdates()) {
  if (update.my_chat_member) {
    const { chat, new_chat_member } = update.my_chat_member;
    if (
      (chat.type === "group" || chat.type === "supergroup") &&
      (new_chat_member.status === "member" || new_chat_member.status === "administrator")
    ) {
      await bot.sendMessage({ chat_id: chat.id, text: "Hello, everyone!" });
    }
  }
}

Download and show/save bot's profile photo.

/// <reference lib="dom" />
import { writeFile } from "node:fs/promises"

const bot = initTgBot({ botToken: "YOUR_TOKEN" });

// Download bot's profile photo
const botUser = await bot.getMe();

const botPhoto = await bot.getUserProfilePhotos({ user_id: botUser.id });
const botPhotoFileId = botPhoto.photos[0]?.[0]?.file_id;
if (botPhotoFileId == null) throw new Error("No profile photo");

const botPhotoFile = await bot.getFile({ file_id: botPhotoFileId });
if (botPhotoFile.file_path == null) throw new Error("Photo file unavailable");

const botPhotoBlob = await bot.getFileData(botPhotoFile.file_path);

// Save to file
await writeFile("bot.jpg", botPhotoBlob.stream());

// Set as img src
const img = document.createElement("img");
img.src = URL.createObjectURL(botPhotoBlob);
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@smol/gram

Import symbol

import * as gram from "@smol/gram";
or

Import directly with a jsr specifier

import * as gram from "jsr:@smol/gram";

Add Package

pnpm i jsr:@smol/gram
or (using pnpm 10.8 or older)
pnpm dlx jsr add @smol/gram

Import symbol

import * as gram from "@smol/gram";

Add Package

yarn add jsr:@smol/gram
or (using Yarn 4.8 or older)
yarn dlx jsr add @smol/gram

Import symbol

import * as gram from "@smol/gram";

Add Package

vlt install jsr:@smol/gram

Import symbol

import * as gram from "@smol/gram";

Add Package

npx jsr add @smol/gram

Import symbol

import * as gram from "@smol/gram";

Add Package

bunx jsr add @smol/gram

Import symbol

import * as gram from "@smol/gram";