Skip to content

Commit

Permalink
Feat: Add the config of debounce second, to control the edit rate (#28)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lxy1992 and m1guelpf authored Dec 9, 2022
1 parent 8326b88 commit 02dd479
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
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.
- `TELEGRAM_TOKEN`: Your Telegram Bot token
- Follow [this guide](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) to create a bot and get the token.
- `TELEGRAM_ID` (Optional): Your Telegram User ID
- If you set this, only you will be able to interact with the bot.
- To get your ID, message `@userinfobot` on Telegram.
- `EDIT_WAIT_SECONDS` (Optional): Amount of seconds to wait between edits
- This is set to `1` by default, but you can increase if you start getting a lot of `Too Many Requests` errors.
- 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=
EDIT_WAIT_SECONDS=1
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)

editInterval := 1 * time.Second
if os.Getenv("EDIT_WAIT_SECONDS") != "" {
editSecond, err := strconv.ParseInt(os.Getenv("EDIT_WAIT_SECONDS"), 10, 64)
if err != nil {
log.Printf("Couldn't convert your edit seconds setting into int: %v", err)
editSecond = 1
}
editInterval = 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(editInterval, func(text interface{}, messageId interface{}) {
_, err = bot.Request(tgbotapi.EditMessageTextConfig{
BaseEdit: tgbotapi.BaseEdit{
ChatID: msg.ChatID,
Expand Down

0 comments on commit 02dd479

Please sign in to comment.