Skip to content

Commit

Permalink
fix channel
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Jan 20, 2023
1 parent 6a42f7d commit 39c743a
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions idbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
from typing import Any, Union

from pyrogram import Client, filters, types
from pyrogram import Client, filters, types, raw
from tgbot_ping import get_runtime

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
Expand Down Expand Up @@ -45,7 +45,7 @@ def create_app():
service_count = 0


def get_detail(user: "Union[types.User, types.Chat]") -> "str":
def get_user_detail(user: "Union[types.User, types.Chat]") -> "str":
global service_count
service_count += 1
if user is None:
Expand All @@ -64,6 +64,18 @@ def get_detail(user: "Union[types.User, types.Chat]") -> "str":
"""


def get_channel_detail(channel) -> "str":
global service_count
service_count += 1
return f"""
Channel/group detail(you can also forward message to see detail):
name: `@{channel.chats[0].username} `
title: `{channel.chats[0].title}`
id: `-100{channel.chats[0].id}`
"""


@app.on_message(filters.command(["start"]))
def start_handler(client: "Client", message: "types.Message"):
chat_id = message.chat.id
Expand All @@ -81,7 +93,7 @@ def help_handler(client: "Client", message: "types.Message"):

@app.on_message(filters.command(["getme"]))
def getme_handler(client: "Client", message: "types.Message"):
me = get_detail(message.from_user)
me = get_user_detail(message.from_user)
message.reply_text(me, quote=True)


Expand All @@ -100,7 +112,7 @@ def start_handler(client: "Client", message: "types.Message"):

@app.on_message(filters.command(["getgroup"]))
def getgroup_handler(client: "Client", message: "types.Message"):
me = get_detail(message.chat)
me = get_user_detail(message.chat)
message.reply_text(me, quote=True)


Expand All @@ -118,21 +130,41 @@ def getgroup_compatibly_handler(client: "Client", message: "types.Message"):
@app.on_message(filters.forwarded & filters.private)
def forward_handler(client: "Client", message: "types.Message"):
fwd = message.forward_from or message.forward_from_chat
me = get_detail(fwd)
me = get_user_detail(fwd)
message.reply_text(me, quote=True)


def get_users(username):
user: "Union[types.User, Any]" = app.get_users(username)
return get_user_detail(user)


def get_channel(username):
peer: "Union[raw.base.InputChannel, Any]" = app.resolve_peer(username)
result = app.invoke(
raw.functions.channels.GetChannels(
id=[peer]
)
)
return get_channel_detail(result)


@app.on_message(filters.text & filters.private)
def private_handler(client: "Client", message: "types.Message"):
username = re.sub(r"@+|https://t.me/", "", message.text)
try:
user: "Union[types.User, Any]" = client.get_users(username)
me = get_detail(user)
except Exception as e:
logging.error(traceback.format_exc())
me = e

message.reply_text(me, quote=True)
funcs = [get_users, get_channel]
text = ""

for func in funcs:
try:
text = func(username)
if text:
break
except Exception as e:
logging.error(traceback.format_exc())
text = e

message.reply_text(text, quote=True)


if __name__ == '__main__':
Expand Down

0 comments on commit 39c743a

Please sign in to comment.