# teletype-telegram-bot-api

> A Telegram bot API written with TypeScript.

Latest version **1.1.0** (published 2020-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install teletype-telegram-bot-api
pnpm add teletype-telegram-bot-api
yarn add teletype-telegram-bot-api
bun add teletype-telegram-bot-api
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-07-01 |
| First published | 2020-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 30.4 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | Weikeup |
| Maintainers | weikeup |
| Keywords | teletype, telegram, telegram bot api, bot api |

## Links

- npm: https://www.npmjs.com/package/teletype-telegram-bot-api
- Repository: https://gitlab.com/weikeup/teletype-telegram-bot-api
- Homepage: https://gitlab.com/weikeup/teletype-telegram-bot-api#readme
- Issues: https://gitlab.com/weikeup/teletype-telegram-bot-api/issues
- npm.io page: https://npm.io/package/teletype-telegram-bot-api

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.19.2
- [telegram-typings](https://npm.io/package/telegram-typings.md) ^4.0.0-beta

## Recent versions

- 1.1.0 (latest) — 2020-07-01
- 1.0.0 — 2020-06-29
- 0.0.7 — 2020-06-29
- 0.0.6 — 2020-06-28
- 0.0.5 — 2020-06-28
- 0.0.4 — 2020-06-28
- 0.0.3 — 2020-06-28
- 0.0.2 — 2020-06-27
- 0.0.1 — 2020-06-25

## README

# Teletype - A Telegrm Bot API

An easy way to use Telegram bot with Typescript.

## Features
- [x] Long Polling mode
- [x] Webhook mode
- [x] Subscribe events
- [x] Send message
- [x] Send sticker
- [ ] Send media (files, photos, videos...etc)
- [ ] Send keyboard
- [ ] Inline mode event

## Dependency package

- Axios
- Telegram-typings

## How to use it

### 1. Install package

```shell
npm i teletype-telegram-bot-api
```

### 2. Create object

Import `Teletype` and `TgEvents` from the package.
```typescript
import { Teletype, TgEvents } from 'teletype-telegram-bot-api'
```

Then create a Teletype object with a token of your bot.
```typescript
const bot = new Teletype(botToken)
```

### 3. Subscribe event

Use these functions to subscribe events.

- `on(TgEvents, EventId, Triggered function)`  
If user send a sticker to bot, you can subscribe a sticker event like this (See more events in [TgEvents.ts](./src/TgEvents.ts)):
    ```typescript
    bot.on(TgEvents.STICKER, "name of event", async (update) => {
        // Do something here...
    })
    ```

- `onText(RegExStr, Triggered function)`  
If the text sent by user matches with the regular expression, then the function you send in will be called.
    ```typescript
    bot.onText("[h|H][i|I]", async (message, update) => {
        // Do something here...
    })
    ```

- `onCommand(RegExStr, Triggered function)`  
If the command sent by user matches with the regular expression, then the function you send in will be called.
    ```typescript
    bot.onCommand("start", async (message, update) => {
        // Do something here...
    })
    ```

- `always(Triggered function)`  
Whatever user send, always call this function.
    ```typescript
    bot.always(async (update) => {
        // Do something here...
    })
    ```

### 4. Process Update

#### Webhook
> If you did not set webhook yet, call `setWebhook(url)` to set it.

Call `processUpdate` and pass whole body of request.
```typescript
bot.processUpdate(request.body)
```

#### Long Polling
> If you have already set webhook, please delete it first by calling `deleteWebhook`.
 
Use `getUpdates()` to get the latest updates and pass each `Update` object to `processUpdate`.
```typescript
const updates = await bot.getUpdates()
updates.forEach(update=>{
        bot.processUpdate(update)
})
```

### 5. Send something

- `sendMessage(ChatId, Text)`  
Send a message to a chat room.
```typescript
await bot.sendMessage(chatId, "Hello~")
```

- `sendSticker(ChatId, StickerFileId)`  
Send a sticker to a chat room.
```typescript
await bot.sendSticker(chatId, "Sticker file_id")
```

---
_Source: https://npm.io/package/teletype-telegram-bot-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
