-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.py
296 lines (273 loc) · 14 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
#!/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
Version_Code = 'v1.1.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()) # 加载配置文件
LANG = json.loads(open(PATH + 'lang/' + CONFIG['Lang'] + '.json'
).read()) # 加载语言文件
MESSAGE_LOCK = False
message_list = json.loads(open(PATH + 'data.json', 'r').read()) # 加载消息数据
PREFERENCE_LOCK = False
preference_list = json.loads(open(PATH + 'preference.json', 'r').read()) # 加载用户资料与设置
def save_data(): # 保存消息数据
global MESSAGE_LOCK
while MESSAGE_LOCK:
time.sleep(0.05)
MESSAGE_LOCK = True
f = open(PATH + 'data.json', 'w')
f.write(json.dumps(message_list))
f.close()
MESSAGE_LOCK = False
def save_preference(): # 保存用户资料与设置
global PREFERENCE_LOCK
while PREFERENCE_LOCK:
time.sleep(0.05)
PREFERENCE_LOCK = True
f = open(PATH + 'preference.json', 'w')
f.write(json.dumps(preference_list))
f.close()
PREFERENCE_LOCK = False
def save_config(): # 保存配置
f = open(PATH + 'config.json', 'w')
f.write(json.dumps(CONFIG, indent=4))
f.close()
def init_user(user): # 初始化用户
global preference_list
if not str(user.id) in preference_list: # 如果用户是第一次使用Bot
preference_list[str(user.id)] = {}
preference_list[str(user.id)]['notification'] = False # 默认关闭消息发送提示
preference_list[str(user.id)]['blocked'] = False # 默认用户未被封禁
preference_list[str(user.id)]['name'] = user.full_name # 保存用户昵称
threading.Thread(target=save_preference).start()
return
if not 'blocked' in preference_list[str(user.id)]: # 兼容1.0.x版本
preference_list[str(user.id)]['blocked'] = False
if preference_list[str(user.id)]['name'] != user.full_name: # 如果用户的昵称变了
preference_list[str(user.id)]['name'] = user.full_name
threading.Thread(target=save_preference).start()
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): # 处理消息
global message_list
init_user(update.message.from_user)
if CONFIG['Admin'] == 0: # 如果未设置管理员
bot.send_message(chat_id=update.message.from_user.id,
text=LANG['please_setup_first'])
return
if update.message.from_user.id == CONFIG['Admin']: # 如果是管理员发送的消息
if update.message.reply_to_message: # 如果未回复消息
if str(update.message.reply_to_message.message_id) in message_list: # 如果消息数据存在
msg = update.message
sender_id = message_list[str(update.message.reply_to_message.message_id)]['sender_id']
# 匿名转发
try:
if msg.audio:
bot.send_audio(chat_id=sender_id,
audio=msg.audio, caption=msg.caption)
elif msg.document:
bot.send_document(chat_id=sender_id,
document=msg.document,
caption=msg.caption)
elif msg.voice:
bot.send_voice(chat_id=sender_id,
voice=msg.voice, caption=msg.caption)
elif msg.video:
bot.send_video(chat_id=sender_id,
video=msg.video, caption=msg.caption)
elif msg.sticker:
bot.send_sticker(chat_id=sender_id,
sticker=update.message.sticker)
elif msg.photo:
bot.send_photo(chat_id=sender_id,
photo=msg.photo[0], caption=msg.caption)
elif msg.text_markdown:
bot.send_message(chat_id=sender_id,
text=msg.text_markdown,
parse_mode=telegram.ParseMode.MARKDOWN)
else:
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['reply_type_not_supported'])
return
except Exception as e:
if e.message \
== 'Forbidden: bot was blocked by the user':
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['blocked_alert']) # Bot被停用
else:
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['reply_message_failed'])
return
if preference_list[str(update.message.from_user.id)]['notification']: # 如果启用消息发送提示
bot.send_message(chat_id=update.message.chat_id,
text=LANG['reply_message_sent']
% (preference_list[str(sender_id)]['name'],
str(sender_id)),
parse_mode=telegram.ParseMode.MARKDOWN)
else:
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['reply_to_message_no_data'])
else:
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['reply_to_no_message'])
else: # 如果不是管理员发送的消息
if preference_list[str(update.message.from_user.id)]['blocked']:
bot.send_message(chat_id=update.message.from_user.id,text=LANG['be_blocked_alert'])
return
fwd_msg = bot.forward_message(chat_id=CONFIG['Admin'],
from_chat_id=update.message.chat_id,
message_id=update.message.message_id) # 转发消息
if fwd_msg.sticker: # 如果是贴纸,则发送发送者身份提示
bot.send_message(chat_id=CONFIG['Admin'],
text=LANG['info_data']
% (update.message.from_user.full_name,
str(update.message.from_user.id)),
parse_mode=telegram.ParseMode.MARKDOWN,
reply_to_message_id=fwd_msg.message_id)
if preference_list[str(update.message.from_user.id)]['notification']: # 如果启用消息发送提示
bot.send_message(chat_id=update.message.from_user.id,text=LANG['message_received_notification'])
message_list[str(fwd_msg.message_id)] = {}
message_list[str(fwd_msg.message_id)]['sender_id'] = update.message.from_user.id
threading.Thread(target=save_data).start() # 保存消息数据
pass
def process_command(bot, update): # 处理指令
init_user(update.message.from_user)
id = update.message.from_user.id
global CONFIG
global preference_list
command = update.message.text[1:].replace(CONFIG['Username'], ''
).lower().split()
if command[0] == 'start':
bot.send_message(chat_id=update.message.chat_id,
text=LANG['start'])
return
elif command[0] == 'version':
bot.send_message(chat_id=update.message.chat_id,
text='Telegram Private Message Chat Bot\n'
+ Version_Code
+ '\nhttps://github.com/Netrvin/telegram-pm-chat-bot'
)
return
elif command[0] == 'setadmin': # 设置管理员
if CONFIG['Admin'] == 0: # 判断管理员是否未设置
CONFIG['Admin'] = int(update.message.from_user.id)
save_config()
bot.send_message(chat_id=update.message.chat_id,
text=LANG['set_admin_successful'])
else:
bot.send_message(chat_id=update.message.chat_id,
text=LANG['set_admin_failed'])
return
elif command[0] == 'togglenotification': # 切换消息发送提示开启状态
preference_list[str(id)]['notification'] = \
preference_list[str(id)]['notification'] == False
threading.Thread(target=save_preference).start()
if preference_list[str(id)]['notification']:
bot.send_message(chat_id=update.message.chat_id,
text=LANG['togglenotification_on'])
else:
bot.send_message(chat_id=update.message.chat_id,
text=LANG['togglenotification_off'])
elif command[0] == 'info': # 发送者信息
if update.message.from_user.id == CONFIG['Admin'] \
and update.message.chat_id == CONFIG['Admin']:
if update.message.reply_to_message:
if str(update.message.reply_to_message.message_id) in message_list:
sender_id = message_list[str(update.message.reply_to_message.message_id)]['sender_id']
bot.send_message(chat_id=update.message.chat_id,
text=LANG['info_data']
% (preference_list[str(sender_id)]['name'],
str(sender_id)),
parse_mode=telegram.ParseMode.MARKDOWN,
reply_to_message_id=update.message.reply_to_message.message_id)
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_to_message_no_data'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_to_no_message'])
else:
bot.send_message(chat_id=update.message.chat_id, text=LANG['not_an_admin'])
elif command[0] == 'ping': # Ping~Pong!
bot.send_message(chat_id=update.message.chat_id, text='Pong!')
elif command[0] == 'ban': # 封禁用户
if update.message.from_user.id == CONFIG['Admin'] \
and update.message.chat_id == CONFIG['Admin']:
if update.message.reply_to_message:
if str(update.message.reply_to_message.message_id) in message_list:
sender_id = message_list[str(update.message.reply_to_message.message_id)]['sender_id']
preference_list[str(sender_id)]['blocked'] = True
bot.send_message(chat_id=update.message.chat_id,
text=LANG['ban_user']
% (preference_list[str(sender_id)]['name'],
str(sender_id)),
parse_mode=telegram.ParseMode.MARKDOWN)
bot.send_message(chat_id=sender_id,text=LANG['be_blocked_alert'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_to_message_no_data'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_to_no_message'])
else:
bot.send_message(chat_id=update.message.chat_id, text=LANG['not_an_admin'])
elif command[0] == 'unban': # 解禁用户
if update.message.from_user.id == CONFIG['Admin'] \
and update.message.chat_id == CONFIG['Admin']:
if update.message.reply_to_message:
if str(update.message.reply_to_message.message_id) in message_list:
sender_id = message_list[str(update.message.reply_to_message.message_id)]['sender_id']
preference_list[str(sender_id)]['blocked'] = False
bot.send_message(chat_id=update.message.chat_id,
text=LANG['unban_user']
% (preference_list[str(sender_id)]['name'],
str(sender_id)),
parse_mode=telegram.ParseMode.MARKDOWN)
bot.send_message(chat_id=sender_id,text=LANG['be_unbanned'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_to_message_no_data'])
elif len(command) == 2:
if command[1] in preference_list:
preference_list[command[1]]['blocked'] = False
bot.send_message(chat_id=update.message.chat_id,
text=LANG['unban_user']
% (preference_list[command[1]]['name'],
command[1]),
parse_mode=telegram.ParseMode.MARKDOWN)
bot.send_message(chat_id=int(command[1]),text=LANG['be_unbanned'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['user_not_found'])
else:
bot.send_message(chat_id=update.message.chat_id,text=LANG['reply_or_enter_id'])
else:
bot.send_message(chat_id=update.message.chat_id, text=LANG['not_an_admin'])
else: # 指令不存在
bot.send_message(chat_id=update.message.chat_id, text=LANG['nonexistent_command'])
# 添加Handle
dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.all
& telegram.ext.Filters.private
& ~telegram.ext.Filters.command
& ~telegram.ext.Filters.status_update,
process_msg)) # 处理消息
dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command
& telegram.ext.Filters.private, process_command)) # 处理指令
updater.start_polling() # 开始轮询
print('Started')
updater.idle()
print('Stopping...')
save_data() # 保存消息数据
save_preference() # 保存用户资料与设置
print('Data saved.')
print('Stopped.')