-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 71f6335
Showing
15 changed files
with
1,342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: release | ||
|
||
on: | ||
pull_request: | ||
push: | ||
# run only against tags | ||
tags: | ||
- "*" | ||
|
||
permissions: | ||
contents: write | ||
# packages: write | ||
# issues: write | ||
|
||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
|
||
- name: Run GoReleaser | ||
uses: goreleaser/goreleaser-action@v6 | ||
with: | ||
version: "~> v2" | ||
args: release --clean | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<p align="center"> | ||
<img src="assets/openai-telegram-logo.webp" alt="OpenAI Telegram logo" height="100px"> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://github.com/TheoBrigitte/chatgpt-telegram-bot/releases"><img src="https://img.shields.io/github/release/TheoBrigitte/chatgpt-telegram-bot.svg" alt="Github release"></a> | ||
<a href="https://github.com/TheoBrigitte/chatgpt-telegram-bot/actions/workflows/build.yml"><img src="https://github.com/TheoBrigitte/chatgpt-telegram-bot/actions/workflows/build.yml/badge.svg" alt="Github action"></a> | ||
</p> | ||
|
||
|
||
### About | ||
|
||
OpenAI ChatGPT is a conversational AI model that generates human-like text. This Telegram bot allows users to interact with the model directly from the Telegram app. | ||
|
||
### Screenshots | ||
|
||
<p align="center"> | ||
<img src="assets/chat-example.png" alt="Chat example screenshot"> | ||
</p> | ||
|
||
### Quickstart | ||
|
||
``` | ||
git clone [email protected]:TheoBrigitte/chatgpt-telegram-bot.git | ||
cd chatgpt-telegram-bot | ||
cp config.env.example config.env | ||
$EDITOR config.env # fill in the required environment variables | ||
source config.env | ||
go build -v | ||
./chatgpt-telegram-bot run | ||
``` | ||
|
||
### Features | ||
|
||
- Send a message to the bot to get a response from the ChatGPT Model | ||
- Set system role with `/setrole` command | ||
- Clear chat session (history and role) with `/clear` command | ||
- Clear chat history with `/clearhistory` command | ||
- Clear system role with `/clearrole` command | ||
|
||
### Configuration | ||
|
||
Configuration is done through environment variables. | ||
|
||
Copy the [config.env.example](config.env.example) file to `config.env` and fill in the required environment variables. | ||
|
||
The following variables are available: | ||
|
||
- `OPENAI_API_KEY`: Your OpenAI API key, you can get it from [here](https://platform.openai.com/account/api-keys) | ||
- `TELEGRAM_BOT_TOKEN`: Your Telegram bot's token, you can get it using [this tutorial](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) | ||
|
||
### Credits | ||
|
||
- [sashabaranov/go-openai](https://github.com/sashabaranov/go-openai) | ||
- [tucnak/telebot](https://github.com/tucnak/telebot) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().StringP("log-level", "l", log.InfoLevel.String(), "log level") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// logLevel set the level of the logger. | ||
func logLevel(cmd *cobra.Command, args []string) error { | ||
level, err := log.ParseLevel(cmd.Flag("log-level").Value.String()) | ||
if err != nil { | ||
return err | ||
} | ||
log.SetLevel(level) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/TheoBrigitte/chatgpt-telegram-bot/cmd/run" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "chatgpt-telegram-bot", | ||
Short: "ChatGPT Telegram bot", | ||
Long: `Telegram chat bot powered by OpenAI's GPT-3.`, | ||
PersistentPreRunE: logLevel, | ||
SilenceUsage: true, | ||
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true}, | ||
} | ||
|
||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(run.Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package run | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
tele "gopkg.in/telebot.v3" | ||
|
||
"github.com/TheoBrigitte/chatgpt-telegram-bot/pkg/openai" | ||
) | ||
|
||
var ( | ||
Cmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run Telegram bot", | ||
RunE: runner, | ||
} | ||
) | ||
|
||
func runner(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
|
||
// Initialize OpenAI client | ||
openaiApiKey := os.Getenv("OPENAI_API_KEY") | ||
openaiClient := openai.New(openaiApiKey) | ||
|
||
// Initialize Telegram bot | ||
telegramBotToken := os.Getenv("TELEGRAM_BOT_TOKEN") | ||
pref := tele.Settings{ | ||
Token: telegramBotToken, | ||
Poller: &tele.LongPoller{Timeout: 10 * time.Second}, | ||
} | ||
|
||
b, err := tele.NewBot(pref) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Handler for chat conversations | ||
b.Handle(tele.OnText, func(c tele.Context) error { | ||
sender := c.Sender() | ||
text := c.Text() | ||
|
||
response, err := openaiClient.ChatCompletion(ctx, sender.ID, text) | ||
if err != nil { | ||
return fmt.Errorf("Response error: %w", err) | ||
} | ||
|
||
return c.Reply(response) | ||
}) | ||
|
||
// Handler to display chat history | ||
b.Handle("/history", func(c tele.Context) error { | ||
sender := c.Sender() | ||
|
||
history := openaiClient.GetHistory(sender.ID) | ||
|
||
var output string | ||
for _, h := range history { | ||
output += fmt.Sprintf("%s: %s\n", h.Role, h.Content) | ||
} | ||
|
||
return c.Reply(fmt.Sprintf("History\n%s", output)) | ||
}) | ||
|
||
// Handler to display chat role | ||
b.Handle("/role", func(c tele.Context) error { | ||
sender := c.Sender() | ||
|
||
role := openaiClient.GetRole(sender.ID) | ||
|
||
return c.Reply(fmt.Sprintf("Role is: %s", role)) | ||
}) | ||
|
||
// Handler to set chat role | ||
b.Handle("/setrole", func(c tele.Context) error { | ||
sender := c.Sender() | ||
role := c.Message().Payload | ||
|
||
if role == "" { | ||
return c.Reply("Usage: /setrole <role>") | ||
} | ||
|
||
openaiClient.SetRole(sender.ID, role) | ||
|
||
return c.Reply(fmt.Sprintf("Role set to: %s", role)) | ||
}) | ||
|
||
// Handler to clear chat session | ||
b.Handle("/clear", func(c tele.Context) error { | ||
sender := c.Sender() | ||
|
||
openaiClient.ClearHistory(sender.ID) | ||
openaiClient.ClearRole(sender.ID) | ||
|
||
return c.Reply("Session cleared") | ||
}) | ||
|
||
// Handler to clear chat history | ||
b.Handle("/clearhistory", func(c tele.Context) error { | ||
sender := c.Sender() | ||
|
||
openaiClient.ClearHistory(sender.ID) | ||
|
||
return c.Reply("History cleared") | ||
}) | ||
|
||
// Handler to clear chat role | ||
b.Handle("/clearrole", func(c tele.Context) error { | ||
sender := c.Sender() | ||
|
||
openaiClient.ClearRole(sender.ID) | ||
|
||
return c.Reply("Role cleared") | ||
}) | ||
|
||
// Handler to display help | ||
b.Handle("/help", func(c tele.Context) error { | ||
return c.Send("Available commands:\n" + | ||
"/history\n" + | ||
"/role\n" + | ||
"/setrole\n" + | ||
"/clear\n" + | ||
"/clearhistory\n" + | ||
"/clearrole\n" + | ||
"/help", | ||
) | ||
}) | ||
|
||
// Start the bot | ||
fmt.Println("Telegram bot is running") | ||
b.Start() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export OPENAI_API_KEY="sk-proj-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
export TELEGRAM_BOT_TOKEN="xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module github.com/TheoBrigitte/chatgpt-telegram-bot | ||
|
||
go 1.22.3 | ||
|
||
require ( | ||
github.com/sashabaranov/go-openai v1.28.2 | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/spf13/cobra v1.8.1 | ||
gopkg.in/telebot.v3 v3.3.8 | ||
) | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect | ||
) |
Oops, something went wrong.