@effect-ak/tg-bot-client 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +96 -75
- package/dist/index.js +203 -133
- package/dist/index.mjs +202 -133
- package/package.json +1 -1
- package/readme.md +76 -43
package/readme.md
CHANGED
|
@@ -5,27 +5,25 @@
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## Motivation
|
|
9
9
|
|
|
10
|
-
The official documentation is available as a comprehensive [HTML page](https://core.telegram.org/bots/api), providing basic navigation. While functional, relying solely on this format can be somewhat inconvenient during bot development
|
|
10
|
+
The official documentation is available as a comprehensive [HTML page](https://core.telegram.org/bots/api), providing basic navigation. While functional, relying solely on this format can be somewhat inconvenient during bot **development**.
|
|
11
11
|
|
|
12
|
-
This client facilitates interaction with the Telegram Bot API
|
|
12
|
+
This client facilitates interaction with the **Telegram Bot API**. It was created primarily because **Telegram** does not offer an official TypeScript **SDK** for their **API**.
|
|
13
13
|
|
|
14
|
-
##
|
|
15
|
-
- **
|
|
16
|
-
- **[
|
|
17
|
-
- **Complete**: The entire API is generated from [the official documentation](https://core.telegram.org/bots/api) using a [code generator](./codegen/main.ts)
|
|
14
|
+
## Highlights:
|
|
15
|
+
- **[Client](#client)**: Light TypeScript client
|
|
16
|
+
- **[ChatBot runner](#chatbot-runner)**: Focus on the logic of your chat bot
|
|
17
|
+
- **Complete and Up-to-Date Telegram Bot API**: The entire API is generated from [the official documentation](https://core.telegram.org/bots/api) using a [code generator](./codegen/main.ts), ensuring this client remains in sync and supports every method and type provided by the **Telegram Bot API**.
|
|
18
18
|
- **Readable Method Names**: Method names, such as `setChatAdministratorCustomTitle`, are converted to snake_case for easier code readability, e.g., `set_chat_administrator_custom_title`.
|
|
19
19
|
- **Type Mapping**: Types from the documentation are converted to TypeScript types:
|
|
20
|
-
- `Integer`
|
|
21
|
-
- `True`
|
|
22
|
-
- `String or Number`
|
|
23
|
-
- Enumerated types, such as `Type of the chat
|
|
24
|
-
- And
|
|
20
|
+
- `Integer` → `number`
|
|
21
|
+
- `True` → `boolean`
|
|
22
|
+
- `String or Number` → `string | number`
|
|
23
|
+
- Enumerated types, such as `"Type of the chat can be either “private”, “group”, “supergroup” or “channel”"`, are converted to a standard union of literal types `"private" | "group" | "supergroup" | "channel"`
|
|
24
|
+
- And more...
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
#### Creating a Client
|
|
26
|
+
## Client
|
|
29
27
|
|
|
30
28
|
```typescript
|
|
31
29
|
import { makeTgBotClient } from "@effect-ak/tg-bot-client"
|
|
@@ -35,7 +33,7 @@ const client = makeTgBotClient({
|
|
|
35
33
|
});
|
|
36
34
|
```
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
### Executing api methods
|
|
39
37
|
|
|
40
38
|
`client` has an `execute` method which requires two arguments
|
|
41
39
|
|
|
@@ -93,16 +91,23 @@ const file =
|
|
|
93
91
|
});
|
|
94
92
|
```
|
|
95
93
|
|
|
96
|
-
|
|
94
|
+
## ChatBot runner
|
|
95
|
+
|
|
96
|
+
### How this library helps
|
|
97
|
+
|
|
98
|
+
A chatbot is essentially a **function** that is triggered by every user message, whether it's a text message, a reaction, or a payment update. Lets name this function as **handler function**
|
|
99
|
+
|
|
100
|
+
This library handles the task of reading these **updates** from the Telegram Bot API's **message queue**. It then invokes the appropriate **handler function** with the received update.
|
|
101
|
+
|
|
102
|
+
### Playground
|
|
97
103
|
|
|
98
|
-
|
|
104
|
+
Develop/Run chat bots in your browser via **[Chat Bot Playground](https://effect-ak.github.io/telegram-bot-playground/)**
|
|
99
105
|
|
|
100
|
-
|
|
106
|
+
### Local run
|
|
101
107
|
|
|
102
|
-
|
|
108
|
+
You can write the logic for your chatbot and **run it locally** and message to your bot via **Telegram** messenger.
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
- **Sequential Message Processing:** Messages in the queue are read one by one, and the handler is invoked for each message. If an error occurs in the handler, the next message remains in the queue, and the bot stops running. When the handler successfully processes a message, it proceeds to the next one.
|
|
110
|
+
Take a look at examples [here](example)
|
|
106
111
|
|
|
107
112
|
### Setup Instructions
|
|
108
113
|
|
|
@@ -123,38 +128,66 @@ The Telegram bot supports both push and pull notification models for messages. T
|
|
|
123
128
|
Create a file named `bot.js` and add your bot's logic as shown below:
|
|
124
129
|
|
|
125
130
|
```typescript
|
|
126
|
-
import { MESSAGE_EFFECTS, runTgChatBot } from "@effect-ak/tg-bot-client"
|
|
131
|
+
import { MESSAGE_EFFECTS, runTgChatBot, BotResponse } from "@effect-ak/tg-bot-client"
|
|
127
132
|
|
|
128
133
|
runTgChatBot({
|
|
129
134
|
type: "fromJsonFile",
|
|
130
135
|
on_message: (msg) => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
136
|
+
|
|
137
|
+
if (!msg.text) return BotResponse.ignore;
|
|
138
|
+
|
|
139
|
+
if (msg?.text === "bye") {
|
|
140
|
+
return BotResponse.make({
|
|
141
|
+
type: "message",
|
|
142
|
+
text: "See you later!",
|
|
143
|
+
message_effect_id: MESSAGE_EFFECTS["❤️"]
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return BotResponse.make({
|
|
148
|
+
type: "message",
|
|
149
|
+
text: "I'm a simple bot"
|
|
150
|
+
})
|
|
143
151
|
}
|
|
144
152
|
})
|
|
145
153
|
```
|
|
146
154
|
|
|
147
|
-
**Explanation:**
|
|
148
|
-
- **Import Statements:** Import necessary modules from the `@effect-ak/tg-bot-client` package.
|
|
149
|
-
- **`runTgChatBot` Function:** Initializes the Telegram chatbot using the configuration from the `config.json` file.
|
|
150
|
-
- **`on_message` Handler:** Defines the logic for handling incoming messages.
|
|
151
|
-
- If the message text is `"bye"`, the bot responds with `"See you later!"` and adds a heart emoji effect.
|
|
152
|
-
- For any other message, the bot responds with `"I'm a simple bot"`.
|
|
153
|
-
|
|
154
155
|
3. **Run the Bot**
|
|
155
156
|
|
|
156
157
|
To start your chatbot, execute the following command in your terminal:
|
|
157
158
|
|
|
158
159
|
```bash
|
|
159
160
|
node bot.js
|
|
160
|
-
```
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### How It Works: Pull Model
|
|
164
|
+
|
|
165
|
+
The Telegram bot supports both **push** and **pull** notification models for messages. This package uses the **pull** model for several reasons:
|
|
166
|
+
|
|
167
|
+
- **Run chat bots anywhere without exposing public ports or URLs:** The Telegram **push** model requires you as a developer to specify a public URL where updates will be sent.
|
|
168
|
+
For example, **pull** allows running chat bots in a web browser, which doesn't have a public URL.
|
|
169
|
+
|
|
170
|
+
- **Leveraging Telegram's infrastructure:** Telegram keeps new updates for 24 hours and gives you plenty of time to process them.
|
|
171
|
+
|
|
172
|
+
#### Few details and clarifications
|
|
173
|
+
|
|
174
|
+
Developer is responsible only for **Handler Function**.
|
|
175
|
+
|
|
176
|
+
**ChatBot runner** reads updates from the queue and shifts **ID** of last proceeded update so that **handler function** won't be triggered multiple times for the same update.
|
|
177
|
+
|
|
178
|
+
```mermaid
|
|
179
|
+
graph TD
|
|
180
|
+
HandlerFunction[/**Handler Function**/]
|
|
181
|
+
Library[**ChatBot runner**]
|
|
182
|
+
TgBot[Telegram chat bot]
|
|
183
|
+
MessageQueue[**Bot updates queue**<br>_api.telegram.org/bot/updates_]
|
|
184
|
+
User -->|Sends Update, e.g a text message| TgBot
|
|
185
|
+
TgBot -->|Stores Update for 24 hours | MessageQueue
|
|
186
|
+
|
|
187
|
+
subgraph Pull Updates
|
|
188
|
+
Library -->|Fetches Update | MessageQueue
|
|
189
|
+
Library -->|Invokes| HandlerFunction
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
```
|