Skip to content

Commit 02dd479

Browse files
lxy1992m1guelpf
andauthored
Feat: Add the config of debounce second, to control the edit rate (#28)
* Feat: Add the config of debounce second, to control the edit rate - Addtionally, add the config manifest into the README.md, and improve this file for better readability Co-authored-by: Miguel Piedrafita <github@miguelpiedrafita.com>
1 parent 8326b88 commit 02dd479

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
Go CLI to fuels a Telegram bot that lets you interact with [ChatGPT](https://openai.com/blog/chatgpt/), a large language model trained by OpenAI.
66

77
## Installation
8-
9-
Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest):
10-
8+
Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest).
119
- `chatgpt-telegram-Darwin-amd64`: macOS (Intel)
1210
- `chatgpt-telegram-Darwin-arm64`: macOS (M1)
1311
- `chatgpt-telegram-Linux-amd64`: Linux
1412
- `chatgpt-telegram-Linux-arm64`: Linux (ARM)
1513
- `chatgpt-telegram-Win-amd64`: Windows
1614

17-
After you download the file, extract it into a folder and open the `env.example` file with a text editor and fill in your credentials. You'll need your bot token, which you can find [here](https://core.telegram.org/bots/tutorial#obtain-your-bot-token), and optionally your telegram id, which you can find by DMing `@userinfobot` on Telegram. Save the file, and rename it to `.env`.
18-
15+
After you download the file, extract it into a folder and open the `env.example` file with a text editor and fill in your credentials.
16+
- `TELEGRAM_TOKEN`: Your Telegram Bot token
17+
- Follow [this guide](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) to create a bot and get the token.
18+
- `TELEGRAM_ID` (Optional): Your Telegram User ID
19+
- If you set this, only you will be able to interact with the bot.
20+
- To get your ID, message `@userinfobot` on Telegram.
21+
- `EDIT_WAIT_SECONDS` (Optional): Amount of seconds to wait between edits
22+
- This is set to `1` by default, but you can increase if you start getting a lot of `Too Many Requests` errors.
23+
- Save the file, and rename it to `.env`.
1924
> **Note** Make sure you rename the file to _exactly_ `.env`! The program won't work otherwise.
2025
2126
Finally, open the terminal in your computer (if you're on windows, look for `PowerShell`), navigate to the path you extracted the above file (you can use `cd dirname` to navigate to a directory, ask ChatGPT if you need more assistance 😉) and run `./chatgpt-telegram`.
@@ -24,7 +29,7 @@ Finally, open the terminal in your computer (if you're on windows, look for `Pow
2429

2530
By default, the program will launch a browser for you to sign into your account, and close it once you're signed in. If this setup doesn't work for you (there are issues with the browser starting, you want to run this in a computer with no screen, etc.), you can manually extract your session from your browser instead.
2631

27-
To do this, first sign into ChatGPT on your browser, then open the Developer Tools (right click anywhere in the page, then click "Inspect"), click on the Application tab and then on the Cookies section, and copy the value of the `__Secure-next-auth.session-token` cookie.
32+
To do this, first sign in to ChatGPT on your browser, then open the Developer Tools (right click anywhere in the page, then click "Inspect"), click on the Application tab and then on the Cookies section, and copy the value of the `__Secure-next-auth.session-token` cookie.
2833

2934
You will then have to create a config file in the following location depending on your OS (replace `YOUR_USERNAME_HERE` with your username:
3035

env.example

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
TELEGRAM_ID=
22
TELEGRAM_TOKEN=
3+
EDIT_WAIT_SECONDS=1

main.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func main() {
7070

7171
userConversations := make(map[int64]Conversation)
7272

73+
editInterval := 1 * time.Second
74+
if os.Getenv("EDIT_WAIT_SECONDS") != "" {
75+
editSecond, err := strconv.ParseInt(os.Getenv("EDIT_WAIT_SECONDS"), 10, 64)
76+
if err != nil {
77+
log.Printf("Couldn't convert your edit seconds setting into int: %v", err)
78+
editSecond = 1
79+
}
80+
editInterval = time.Duration(editSecond) * time.Second
81+
}
82+
7383
for update := range updates {
7484
if update.Message == nil {
7585
continue
@@ -99,7 +109,8 @@ func main() {
99109
debouncedType := ratelimit.Debounce((10 * time.Second), func() {
100110
bot.Request(tgbotapi.NewChatAction(update.Message.Chat.ID, "typing"))
101111
})
102-
debouncedEdit := ratelimit.DebounceWithArgs((1 * time.Second), func(text interface{}, messageId interface{}) {
112+
113+
debouncedEdit := ratelimit.DebounceWithArgs(editInterval, func(text interface{}, messageId interface{}) {
103114
_, err = bot.Request(tgbotapi.EditMessageTextConfig{
104115
BaseEdit: tgbotapi.BaseEdit{
105116
ChatID: msg.ChatID,

0 commit comments

Comments
 (0)