This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.py
327 lines (283 loc) · 13.3 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import json
import telegram.ext
import telegram
import sys
import datetime
import os
import logging
import threading
import six
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf8')
Version_Code = 'v1.0.0'
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
PATH = os.path.dirname(os.path.realpath(__file__)) + '/'
CONFIG = json.loads(open(PATH + 'config.json', 'r').read())
DATA_LOCK = False
submission_list = json.loads(open(PATH + 'data.json', 'r').read())
def save_data():
global DATA_LOCK
while DATA_LOCK:
time.sleep(0.05)
DATA_LOCK = True
f = open(PATH + 'data.json', 'w')
f.write(json.dumps(submission_list, ensure_ascii=False))
f.close()
DATA_LOCK = False
def save_config():
f = open(PATH + 'config.json', 'w')
f.write(json.dumps(CONFIG, indent=4))
f.close()
updater = telegram.ext.Updater(token=CONFIG['Token'])
dispatcher = updater.dispatcher
me = updater.bot.get_me()
CONFIG['ID'] = me.id
CONFIG['Username'] = '@' + me.username
print('Starting... (ID: ' + str(CONFIG['ID']) + ', Username: ' + CONFIG['Username'] + ')')
def process_msg(bot, update):
if update.channel_post != None:
return
if update.message.chat_id == CONFIG['Group_ID'] \
and update.message.reply_to_message != None:
if update.message.reply_to_message.from_user.id == CONFIG['ID'] \
and (update.message.reply_to_message.forward_from != None
or update.message.reply_to_message.forward_from_chat
!= None):
msg = update.message.reply_to_message
global submission_list
if submission_list[str(CONFIG['Group_ID']) + ':'
+ str(msg.message_id)]['posted'] == True:
return
if submission_list[str(CONFIG['Group_ID']) + ':'
+ str(msg.message_id)]['type'] == 'real':
post = real_name_post(bot, msg,
update.message.from_user)
elif submission_list[str(CONFIG['Group_ID']) + ':'
+ str(msg.message_id)]['type'] \
== 'anonymous':
post = anonymous_post(bot, msg,
update.message.from_user)
if update.message.text != None:
bot.send_message(chat_id=CONFIG['Publish_Channel_ID'],
text=update.message.text,
reply_to_message_id=post.message_id)
return
if update.message.from_user.id == update.message.chat_id:
markup = \
telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("是"
, callback_data='submission_type:real'),
telegram.InlineKeyboardButton("否",
callback_data='submission_type:anonymous')],
[telegram.InlineKeyboardButton("取消投稿",
callback_data='cancel:submission')]])
if update.message.forward_from != None \
or update.message.forward_from_chat != None:
if update.message.forward_from_chat != None:
markup = \
telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("是"
, callback_data='submission_type:real')],
[telegram.InlineKeyboardButton("取消投稿",
callback_data='cancel:submission')]])
elif update.message.forward_from.id \
!= update.message.from_user.id:
markup = \
telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("是"
, callback_data='submission_type:real')],
[telegram.InlineKeyboardButton("取消投稿",
callback_data='cancel:submission')]])
bot.send_message(chat_id=update.message.chat_id,
text="即将完成投稿...\n您是否想要保留消息来源(保留消息发送者用户名)",
reply_to_message_id=update.message.message_id,
reply_markup=markup)
def process_command(bot, update):
if update.channel_post != None:
return
command = update.message.text[1:].replace(CONFIG['Username'], ''
).lower()
if command == 'start':
bot.send_message(chat_id=update.message.chat_id,
text="""可接收的投稿类型:
文字
图片
音频/语音
视频
文件""")
return
if command == 'version':
bot.send_message(chat_id=update.message.chat_id,
text='Telegram Submission Bot\n'
+ Version_Code
+ '\nhttps://github.com/Netrvin/telegram-submission-bot'
)
return
if update.message.from_user.id == CONFIG['Admin']:
if command == 'setgroup':
CONFIG['Group_ID'] = update.message.chat_id
save_config()
bot.send_message(chat_id=update.message.chat_id,
text="已设置本群为审稿群")
return
def anonymous_post(bot, msg, editor):
if msg.audio != None:
r = bot.send_audio(chat_id=CONFIG['Publish_Channel_ID'],
audio=msg.audio, caption=msg.caption)
elif msg.document != None:
r = bot.send_document(chat_id=CONFIG['Publish_Channel_ID'],
document=msg.document,
caption=msg.caption)
elif msg.voice != None:
r = bot.send_voice(chat_id=CONFIG['Publish_Channel_ID'],
voice=msg.voice, caption=msg.caption)
elif msg.video != None:
r = bot.send_video(chat_id=CONFIG['Publish_Channel_ID'],
video=msg.video, caption=msg.caption)
elif msg.photo:
r = bot.send_photo(chat_id=CONFIG['Publish_Channel_ID'],
photo=msg.photo[0], caption=msg.caption)
else:
r = bot.send_message(chat_id=CONFIG['Publish_Channel_ID'],
text=msg.text_markdown,
parse_mode=telegram.ParseMode.MARKDOWN)
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(msg.message_id)]['posted'] = True
bot.edit_message_text(text="新投稿\n投稿人: ["
+ submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_Name']
+ '](tg://user?id='
+ str(submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_ID'])
+ """)
来源: 保留
审稿人: [""" + editor.name
+ '](tg://user?id=' + str(editor.id)
+ ")\n已采用", chat_id=CONFIG['Group_ID'],
parse_mode=telegram.ParseMode.MARKDOWN,
message_id=submission_list[str(CONFIG['Group_ID'
]) + ':' + str(msg.message_id)]['Markup_ID'])
bot.send_message(chat_id=submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_ID'],
text="您的稿件已过审,感谢您对我们的支持",
reply_to_message_id=submission_list[str(CONFIG['Group_ID'
]) + ':' + str(msg.message_id)]['Original_MsgID'])
threading.Thread(target=save_data).start()
return r
def real_name_post(bot, msg, editor):
global submission_list
r = bot.forward_message(chat_id=CONFIG['Publish_Channel_ID'],
from_chat_id=CONFIG['Group_ID'],
message_id=msg.message_id)
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(msg.message_id)]['posted'] = True
bot.edit_message_text(text="新投稿\n投稿人: ["
+ submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_Name']
+ '](tg://user?id='
+ str(submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_ID'])
+ """)
来源: 保留
审稿人: [""" + editor.name
+ '](tg://user?id=' + str(editor.id)
+ ")\n已采用", chat_id=CONFIG['Group_ID'],
parse_mode=telegram.ParseMode.MARKDOWN,
message_id=submission_list[str(CONFIG['Group_ID'
]) + ':' + str(msg.message_id)]['Markup_ID'])
bot.send_message(chat_id=submission_list[str(CONFIG['Group_ID'])
+ ':' + str(msg.message_id)]['Sender_ID'],
text="您的稿件已过审,感谢您对我们的支持",
reply_to_message_id=submission_list[str(CONFIG['Group_ID'
]) + ':' + str(msg.message_id)]['Original_MsgID'])
threading.Thread(target=save_data).start()
return r
def process_callback(bot, update):
if update.channel_post != None:
return
global submission_list
query = update.callback_query
if query.message.chat_id == CONFIG['Group_ID'] and query.data \
== 'receive:real':
real_name_post(bot, query.message.reply_to_message,
query.from_user)
return
if query.message.chat_id == CONFIG['Group_ID'] and query.data \
== 'receive:anonymous':
anonymous_post(bot, query.message.reply_to_message,
query.from_user)
return
if query.data == 'cancel:submission':
bot.edit_message_text(text="已取消投稿",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return
msg = "新投稿\n投稿人: [" + query.message.reply_to_message.from_user.name \
+ '](tg://user?id=' \
+ str(query.message.reply_to_message.from_user.id) + ")\n来源: "
fwd_msg = bot.forward_message(chat_id=CONFIG['Group_ID'],
from_chat_id=query.message.chat_id,
message_id=query.message.reply_to_message.message_id)
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)] = {}
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['posted'] = False
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['Sender_Name'] = \
query.message.reply_to_message.from_user.name
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['Sender_ID'] = \
query.message.reply_to_message.from_user.id
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['Original_MsgID'] = \
query.message.reply_to_message.message_id
if query.data == 'submission_type:real':
msg += "保留"
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['type'] = 'real'
markup = \
telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("采用"
, callback_data='receive:real')]])
markup_msg = bot.send_message(chat_id=CONFIG['Group_ID'],
text=msg, reply_to_message_id=fwd_msg.message_id,
reply_markup=markup,
parse_mode=telegram.ParseMode.MARKDOWN)
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['Markup_ID'] = \
markup_msg.message_id
elif query.data == 'submission_type:anonymous':
msg += "匿名"
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['type'] = 'anonymous'
markup = \
telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("采用"
, callback_data='receive:anonymous')]])
markup_msg = bot.send_message(chat_id=CONFIG['Group_ID'],
text=msg, reply_to_message_id=fwd_msg.message_id,
reply_markup=markup,
parse_mode=telegram.ParseMode.MARKDOWN)
submission_list[str(CONFIG['Group_ID']) + ':'
+ str(fwd_msg.message_id)]['Markup_ID'] = \
markup_msg.message_id
bot.edit_message_text(text="感谢您的投稿", chat_id=query.message.chat_id,
message_id=query.message.message_id)
threading.Thread(target=save_data).start()
dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text
| telegram.ext.Filters.audio
| telegram.ext.Filters.photo
| telegram.ext.Filters.video
| telegram.ext.Filters.voice
| telegram.ext.Filters.document, process_msg))
dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command,
process_command))
dispatcher.add_handler(telegram.ext.CallbackQueryHandler(process_callback))
updater.start_polling()
print('Started')
updater.idle()
print('Stopping...')
save_data()
print('Data saved.')
print('Stopped.')