{"id":19881805,"url":"https://github.com/daniel0130/ts-telegram-bot","last_synced_at":"2025-05-02T14:31:09.243Z","repository":{"id":259930505,"uuid":"876629608","full_name":"daniel0130/ts-telegram-bot","owner":"daniel0130","description":"This is a TypeScript wrapper for the Telegram Bot API Node.js and browsers. It allows you to easily interact with the Telegram Bot API using TypeScript. Check out the browser live demo here: StarExplorer.","archived":false,"fork":false,"pushed_at":"2024-10-22T09:46:33.000Z","size":1844,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-28T18:38:02.396Z","etag":null,"topics":["grammyjs","telegram-bot","telegram-bot-api","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daniel0130.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-22T09:46:05.000Z","updated_at":"2024-10-28T11:49:21.000Z","dependencies_parsed_at":"2024-10-28T18:50:11.959Z","dependency_job_id":null,"html_url":"https://github.com/daniel0130/ts-telegram-bot","commit_stats":null,"previous_names":["daniel0130/ts-telegram-bot"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel0130%2Fts-telegram-bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel0130%2Fts-telegram-bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel0130%2Fts-telegram-bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel0130%2Fts-telegram-bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daniel0130","download_url":"https://codeload.github.com/daniel0130/ts-telegram-bot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224315178,"owners_count":17290992,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grammyjs","telegram-bot","telegram-bot-api","typescript"],"created_at":"2024-11-12T17:15:19.950Z","updated_at":"2024-11-12T17:15:20.495Z","avatar_url":"https://github.com/daniel0130.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📦 typescript-telegram-bot-api\n[![CI](https://github.com/Borodin/typescript-telegram-bot-api/actions/workflows/ci.yml/badge.svg)](https://github.com/Borodin/typescript-telegram-bot-api/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/typescript-telegram-bot-api)](https://www.npmjs.com/package/typescript-telegram-bot-api)\n[![npm](https://img.shields.io/npm/dt/typescript-telegram-bot-api)](https://www.npmjs.com/package/typescript-telegram-bot-api)\n[![codecov](https://codecov.io/github/Borodin/typescript-telegram-bot-api/graph/badge.svg?token=509N5AZDTV)](https://codecov.io/github/Borodin/typescript-telegram-bot-api)\n[![GitHub](https://img.shields.io/badge/Bot_API-v7.10-0088cc)](https://core.telegram.org/bots/api#september-6-2024)\n\n\nThis is a TypeScript wrapper for the [Telegram Bot API](https://core.telegram.org/bots/api) Node.js and browsers. It allows you to easily interact with the Telegram Bot API using TypeScript.\nCheck out the browser live demo here: [StarExplorer](https://borodin.github.io/StarExplorer/).\n\n## Installation\n\n```bash\nnpm install typescript-telegram-bot-api\n```\n## Usage\n\n```typescript\nimport { TelegramBot } from 'typescript-telegram-bot-api';\n\nconst bot = new TelegramBot({ botToken: 'YOUR_BOT_TOKEN' });\nbot.startPolling();\n\nbot.on('message', (message) =\u003e {\n  console.log('Received message:', message.text);\n});\n\nbot.on('message:sticker', (message) =\u003e {\n  console.log('Received sticker:', message.sticker.emoji);\n});\n\nbot.getMe()\n  .then(console.log)\n  .catch(console.error);\n```\n\n## Supported Methods\nDescriptions of methods can be found at https://core.telegram.org/bots/api#available-methods.\nMethod names and parameters correspond to those in the official API documentation.\nEach method returns a Promise that resolves with data received from the API.\n\nIn case of an API error, the Promise will be rejected with a `TelegramError` containing the error code and description from the API.\nIf the API error includes a `retry_after` field, the library will retry the request after the specified number of seconds, until a response without an error is received. This behavior can be disabled by setting the `autoRetry` parameter to `false`.\n\nIf the error is not related to the API, the Promise will be rejected with a different error.\n\nFor sending files, you can use not only ```'file_id'``` or ```'url'```, but also ```stream.Readable``` or ```Buffer```.\nTo send files with additional parameters, such as a filename or specific contentType, use the ```FileOptions``` wrapper class.\n```typescript\nimport { TelegramBot, FileOptions } from 'typescript-telegram-bot-api';\nimport { createReadStream } from 'fs';\nimport { readFile } from 'fs/promises';\n\n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: 'AgACAgIAAxkDAAIF62Zq43...AgADcwADNQQ',\n  caption: 'file_id',\n});\n\n// or\n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: 'https://unsplash.it/640/480',\n  caption: 'url',\n});\n\n// or\n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: createReadStream('photo.jpg'),\n  caption: 'stream',\n});\n\n// or \n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: await readFile('photo.jpg'),\n  caption: 'buffer',\n});\n\n// or \n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: new FileOptions(\n    await readFile('photo.jpg'), {\n      filename: 'custom_file_name.jpg',\n      contentType: 'image/jpeg',\n    }\n  ),\n  caption: 'FileOptions',\n});\n\n// or in browser\n\nawait bot.sendPhoto({\n  chat_id: chat_id,\n  photo: input.files[0], // or new File(…)\n  caption: 'file',\n});\n```\n## Events\nTelegramBot is an EventEmitter that emits the [Update](https://core.telegram.org/bots/api#update) event and also emits events for each type of [Message](https://core.telegram.org/bots/api#message), such as `message:audio`, when the `audio` field is present in the message object.\n```typescript\nbot.on('message', (message) =\u003e {\n  console.log('Received message:', message.text);\n});\n\nbot.on('message_reaction', (messageReactionUpdated) =\u003e {\n  console.log('Received message_reaction:', messageReactionUpdated);\n});\n\nbot.on('message:audio', (message) =\u003e {\n  console.log('Received audio:', message.audio.file_id);\n});\n```\n\n## Webhooks\nTo use webhooks, you need to set up a server that will receive updates from Telegram. You can use the [express](https://www.npmjs.com/package/express) library for this purpose.\n\nThis example demonstrates a basic Telegram bot that responds with a reaction to any incoming message using Webhooks. The use of [ngrok](https://www.npmjs.com/package/ngrok) as a tunneling service simplifies the setup, allowing the bot to be easily deployed in various environments without complex network configuration. This approach is ideal for quick testing and development purposes. For production use, you should consider deploying the bot on a server with a public IP address and a valid SSL certificate.\n```typescript\nimport 'dotenv/config';\nimport * as ngrok from 'ngrok';\nimport express from \"express\";\nimport {TelegramBot} from \"./src\";\nimport {Update} from \"./src/types\";\n\nconst port = 3001;\n\nconst bot = new TelegramBot({\n  botToken: process.env.TEST_TELEGRAM_TOKEN as string,\n});\n\nbot.on('message', async (message) =\u003e {\n  await bot.setMessageReaction({\n    chat_id: message.chat.id,\n    message_id: message.message_id,\n    reaction: [{\n      type: 'emoji', emoji: '👍'\n    }]\n  });\n});\n\nconst app = express();\napp.use(express.json());\napp.post('/', async (req, res) =\u003e {\n  try {\n    await bot.processUpdate(req.body as Update);\n    res.sendStatus(200);\n  } catch (e) {\n    res.sendStatus(500);\n  }\n});\n\n(async () =\u003e {\n  app.listen(port, async () =\u003e {\n    const url = await ngrok.connect({\n      proto: 'http',\n      addr: port,\n    });\n    await bot.setWebhook({url});\n    console.log('Set Webhook to', url);\n  })\n})();\n\nprocess.on('SIGINT', async () =\u003e {\n  await bot.deleteWebhook();\n  await ngrok.disconnect();\n  console.log('Webhook deleted');\n});\n```\n\n\n## Tests\n```bash\nnpm test\n```\nCI/CD is set up with GitHub Actions. Tests and linters are run on every pull request.\n\nIf you want to run tests locally, follow the instructions in [tests/README.md](tests/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniel0130%2Fts-telegram-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaniel0130%2Fts-telegram-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniel0130%2Fts-telegram-bot/lists"}