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 30cd60d commit 94d9c93
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/chatgpt-telegram.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@ Go CLI to fuels a Telegram bot that lets you interact with [ChatGPT](https://ope

## Installation

Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest). After you extract it, copy `env.example` to `.env` and fill in your Bot's details (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.
- Download the file corresponding to your OS in the [releases page](https://github.com/m1guelpf/chatgpt-telegram/releases/latest).
- After you extract it, copy `env.example` to `.env`
- Fill in your Bot's details
- 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```

## Usage

Run the `chatgpt-telegram` binary!

## Browserless Authentication
```bash
./chatgpt-telegram
```

By default, the program will launch a browser for you to sign into your account. If for whatever reason this isn't possible (compatibility issues, running on a server without a screen, etc.), you can manually provide your cookie.
## Browserless Authentication

To do this, first sign into ChatGPT on your browser, then open the Developer Tools, go to the Cookies section in the Application tab, and copy the value of the `__Secure-next-auth.session-token` cookie. Then, create a JSON file in your config dir (`/Users/[username]/Library/Application Support/chatgpt.json` in macOS, `C:\Users\[username]\AppData\Roaming\chatgpt.json` in Windows, `~/.config/chatgpt.json` in Linux), and write your cookie in the following format: `{ "openaisession": "YOUR_COOKIE_HERE" }`.
By default, the program will launch a browser for you to sign into your account.

If for whatever reason this isn't possible (compatibility issues, running on a server without a screen, etc.), you can manually provide your cookie.

To do this
- First sign in to ChatGPT on your browser
- Open the Developer Tools, go to the Cookies section in the Application tab
- Copy the value of the `__Secure-next-auth.session-token` cookie.
- Then, create a JSON file in your config dir
- MacOS
- `/Users/[username]/Library/Application Support/chatgpt.json`
- Windows
- `C:\Users\[username]\AppData\Roaming\chatgpt.json`
- Linux(Recommend Ubuntu, CentOS may not be able to install playwright)
- `~/.config/chatgpt.json`
- Write your cookie in the following format: `{ "openaisession": "YOUR_COOKIE_HERE" }`.

## License

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 94d9c93

Please sign in to comment.