@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/readme.md CHANGED
@@ -5,27 +5,25 @@
5
5
 
6
6
 
7
7
 
8
- ### Motivation
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. It was created primarily because Telegram does not offer an official SDK for their 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
- ## Features:
15
- - **Typesafe Client**: This is a clean client written in TypeScript with no abstractions.
16
- - **[Playground](https://effect-ak.github.io/telegram-bot-playground/)**: Develop/Run chat bots in your browser
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` becomes `number`
21
- - `True` becomes `boolean`
22
- - `String or Number` becomes `string | number`
23
- - Enumerated types, such as `Type of the chat, can be either “private”, “group”, “supergroup” or “channel”` becomes a standard union of literal types `"private"| "group" | "supergroup" | "channel"`
24
- - And so on
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
- ### Usage example
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
- #### Executing api methods
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
- ### Chatbot Support
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
- You can write the logic for your chatbot and run it locally.
104
+ Develop/Run chat bots in your browser via **[Chat Bot Playground](https://effect-ak.github.io/telegram-bot-playground/)**
99
105
 
100
- Take a look at [example](./example/echo-bot.ts)
106
+ ### Local run
101
107
 
102
- The Telegram bot supports both push and pull notification models for messages. This package uses the **pull** model for several reasons:
108
+ You can write the logic for your chatbot and **run it locally** and message to your bot via **Telegram** messenger.
103
109
 
104
- - **Flexibility in Handler Deployment:** Allows you to run the bot handler on any JS platform (NodeJs, Browser)
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
- if (msg?.text === "bye") {
132
- return {
133
- type: "message",
134
- text: "See you later!",
135
- message_effect_id: MESSAGE_EFFECTS["❤️"]
136
- }
137
- }
138
-
139
- return {
140
- type: "message",
141
- text: "I'm a simple bot"
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
+ ```