-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.py
368 lines (308 loc) · 10.4 KB
/
main.py
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import sys
import string
import random
import time
import subprocess
import traceback
from time import sleep
if not os.path.exists("config.py"):
print("Error: 配置文件不存在,请检查是否正确配置!")
exit(0)
import config
if not os.path.exists("data.py"):
print("Error: 数据文件不存在,请检查是否正确配置!")
exit(0)
from data import *
try: # 向下兼容
if config.USE_SENTRY: # Bug Tracker
import sentry_sdk
sentry_sdk.init(config.SENTRY_LINK)
except Exception as e:
ret = str(e)
xx = ""
def getReportID():
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
sa = []
for i in range(8):
sa.append(random.choice(seed))
salt = "".join(sa)
return salt
totalcount = 0
chatcount = 0
inlinecount = 0
def getanother():
xx = " "
xx += "\r\n"
xx += " "
return xx
samepoint = 2
def shuffle(List):
global samepoint
pool = list(List) * samepoint
while True:
random.shuffle(pool)
for element in pool:
yield element
nextsays = shuffle(text)
nextquote = shuffle(quotes)
def getquotes():
global nextquote
xx = next(nextquote)
xx = xx.replace("a", random.choice(frontsays))
xx = xx.replace("b", random.choice(backsays))
return xx
def Process(msg, maxlength):
global nextsays
xx = msg
tmp = str()
while len(tmp) < maxlength:
branch = random.randint(0, 100)
if branch < 5:
tmp += getanother()
while tmp[-1] == ",":
tmp += next(nextsays)
while tmp[-1] == ":":
tmp += next(nextsays)
elif branch < 20:
tmp += getquotes()
else:
tmp += next(nextsays)
while tmp[-1] == ",":
tmp += next(nextsays)
while tmp[-1] == ":":
tmp += next(nextsays)
tmp = " " + tmp
tmp = tmp.replace("x", xx)
# print(tmp)
return tmp
def xiaobMode(qtext, fr):
tmp = (
xiaobtext_front.replace("%ss", qtext) + fr + xiaobtext_end.replace("%ss", qtext)
)
return tmp
import logging
import telebot
bot = telebot.TeleBot(config.TOKEN)
from telebot import apihelper
from telebot import types
from telebot import util
if config.USE_PROXY:
apihelper.proxy = config.HTTP_PROXY
# telebot.logger.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.INFO)
try:
ghash = str(
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode("utf-8")
)
gtime = str(
subprocess.check_output(["git", "log", '--pretty=format:"%cd"', "-1"]).decode(
"utf-8"
)
)
except Exception as e:
rid = getReportID()
traceback.print_exc()
logging.error(str(rid + "::" + str(repr(e)) + "\n" + traceback.format_exc()))
def get_githash():
global ghash
return ghash
def get_gittime():
global gtime
return gtime
def getstat(mode):
global totalcount
global inlinecount
global chatcount
ret = '欢迎使用"狗屁不通生成器"的 Telegram 移植版本!\n'
ret += "源码:https://github.com/abc1763613206/TGBullshitGeneratorBot\n"
ret += "当前 commit 版本 {}最后更新于 {}\n".format(get_githash(), get_gittime())
ret += data_stat()
ret += (
"\n自上一次重启以来,总调用{}次,其中 inline 模式{}次,私聊模式{}次\n".format(
str(totalcount), str(inlinecount), str(chatcount)
)
)
if mode == 0:
ret += "您正在使用 inline 模式,如有疑问请 @abc1763613206"
else:
ret += "您正在使用 私聊 模式,如有疑问请 @abc1763613206"
return ret
@bot.message_handler(commands=["start", "help"])
def send_welcome(message):
bot.reply_to(
message,
"狗屁不通生成器(https://github.com/menzi11/BullshitGenerator) 的 Telegram 移植版本,如有疑问请 @abc1763613206\n源码: https://github.com/abc1763613206/TGBullshitGeneratorBot",
)
@bot.message_handler(commands=["stat"])
def send_stat(message):
try:
global totalcount
totalcount += 1
global chatcount
chatcount += 1
bot.reply_to(message, getstat(1))
except Exception as e:
traceback.print_exc()
logging.error(e)
@bot.message_handler(func=lambda m: True)
def echo_all(message):
try:
global totalcount
totalcount += 1
global chatcount
chatcount += 1
if message.content_type != "text":
bot.reply_to(message, "抱歉,我还只支持文字!")
logging.info(
str(
"Chat::@{} ({} {}): {}".format(
message.from_user.username,
message.from_user.first_name,
message.from_user.last_name,
message.text,
)
)
)
# print(message.type)
# print(type(message.from_user))
# if len(message.text) > 15:
# bot.reply_to(message,'你说的内容太长了!何不切分一下试试?')
# return
ret = Process(message.text, 1000)
if len(ret) >= 4500:
bot.reply_to(message, "您要说的内容实在太长了,将为您分段处理!")
splitted_ret = util.split_string(ret, 3000)
logging.info(
str("Too long.Split into " + str(len(splitted_ret)) + " parts")
)
for sret in splitted_ret:
bot.reply_to(message, sret)
# sleep(0.3)
else:
bot.reply_to(message, ret)
except Exception as e:
rid = getReportID()
traceback.print_exc()
logging.error(str(rid + "::" + str(repr(e)) + "\n" + traceback.format_exc()))
bot.send_message(
message.from_user.id,
"抱歉,我出现错误,识别ID为 "
+ rid
+ "\n\n信息如下:\n"
+ str(repr(e))
+ "\n\n请将这条信息 Forward 给 @abc1763613206 中所列的用户 进行处理!",
)
# @bot.inline_handler(lambda query: SetQText(query.query) )
@bot.inline_handler(lambda query: query.query != "")
def query_text(inline_query):
global totalcount
totalcount += 1
global inlinecount
inlinecount += 1
qtext = inline_query.query
if qtext == "":
pass
logging.info(
str("Inline:: @{} ({} {}): {}").format(
inline_query.from_user.username,
inline_query.from_user.first_name,
inline_query.from_user.last_name,
inline_query.query,
)
)
try:
if len(qtext) > 15:
r5 = types.InlineQueryResultArticle(
"1",
"你要说的内容太长了!将直接发送原消息(小提示:私聊模式可以发送长消息哦!)",
types.InputTextMessageContent(str(qtext)),
)
r6 = types.InlineQueryResultArticle(
"2", "查看当前统计(Beta)", types.InputTextMessageContent(getstat(0))
)
bot.answer_inline_query(inline_query.id, [r5, r6], cache_time=1)
else:
r1 = types.InlineQueryResultArticle(
"1", "小作文(200字)", types.InputTextMessageContent(Process(qtext, 200))
)
r2 = types.InlineQueryResultArticle(
"2",
"普通玩法(600字)",
types.InputTextMessageContent(Process(qtext, 600)),
)
r3 = types.InlineQueryResultArticle(
"3",
"加强玩法(1000字)",
types.InputTextMessageContent(Process(qtext, 1000)),
)
r4 = types.InlineQueryResultArticle(
"4",
"再多来些(1500字)",
types.InputTextMessageContent(Process(qtext, 1500)),
)
r5 = types.InlineQueryResultArticle(
"5",
"与小编联动(@xiaobbot)(400字)",
types.InputTextMessageContent(xiaobMode(qtext, Process(qtext, 200))),
)
r6 = types.InlineQueryResultArticle(
"6",
"小编(@xiaobbot)想多说点(800字)",
types.InputTextMessageContent(xiaobMode(qtext, Process(qtext, 600))),
)
r7 = types.InlineQueryResultArticle(
"7", "查看当前统计(Beta)", types.InputTextMessageContent(getstat(0))
)
bot.answer_inline_query(
inline_query.id, [r1, r2, r3, r4, r5, r6, r7], cache_time=1
)
except Exception as e:
rid = getReportID()
traceback.print_exc()
logging.error(str(rid + "::" + str(repr(e)) + "\n" + traceback.format_exc()))
@bot.inline_handler(lambda query: query.query == "")
def query_text(inline_query):
try:
r1 = types.InlineQueryResultArticle(
"1",
"查看当前统计(Beta)(不会即时更新)",
types.InputTextMessageContent(getstat(0)),
)
r2 = types.InlineQueryResultArticle(
"2",
"您正在使用 inline 模式,请输入内容以便生成",
types.InputTextMessageContent("?"),
)
bot.answer_inline_query(inline_query.id, [r1, r2], cache_time=360)
except Exception as e:
rid = getReportID()
traceback.print_exc()
logging.error(str(rid + "::" + str(repr(e)) + "\n" + traceback.format_exc()))
# @bot.inline_handler(lambda query: True)
# def query_text(inline_query):
# try:
# r = types.InlineQueryResultArticle('1', 'Result', types.InputTextMessageContent('Result message.'))
# r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('Result message2.'))
# bot.answer_inline_query(inline_query.id, [r, r2])
# except Exception as e:
# print(e)
def main_loop():
bot.polling(True)
while 1:
time.sleep(3)
if __name__ == "__main__":
try:
logging.info(data_stat())
logging.info(
"当前 commit 版本 {}最后更新于 {}\n".format(get_githash(), get_gittime())
) # git supported
logging.info("准备处理信息")
main_loop()
except KeyboardInterrupt:
print("\nExiting by user request.\n")
exit(0)
except Exception as e:
rid = getReportID()
traceback.print_exc()
logging.error(str(rid + "::" + str(repr(e)) + "\n" + traceback.format_exc()))