-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgb.go
222 lines (178 loc) · 3.63 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
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
Host string `default:"0.0.0.0"`
Port string `default:"8080"`
Token string `require:"true"`
}
type Context struct {
Rand *rand.Rand
Query *tgbotapi.InlineQuery
}
type builder struct {
strings.Builder
}
func (b *builder) WriteStrings(strs ...string) {
for _, s := range strs {
b.WriteString(s)
}
}
func newRand(seeds []uint64) *rand.Rand {
var s uint64
for _, v := range seeds {
s ^= v
}
return rand.New(rand.NewSource(int64(s)))
}
func pia(ctx *Context) string {
var b builder
switch ctx.Rand.Uint64() % 8 {
case 0:
b.WriteString("Pia!▼(o ‵-′)ノ★ ")
default:
b.WriteString("Pia!<(=o ‵-′)ノ☆ ")
}
b.WriteString(ctx.Query.Query)
return b.String()
}
func divine(ctx *Context) string {
var omen, mult string
var b builder
b.WriteStrings("所求事项: ", ctx.Query.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 answerInline(q *tgbotapi.InlineQuery) tgbotapi.InlineConfig {
h := sha256.New()
if err := binary.Write(
h,
binary.LittleEndian,
uint64(q.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(q.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 {
fmt.Println("binary.Read failed:", err)
}
ctx := Context{
Rand: newRand(seeds),
Query: q,
}
var res []interface{}
res = append(res,
tgbotapi.NewInlineQueryResultArticleMarkdown(
"divine",
"求签",
divine(&ctx),
),
tgbotapi.NewInlineQueryResultArticleMarkdown(
"pia",
"Pia",
pia(&ctx),
),
)
ans := tgbotapi.InlineConfig{
InlineQueryID: q.ID,
Results: res,
}
return ans
}
func main() {
var conf Config
envconfig.MustProcess("", &conf)
bot, err := tgbotapi.NewBotAPI(conf.Token)
if err != nil {
log.Fatal(err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
log.Fatal(err)
}
if info.LastErrorDate != 0 {
tm := time.Unix(int64(info.LastErrorDate), 0)
log.Printf("Last error: %s %s", tm, info.LastErrorMessage)
}
updates := bot.ListenForWebhook("/")
go http.ListenAndServe(net.JoinHostPort(conf.Host, conf.Port), nil)
for update := range updates {
if update.InlineQuery != nil {
ans := answerInline(update.InlineQuery)
resp, err := bot.Request(ans)
if err != nil {
log.Println("Error: ", err)
} else if !resp.Ok {
log.Println(
"Error: ",
resp.ErrorCode,
resp.Description,
)
}
}
}
}