SlashBot/SlashBot.py
2021-08-04 22:54:49 +08:00

91 lines
2.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import re
from typing import List, Dict, Union
import requests
from telegram.ext import Updater, MessageHandler, filters
TELEGRAM = 777000
GROUP = 1087968824
Filters = filters.Filters
parser = re.compile(r'^\/((?:[^  \\]|\\.)+)([  ]*)(.*)$')
escaping = ('\\ ', '\\ ')
# Docker env
if os.environ.get('TOKEN') and os.environ['TOKEN'] != 'X':
Token = os.environ['TOKEN']
else:
raise Exception('no token')
# Find someone's full name by their username
def find_name_by_username(username: str) -> str:
r = requests.get(f'https://t.me/{username}')
return re.search('(?<=<meta property="og:title" content=").*(?=")', r.text, re.IGNORECASE).group(0)
def get_user(msg):
if msg['from']['id'] == TELEGRAM:
return {'first_name': msg['forward_from_chat']['title'], 'id': msg['forward_from_chat']['id']}
elif msg['from']['id'] == GROUP:
return {'first_name': msg['chat']['title'], 'id': msg['chat']['id']}
else:
return msg['from']
def get_users(msg):
msg_from = msg
if 'reply_to_message' in msg.keys():
msg_rpl = msg['reply_to_message']
else:
msg_rpl = msg_from.copy()
from_user, rpl_user = get_user(msg_from), get_user(msg_rpl)
if rpl_user == from_user:
rpl_user = {'first_name': '自己', 'id': rpl_user['id']}
return from_user, rpl_user
def mention(user):
space = ' '
if 'last_name' not in user:
user['last_name'] = ''
space = ''
return f"[{user['first_name']}{space}{user['last_name']}](tg://user?id={user['id']})"
def get_text(mention_from, mention_rpl, command):
parsed = list(parser.search(delUsername.sub('', command)).groups())
for escape in escaping:
parsed[0] = parsed[0].replace(escape, escape[1:])
if parsed[0] == 'me':
return f"{mention_from}{bool(parsed[1])*' '}{parsed[2]}"
elif parsed[0] == 'you':
return f"{mention_rpl}{bool(parsed[1])*' '}{parsed[2]}"
elif parsed[2]:
return f"{mention_from} {parsed[0]} {mention_rpl} {parsed[2]}"
else:
return f"{mention_from} {parsed[0]}{mention_rpl}"
def reply(update, context):
print(update.to_dict())
msg = update.to_dict()['message']
command = msg['text']
from_user, rpl_user = get_users(msg)
mention_from, mention_rpl = mention(from_user), mention(rpl_user)
text = get_text(mention_from, mention_rpl, command)
print(text, end='\n\n')
update.effective_message.reply_text(text, parse_mode='Markdown')
updater = Updater(token=Token, use_context=True)
delUsername = re.compile('@' + updater.bot.username, re.I)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.regex(parser), reply))
updater.start_polling()
updater.idle()