From 8fda8d6c88f3754b694e257872aa0ee515bb4d73 Mon Sep 17 00:00:00 2001 From: Rongrong <15956627+Rongronggg9@users.noreply.github.com> Date: Tue, 10 Nov 2020 01:46:55 +0800 Subject: [PATCH] linked channel and anonymous admin support --- SlashBot.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/SlashBot.py b/SlashBot.py index 1eaf697..81bc897 100644 --- a/SlashBot.py +++ b/SlashBot.py @@ -2,6 +2,8 @@ import os import re from telegram.ext import Updater, MessageHandler, filters +TELEGRAM = 777000 +GROUP = 1087968824 Filters = filters.Filters parser = re.compile(r'^\/(\S+)([  ]*)(.*)$') @@ -12,6 +14,27 @@ else: raise Exception('no token') +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: @@ -36,17 +59,12 @@ def reply(update, context): print(update.to_dict()) msg = update.to_dict()['message'] command = msg['text'] - msg_from = msg['from'] + from_user, rpl_user = get_users(msg) - if 'reply_to_message' in msg.keys() and msg['reply_to_message']['from'] != msg_from: - msg_rpl = msg['reply_to_message']['from'] - else: - msg_rpl = {'first_name': '自己', 'id': msg_from['id']} + mention_from, mention_rpl = mention(from_user), mention(rpl_user) - mention_from = mention(msg_from) - mention_rpl = mention(msg_rpl) text = get_text(mention_from, mention_rpl, command) - print(text) + print(text, end='\n\n') update.effective_message.reply_text(text, parse_mode='Markdown')