Skip to content

Commit

Permalink
Feat: Add the config of debounce second, to control the edit rate
Browse files Browse the repository at this point in the history
- Addtionally, add the config manifest into the README.md, and improve
  this file for better readability
  • Loading branch information
lxy1992 committed Dec 7, 2022
1 parent 8326b88 commit 6765ea5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
vendor
.idea/*
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
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.

## Installation

Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest):

- Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest).
- `chatgpt-telegram-Darwin-amd64`: macOS (Intel)
- `chatgpt-telegram-Darwin-arm64`: macOS (M1)
- `chatgpt-telegram-Linux-amd64`: Linux
- `chatgpt-telegram-Linux-arm64`: Linux (ARM)
- `chatgpt-telegram-Win-amd64`: Windows

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`.

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(`TELEGRAM_TOKEN`)
- Which you can find [here](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)
- Optionally, your telegram id(`TELEGRAM_ID`)
- Which you can find by DMing `@userinfobot` on Telegram. It can limit the user of this bot.
- Optionally, your debounced edit seconds (`DEBOUNCED_EDIT_WAIT_SECONDS`)
- It controls the edit rate of sending telegram message. When you meet errors like below, increase the number:
- ```Couldn't edit message: Too Many Requests: retry after 30```
- Save the file, and rename it to `.env`.
> **Note** Make sure you rename the file to _exactly_ `.env`! The program won't work otherwise.
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`.
Expand All @@ -24,7 +29,7 @@ Finally, open the terminal in your computer (if you're on windows, look for `Pow

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.

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.
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.

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

Expand Down
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
TELEGRAM_ID=
TELEGRAM_TOKEN=
DEBOUNCED_EDIT_WAIT_SECONDS=
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func main() {

userConversations := make(map[int64]Conversation)

debouncedEditSecond := 1 * time.Second
if os.Getenv("DEBOUNCED_EDIT_WAIT_SECONDS") != "" {
editSecond, err := strconv.ParseInt(os.Getenv("DEBOUNCED_EDIT_WAIT_SECONDS"), 10, 64)
if err != nil {
log.Printf("Couldn't convert your debounced edit second setting into int: %v", err)
editSecond = 1
}
debouncedEditSecond = time.Duration(editSecond) * time.Second
}

for update := range updates {
if update.Message == nil {
continue
Expand Down Expand Up @@ -99,7 +109,8 @@ func main() {
debouncedType := ratelimit.Debounce((10 * time.Second), func() {
bot.Request(tgbotapi.NewChatAction(update.Message.Chat.ID, "typing"))
})
debouncedEdit := ratelimit.DebounceWithArgs((1 * time.Second), func(text interface{}, messageId interface{}) {

debouncedEdit := ratelimit.DebounceWithArgs(debouncedEditSecond, func(text interface{}, messageId interface{}) {
_, err = bot.Request(tgbotapi.EditMessageTextConfig{
BaseEdit: tgbotapi.BaseEdit{
ChatID: msg.ChatID,
Expand Down

0 comments on commit 6765ea5

Please sign in to comment.