-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
51 lines (41 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
gogpt "github.com/sashabaranov/go-gpt3"
"github.com/yanzay/tbot/v2"
)
func getChatGPTresponse(ctx context.Context, question string) string {
c := gogpt.NewClient(os.Getenv("OPENAI_TOKEN"))
maxtokens, err0 := strconv.Atoi(os.Getenv("OPENAI_MAXTOKENS"))
if err0 != nil {
fmt.Println("Error during conversion")
return "MaxTokens Conversion Error happened!"
}
req := gogpt.CompletionRequest{
Model: "text-davinci-003",
MaxTokens: maxtokens,
Prompt: question, //it represents the text your typed in
Temperature: 0,
}
resp, err := c.CreateCompletion(ctx, req)
if err != nil {
return "You got an error!"
} else {
fmt.Println(resp.Choices[0].Text)
return resp.Choices[0].Text // it represents the answer ChatGPT gives you
}
}
func main() {
ctx := context.Background()
bot := tbot.New(os.Getenv("TELEGRAM_BOT_TOKEN"),
tbot.WithWebhook("", ":8080"))
c := bot.Client() //Please add your Render URL between "". 請在引號中加入你的Render網址
bot.HandleMessage(".*human:*", func(m *tbot.Message) { //The AI must be triggered by the keyword "human:"
c.SendMessage(m.Chat.ID, "AI:"+getChatGPTresponse(ctx, m.Text)) //m.Text represents the text you typed in 代表你打的文字
})
log.Fatal(bot.Start())
}