-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
203 lines (177 loc) · 5.17 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"fmt"
"github.com/go-redis/redis_rate/v10"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/redis/go-redis/v9"
"github.com/sashabaranov/go-openai"
"log"
"regexp"
"strings"
"time"
)
type App struct {
RedisClient *redis.Client
OpenaiClient *openai.Client
TelegramBot *tgbotapi.BotAPI
RedisLimiter *redis_rate.Limiter
}
type Check struct {
Stupid bool
Smart bool
}
func (app *App) run() {
// Set up updates channel
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := app.TelegramBot.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
// Listen for messages in group chats
for update := range updates {
if update.Message == nil {
continue
}
message := update.Message.Text
if message == "" {
continue
}
check := generateCheck(message)
if !(check.Stupid || check.Smart) {
continue
}
replyMessage := app.generateReplyMessage(message, check, update.Message.From.ID)
if replyMessage == "" {
continue
}
reply := tgbotapi.NewMessage(update.Message.Chat.ID, replyMessage)
reply.ReplyToMessageID = update.Message.MessageID
_, err = app.TelegramBot.Send(reply)
if err != nil {
log.Println(err)
}
}
}
func (app *App) generateReplyMessage(message string, check Check, userID int) string {
if len(message) > AiMessageMaxLength {
return generateReply(check)
}
ctx := context.Background()
// TODO: Replace `redis_rate` with something better
// If global limit is reached for today or user limit is reached, generate answer manually
globalLimit, err := app.RedisLimiter.Allow(
ctx,
"global:ai_total_rate_limit",
perDay(AiTotalRateLimitPerDay),
)
if err != nil {
log.Fatal(err)
}
userDailyLimit, err := app.RedisLimiter.Allow(
ctx,
fmt.Sprintf("user:%d:ai_daily_limit", userID),
perDay(AiUserRateLimitPerDay),
)
if err != nil {
log.Fatal(err)
}
if globalLimit.Allowed == 0 || userDailyLimit.Allowed == 0 {
return generateReply(check)
}
// If users minute limit is reached - escort him to the nearest hyi :)
userMinuteLimit, err := app.RedisLimiter.Allow(
ctx,
fmt.Sprintf("user:%d:ai_minute_limit", userID),
redis_rate.PerMinute(AiUserRateLimitPerMinute),
)
if err != nil {
log.Fatal(err)
}
if userMinuteLimit.Allowed == 0 {
return "Error 429: Пошёл на хуй, пёс!"
}
reply, err := generateOpenAiReply(app.OpenaiClient, message)
if err != nil {
log.Println(err)
// If OpenAI fails, generate answer manually
return generateReply(check)
}
return reply
}
func generateCheck(message string) Check {
m := strings.ToLower(message)
return Check{
Stupid: containsStupidQuestion(m),
Smart: containsSmartQuestion(m),
}
}
func generateReply(check Check) string {
switch check {
case Check{Stupid: true, Smart: false}:
return "На рынке Хопа!"
case Check{Stupid: false, Smart: true}:
return "Держи ссылку с адресом рынка Хопа, раз в гугле забанили:\nhttps://goo.gl/maps/aqN4rzapdDXvRJNW9"
case Check{Stupid: true, Smart: true}:
return "Хопа на рынке Хопа! Вот, ну:\nhttps://goo.gl/maps/aqN4rzapdDXvRJNW9"
default:
return ""
}
}
func containsStupidQuestion(message string) bool {
var re = regexp.MustCompile(
`(\s|^)(?:где|в)\s.*(?:купи(ть|л|ли|ла)|на(йти|шла|ш[её]л)|прода[её]тся|починить|посмотреть|продаже|доста(ть|л|ли|ла)|взя(ть|л|ли|ла)|покупа(л|ли|ла)).*\?`,
)
return re.MatchString(message)
}
func containsSmartQuestion(message string) bool {
var re = regexp.MustCompile(
`(\s|^)(?:где|как)\s.*(?:хоп[ау]|хоп[ауы]).*\?`,
)
return re.MatchString(message)
}
func generateOpenAiReply(client *openai.Client, message string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: openai.GPT4oMini,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You help people find what they are looking for in the telegram chat. " +
"You will suggest the рынок Хопа in Batumi for any request, answering in Russian. " +
"Only if the user asks about the address, answer with the link https://maps.app.goo.gl/MobUYTDFBKhuvMaR7. " +
"Answer sarcastically, with jokes, puns, prejudices about clothing markets. " +
"If someone asks where to find something on Хопа - you can randomly generate the market row number and the store. " +
"Answer 1-2 sentences.",
},
{
Role: openai.ChatMessageRoleUser,
Content: message,
},
},
Temperature: 1.05,
MaxTokens: 200,
TopP: 1,
Stop: []string{"\n"},
FrequencyPenalty: 0.4,
PresencePenalty: 0.4,
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return "", err
}
return resp.Choices[0].Message.Content, nil
}
// perDay is a missing function in redis_rate package
func perDay(rate int) redis_rate.Limit {
return redis_rate.Limit{
Rate: rate,
Period: 24 * time.Hour,
Burst: rate,
}
}