-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgb.go
328 lines (285 loc) · 8.55 KB
/
pgb.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// PGB: Pythia Gata Bot
// Copyright (C) 2019-2024 Yishen Miao
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/sethvargo/go-envconfig"
)
// Config represents the configuration settings for the application.
// It includes the following fields:
// - Host: The hostname or IP address where the application will run. It is
// set via the "HOST" environment variable and defaults to "0.0.0.0".
// - Port: The port number on which the application will listen. It is set via
// the "PORT" environment variable and defaults to "8080".
// - Token: A required authentication token for the application. It is set via
// the "TOKEN" environment variable.
type Config struct {
Host string `env:"HOST, default=0.0.0.0"`
Port string `env:"PORT, default=8080"`
Token string `env:"TOKEN, required"`
}
// UpdateContext holds the context for an update operation.
// It includes a random number generator, a query string, and a locale.
//
// Fields:
// - Rand: A pointer to a rand.Rand instance used for generating random numbers.
// - Query: A pointer to a string representing the query to be executed.
// - Locale: A string representing the locale of the user.
type UpdateContext struct {
Rand *rand.Rand
Query *string
Locale *string
}
// builder is a custom type that embeds strings.Builder to provide additional
// functionality or methods specific to the application. It inherits all the
// methods of strings.Builder and can be used in the same way.
type builder struct {
strings.Builder
}
// WriteStrings writes multiple strings to the builder.
// It takes a variadic parameter of strings and writes each one sequentially
// using the WriteString method.
//
// Parameters:
//
// strs - a variadic list of strings to be written to the builder.
func (b *builder) WriteStrings(strs ...string) {
for _, s := range strs {
b.WriteString(s)
}
}
// newRand creates a new instance of rand.Rand seeded with the XOR combination
// of the provided seeds. It takes a slice of uint64 seeds, combines them using
// the XOR operation, and returns a pointer to a new rand.Rand object seeded
// with the resulting value.
//
// Parameters:
// - seeds: A slice of uint64 values used to seed the random number generator.
//
// Returns:
// - A pointer to a new rand.Rand object seeded with the combined seed value.
func newRand(seeds []uint64) *rand.Rand {
var s uint64
for _, v := range seeds {
s ^= v
}
return rand.New(rand.NewSource(int64(s)))
}
// pia generates a string based on the provided UpdateContext.
// It randomly selects one of two possible pia (slap) actions performed by
// either a dog or a cat and appends the query from the context.
// There is a 1 in 8 chance to summon a dog and a 7 in 8 chance to summon a cat.
//
// Parameters:
// - ctx: A pointer to an UpdateContext containing the query and random number
// generator.
//
// Returns:
// - A string that includes a randomly selected pia prefix and the query from
// the context.
func pia(ctx *UpdateContext) string {
var b builder
switch ctx.Rand.Uint64() % 8 {
case 0:
b.WriteString("Pia!▼(o ‵-′)ノ★ ")
default:
b.WriteString("Pia!<(=o ‵-′)ノ☆ ")
}
b.WriteString(*ctx.Query)
return b.String()
}
// divine generates a divination result based on the provided UpdateContext. It
// constructs a string that includes the query and the result of the divination.
// The result is determined by generating random numbers and mapping them to
// specific outcomes. The outcomes are categorized as "吉" (good) or "凶" (bad)
// with varying degrees of intensity.
//
// Parameters:
// - ctx: A pointer to an UpdateContext which contains the query and a random
// number generator.
//
// Returns:
// - A string representing the divination result.
func divine(ctx *UpdateContext) string {
var omen, mult string
var b builder
b.WriteStrings("所求事项: ", *ctx.Query, "\n结果: ")
o := ctx.Rand.Uint64() % 16
switch {
case 9 <= o:
omen = "吉"
case o < 7:
omen = "凶"
}
if omen == "" {
b.WriteString("尚可")
} else {
m := ctx.Rand.Uint64() % 1024
switch {
case m < 1:
mult = "极小"
case 1 <= m && m < 11:
mult = "超小"
case 11 <= m && m < 56:
mult = "特小"
case 56 <= m && m < 176:
mult = "甚小"
case 176 <= m && m < 386:
mult = "小"
case 386 <= m && m < 638:
mult = ""
case 638 <= m && m < 848:
mult = "大"
case 848 <= m && m < 968:
mult = "甚大"
case 968 <= m && m < 1013:
mult = "特大"
case 1013 <= m && m < 1023:
mult = "超大"
case 1023 <= m:
mult = "极大"
}
b.WriteStrings(mult, omen)
}
return b.String()
}
func main() {
var conf Config
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := envconfig.Process(ctx, &conf); err != nil {
log.Fatal(err)
}
opts := []bot.Option{
bot.WithDefaultHandler(handler),
}
if b, err := bot.New(conf.Token, opts...); nil != err {
panic(err)
} else {
go b.StartWebhook(ctx)
http.ListenAndServe(
net.JoinHostPort(conf.Host, conf.Port),
b.WebhookHandler(),
)
}
}
// handler processes an incoming inline query from a bot and generates a
// response. It uses the query details and current time to create a unique
// context for the query, then generates a set of inline query results based on
// this context and sends them back.
//
// Parameters:
// - ctx: The context for the request, used for cancellation and deadlines.
// - b: The bot instance handling the request.
// - update: The update containing the inline query to be processed.
//
// The function performs the following steps:
// 1. Checks if the update contains an inline query. If not, it returns
// immediately.
// 2. Creates a SHA-256 hash based on the user's ID, current time truncated to
// 30 minutes, and the query text.
// 3. Uses the hash to seed a random number generator.
// 4. Creates an UpdateContext with the random number generator and query text.
// 5. Generates a set of inline query results using the UpdateContext.
// 6. Sends the generated results back to the bot as a response to the inline
// query.
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.InlineQuery == nil {
return
}
locale := "zh"
if update.InlineQuery.From != nil && update.InlineQuery.From.LanguageCode != "" {
locale = update.InlineQuery.From.LanguageCode
}
h := sha256.New()
if err := binary.Write(
h,
binary.LittleEndian,
uint64(update.InlineQuery.From.ID),
); err != nil {
log.Println(err)
}
if err := binary.Write(
h,
binary.LittleEndian,
time.Now().Truncate(30*time.Minute).Unix(),
); err != nil {
log.Println(err)
}
if err := binary.Write(
h,
binary.LittleEndian,
[]byte(update.InlineQuery.Query),
); err != nil {
log.Println(err)
}
r := bytes.NewReader(h.Sum(nil))
// sha256 checksum is 256 bits long, equals to four 64bits integer.
seeds := make([]uint64, 4)
if err := binary.Read(r, binary.BigEndian, &seeds); err != nil {
log.Println("binary.Read failed:", err)
}
rctx := UpdateContext{
Rand: newRand(seeds),
Query: &update.InlineQuery.Query,
Locale: &locale,
}
var divineTitle, piaTitle string
switch locale {
case "zh":
divineTitle = "求签"
piaTitle = "Pia"
default:
divineTitle = "Divination"
piaTitle = "Pia"
}
results := []models.InlineQueryResult{
&models.InlineQueryResultArticle{
ID: "divine",
Title: divineTitle,
InputMessageContent: &models.InputTextMessageContent{
MessageText: divine(&rctx),
},
},
&models.InlineQueryResultArticle{
ID: "pia",
Title: piaTitle,
InputMessageContent: &models.InputTextMessageContent{
MessageText: pia(&rctx),
},
},
}
b.AnswerInlineQuery(
ctx,
&bot.AnswerInlineQueryParams{
InlineQueryID: update.InlineQuery.ID,
Results: results,
},
)
}