diff --git a/modules/ip.py b/modules/ip.py index 9c3381a..c2726bd 100644 --- a/modules/ip.py +++ b/modules/ip.py @@ -1,79 +1,58 @@ from urllib.parse import urlparse from pyrogram import Client, filters -from pyrogram.types import Message +from pyrogram.types import ( + Message, + InlineQuery, + InlineQueryResultArticle, + InputTextMessageContent, +) from defs.ip import ip_info from init import request, bot +async def get_rep_text(url: str): + url = urlparse(url) + if url.hostname or url.path: + url = url.hostname or url.path + ipinfo_json = ( + await request.get( + "http://ip-api.com/json/" + + url + + "?fields=status,message,country,regionName,city," + "lat,lon,isp," + "org,as,mobile,proxy,hosting,query" + ) + ).json() + if ipinfo_json["status"] == "success": + return ip_info(url, ipinfo_json) + + +async def get_rep_text_from_msg(message: Message): + if not message: + return + if not message.entities: + return + for num in range(0, len(message.entities)): + url = message.text[ + message.entities[num].offset : message.entities[num].offset + + message.entities[num].length + ] + if t := await get_rep_text(url): + return t + + @bot.on_message(filters.incoming & filters.command(["ip", f"ip@{bot.me.username}"])) async def ip_command(_: Client, message: Message): msg = await message.reply("正在查询中...") - rep_text = "" reply = message.reply_to_message - if reply: - if reply.entities: - for num in range(0, len(reply.entities)): - url = reply.text[ - reply.entities[num].offset : reply.entities[num].offset - + reply.entities[num].length - ] - url = urlparse(url) - if url.hostname or url.path: - if url.hostname: - url = url.hostname - else: - url = url.path - ipinfo_json = ( - await request.get( - "http://ip-api.com/json/" - + url - + "?fields=status,message,country,regionName,city," - "lat,lon,isp," - "org,as,mobile,proxy,hosting,query" - ) - ).json() - if ipinfo_json["status"] == "success": - rep_text = ip_info(url, ipinfo_json) - text = "" - if message.entities: - for num in range(0, len(message.entities)): - url = message.text[ - message.entities[num].offset : message.entities[num].offset - + message.entities[num].length - ] - url = urlparse(url) - if url.hostname or url.path: - if url.hostname: - url = url.hostname - else: - url = url.path - ipinfo_json = ( - await request.get( - "http://ip-api.com/json/" - + url - + "?fields=status,message,country,regionName,city,lat," - "lon,isp," - "org,as,mobile,proxy,hosting,query" - ) - ).json() - if ipinfo_json["status"] == "success": - text = ip_info(url, ipinfo_json) - if text == "": - url = message.text[4:] - if url != "": - ipinfo_json = ( - await request.get( - "http://ip-api.com/json/" - + url - + "?fields=status,message,country,regionName,city,lat," - "lon,isp," - "org,as,mobile,proxy,hosting,query" - ) - ).json() - if ipinfo_json["status"] == "success": - text = ip_info(url, ipinfo_json) + rep_text = await get_rep_text_from_msg(reply) or "" + text = await get_rep_text_from_msg(message) or "" + if not text: + url = message.text[4:] + if url: + text = await get_rep_text(url) or "" if rep_text == "" and text == "": await msg.edit("没有找到要查询的 ip/域名 ...") elif rep_text != "" and text != "": @@ -82,3 +61,25 @@ async def ip_command(_: Client, message: Message): ) else: await msg.edit(f"{rep_text}{text}", disable_web_page_preview=True) + + +@bot.on_inline_query(filters.regex("^ip")) +async def ip_inline(_: Client, inline_query: InlineQuery): + url = inline_query.query[3:] + text = await get_rep_text(url) + results = [ + InlineQueryResultArticle( + title="查询 IP 数据成功" if text else "查询 IP 数据失败", + description=url, + input_message_content=InputTextMessageContent( + message_text=text or "没有找到要查询的 ip/域名 ..." + ), + # reply_markup=InlineKeyboardMarkup( + # [[InlineKeyboardButton(text="重试", callback_data="dc")]] + # ), + ) + ] + await inline_query.answer( + results=results, + cache_time=0, + ) diff --git a/modules/start.py b/modules/start.py index b9783ff..cb1edfd 100644 --- a/modules/start.py +++ b/modules/start.py @@ -71,6 +71,11 @@ async def empty_inline(_, inline_query: InlineQuery): ), description="使用 exchange 来查询汇率数据", ), + InlineQueryResultArticle( + title="ip", + input_message_content=InputTextMessageContent("使用 ip 来查询 ip 数据"), + description="使用 ip 来查询 ip 数据", + ), ] return await inline_query.answer( results=results,