2020-10-26 02:11:53 +08:00
|
|
|
|
import os
|
2020-10-26 16:08:50 +08:00
|
|
|
|
import re
|
|
|
|
|
from telegram.ext import Updater, MessageHandler, filters
|
|
|
|
|
|
2020-10-26 02:11:53 +08:00
|
|
|
|
Filters = filters.Filters
|
2020-10-26 16:08:50 +08:00
|
|
|
|
parser = re.compile(r'^\/(\S+)( *)(\S*)$')
|
2020-10-26 02:11:53 +08:00
|
|
|
|
|
|
|
|
|
# Docker env
|
|
|
|
|
if os.environ.get('TOKEN') and os.environ['TOKEN'] != 'X':
|
|
|
|
|
Token = os.environ['TOKEN']
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('no token')
|
|
|
|
|
|
2020-10-26 16:08:50 +08:00
|
|
|
|
|
2020-10-26 02:11:53 +08:00
|
|
|
|
def mention(user):
|
2020-10-26 02:54:06 +08:00
|
|
|
|
space = ' '
|
2020-10-26 02:11:53 +08:00
|
|
|
|
if 'last_name' not in user:
|
|
|
|
|
user['last_name'] = ''
|
2020-10-26 02:54:06 +08:00
|
|
|
|
space = ''
|
|
|
|
|
return f"[{user['first_name']}{space}{user['last_name']}](tg://user?id={user['id']})"
|
2020-10-26 02:11:53 +08:00
|
|
|
|
|
|
|
|
|
|
2020-10-26 16:08:50 +08:00
|
|
|
|
def get_text(mention_from, mention_rpl, command):
|
|
|
|
|
parsed = parser.search(command).groups()
|
|
|
|
|
if parsed[2]:
|
|
|
|
|
return f"{mention_from} {parsed[0]} {mention_rpl} {parsed[2]}!"
|
|
|
|
|
else:
|
|
|
|
|
return f"{mention_from} {parsed[0]} 了 {mention_rpl}!"
|
|
|
|
|
|
|
|
|
|
|
2020-10-26 02:11:53 +08:00
|
|
|
|
def reply(update, context):
|
|
|
|
|
print(repr(update.to_dict()))
|
|
|
|
|
msg = update.to_dict()['message']
|
2020-10-26 16:08:50 +08:00
|
|
|
|
command = msg['text']
|
2020-10-26 02:11:53 +08:00
|
|
|
|
msg_from = msg['from']
|
2020-10-26 15:36:12 +08:00
|
|
|
|
|
|
|
|
|
if 'reply_to_message' in msg.keys() and msg['reply_to_message']['from'] != msg_from:
|
2020-10-26 02:11:53 +08:00
|
|
|
|
msg_rpl = msg['reply_to_message']['from']
|
|
|
|
|
else:
|
2020-10-26 15:36:12 +08:00
|
|
|
|
msg_rpl = {'first_name': '自己', 'id': msg_from['id']}
|
|
|
|
|
|
|
|
|
|
mention_from = mention(msg_from)
|
|
|
|
|
mention_rpl = mention(msg_rpl)
|
2020-10-26 16:08:50 +08:00
|
|
|
|
text = get_text(mention_from, mention_rpl, command)
|
2020-10-26 02:11:53 +08:00
|
|
|
|
|
2020-10-26 15:36:12 +08:00
|
|
|
|
update.effective_message.reply_text(text, parse_mode='Markdown')
|
2020-10-26 02:11:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updater = Updater(token=Token, use_context=True)
|
|
|
|
|
dp = updater.dispatcher
|
2020-10-26 16:41:16 +08:00
|
|
|
|
dp.add_handler(MessageHandler(Filters.regex(parser), reply))
|
2020-10-26 02:11:53 +08:00
|
|
|
|
|
|
|
|
|
updater.start_polling()
|
|
|
|
|
updater.idle()
|