sycgram/plugins/ping.py
2022-04-09 18:04:20 +08:00

31 lines
941 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

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.

from core import command
from pyrogram import Client
from pyrogram.types import Message
from tools.sessions import session
from tools.helpers import Parameters, show_cmd_tip, show_exception
@Client.on_message(command('ip'))
async def ip(_: Client, msg: Message):
"""查询ip信息"""
cmd, address = Parameters.get(msg)
if not address:
await show_cmd_tip(msg, cmd)
return
async def get_api(api: str) -> str:
async with session.get(api) as resp:
if resp.status == 200:
data = await resp.json()
tmp = '\n'.join(f"{k}`{v}`" for k, v in data.items())
return tmp if tmp else "😂 No Response ~"
resp.raise_for_status()
try:
api = f"http://ip-api.com/json/{address}"
text = await get_api(api)
except Exception as e:
return await show_exception(msg, e)
else:
await msg.edit_text(text)