mirror of
https://github.com/KimigaiiWuyi/GenshinUID.git
synced 2025-05-12 06:55:58 +08:00
✨ 支持向nonebot.adapter.telegram
发送按钮
This commit is contained in:
parent
5366bbf09d
commit
e41a67285c
@ -13,7 +13,7 @@ from nonebot.matcher import Matcher
|
|||||||
from nonebot.permission import SUPERUSER
|
from nonebot.permission import SUPERUSER
|
||||||
from nonebot.internal.adapter import Event
|
from nonebot.internal.adapter import Event
|
||||||
from websockets.exceptions import ConnectionClosed
|
from websockets.exceptions import ConnectionClosed
|
||||||
from nonebot import require, on_notice, on_message, on_fullmatch
|
from nonebot import on, require, on_notice, on_message, on_fullmatch
|
||||||
|
|
||||||
require('nonebot_plugin_apscheduler')
|
require('nonebot_plugin_apscheduler')
|
||||||
|
|
||||||
@ -41,6 +41,7 @@ else:
|
|||||||
is_repeat = False
|
is_repeat = False
|
||||||
|
|
||||||
|
|
||||||
|
@on('inline').handle()
|
||||||
@get_notice.handle()
|
@get_notice.handle()
|
||||||
async def get_notice_message(bot: Bot, ev: Event):
|
async def get_notice_message(bot: Bot, ev: Event):
|
||||||
if gsclient is None:
|
if gsclient is None:
|
||||||
@ -142,6 +143,29 @@ async def get_notice_message(bot: Bot, ev: Event):
|
|||||||
user_type = 'group'
|
user_type = 'group'
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
elif bot.adapter.get_name() == 'Telegram':
|
||||||
|
from nonebot.adapters.telegram.event import CallbackQueryEvent
|
||||||
|
|
||||||
|
if isinstance(ev, CallbackQueryEvent):
|
||||||
|
if ev.from_.is_bot:
|
||||||
|
return
|
||||||
|
|
||||||
|
user_id = str(ev.from_.id)
|
||||||
|
msg_id = str(ev.id)
|
||||||
|
sender = ev.from_.dict()
|
||||||
|
if ev.message:
|
||||||
|
if ev.message.chat.type == 'private':
|
||||||
|
user_type = 'direct'
|
||||||
|
else:
|
||||||
|
user_type = 'group'
|
||||||
|
group_id = str(ev.message.chat.id)
|
||||||
|
message = [Message('text', ev.data)]
|
||||||
|
else:
|
||||||
|
logger.debug('[gsuid] 不支持该 Telegram 事件...')
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
logger.debug('[gsuid] 不支持该 Telegram 事件...')
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -455,10 +479,10 @@ async def get_all_message(bot: Bot, ev: Event):
|
|||||||
elif bot.adapter.get_name() == 'Villa':
|
elif bot.adapter.get_name() == 'Villa':
|
||||||
from nonebot.adapters.villa import SendMessageEvent
|
from nonebot.adapters.villa import SendMessageEvent
|
||||||
|
|
||||||
|
if isinstance(ev, SendMessageEvent):
|
||||||
sender = {
|
sender = {
|
||||||
'nickname': ev.nickname,
|
'nickname': ev.nickname,
|
||||||
}
|
}
|
||||||
if isinstance(ev, SendMessageEvent):
|
|
||||||
user_type = 'group'
|
user_type = 'group'
|
||||||
msg_id = ev.msg_uid
|
msg_id = ev.msg_uid
|
||||||
group_id = f'{ev.villa_id}-{ev.room_id}'
|
group_id = f'{ev.villa_id}-{ev.room_id}'
|
||||||
|
@ -227,6 +227,7 @@ class GsClient:
|
|||||||
image,
|
image,
|
||||||
file,
|
file,
|
||||||
node,
|
node,
|
||||||
|
buttons,
|
||||||
msg.target_id,
|
msg.target_id,
|
||||||
)
|
)
|
||||||
elif msg.bot_id == 'kaiheila':
|
elif msg.bot_id == 'kaiheila':
|
||||||
@ -450,6 +451,15 @@ def _kaiheila_kb_group(buttons: List[Dict]):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _tg_kb(button: Dict):
|
||||||
|
from nonebot.adapters.telegram.model import InlineKeyboardButton
|
||||||
|
|
||||||
|
return InlineKeyboardButton(
|
||||||
|
text=button['text'],
|
||||||
|
callback_data=button['data'],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def villa_send(
|
async def villa_send(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
content: Optional[str],
|
content: Optional[str],
|
||||||
@ -574,6 +584,10 @@ async def onebot_send(
|
|||||||
target_id: Optional[str],
|
target_id: Optional[str],
|
||||||
target_type: Optional[str],
|
target_type: Optional[str],
|
||||||
):
|
):
|
||||||
|
if target_id is None:
|
||||||
|
return
|
||||||
|
_target_id = int(target_id)
|
||||||
|
|
||||||
async def _send(content: Optional[str], image: Optional[str]):
|
async def _send(content: Optional[str], image: Optional[str]):
|
||||||
from nonebot.adapters.onebot.v11 import MessageSegment
|
from nonebot.adapters.onebot.v11 import MessageSegment
|
||||||
|
|
||||||
@ -595,27 +609,27 @@ async def onebot_send(
|
|||||||
'upload_group_file',
|
'upload_group_file',
|
||||||
file=str(path.absolute()),
|
file=str(path.absolute()),
|
||||||
name=file_name,
|
name=file_name,
|
||||||
group_id=target_id,
|
group_id=_target_id,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await bot.call_api(
|
await bot.call_api(
|
||||||
'upload_private_file',
|
'upload_private_file',
|
||||||
file=str(path.absolute()),
|
file=str(path.absolute()),
|
||||||
name=file_name,
|
name=file_name,
|
||||||
user_id=target_id,
|
user_id=_target_id,
|
||||||
)
|
)
|
||||||
del_file(path)
|
del_file(path)
|
||||||
else:
|
else:
|
||||||
if target_type == 'group':
|
if target_type == 'group':
|
||||||
await bot.call_api(
|
await bot.call_api(
|
||||||
'send_group_msg',
|
'send_group_msg',
|
||||||
group_id=target_id,
|
group_id=_target_id,
|
||||||
message=result_msg,
|
message=result_msg,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await bot.call_api(
|
await bot.call_api(
|
||||||
'send_private_msg',
|
'send_private_msg',
|
||||||
user_id=target_id,
|
user_id=_target_id,
|
||||||
message=result_msg,
|
message=result_msg,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -623,17 +637,16 @@ async def onebot_send(
|
|||||||
if target_type == 'group':
|
if target_type == 'group':
|
||||||
await bot.call_api(
|
await bot.call_api(
|
||||||
'send_group_forward_msg',
|
'send_group_forward_msg',
|
||||||
group_id=target_id,
|
group_id=_target_id,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await bot.call_api(
|
await bot.call_api(
|
||||||
'send_private_forward_msg',
|
'send_private_forward_msg',
|
||||||
user_id=target_id,
|
user_id=_target_id,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
)
|
)
|
||||||
|
|
||||||
target_id = int(target_id)
|
|
||||||
if node:
|
if node:
|
||||||
messages = [
|
messages = [
|
||||||
to_json(
|
to_json(
|
||||||
@ -1188,33 +1201,56 @@ async def telegram_send(
|
|||||||
image: Optional[str],
|
image: Optional[str],
|
||||||
file: Optional[str],
|
file: Optional[str],
|
||||||
node: Optional[List[Dict]],
|
node: Optional[List[Dict]],
|
||||||
|
buttons: Optional[Union[List[Dict], List[List[Dict]]]],
|
||||||
target_id: Optional[str],
|
target_id: Optional[str],
|
||||||
):
|
):
|
||||||
|
from nonebot.adapters.telegram.bot import Bot
|
||||||
|
from nonebot.adapters.telegram.model import InlineKeyboardMarkup
|
||||||
|
from nonebot.adapters.telegram.message import File, Entity, Message
|
||||||
|
|
||||||
|
assert isinstance(bot, Bot)
|
||||||
|
|
||||||
|
if target_id is None:
|
||||||
|
return
|
||||||
|
|
||||||
async def _send(content: Optional[str], image: Optional[str]):
|
async def _send(content: Optional[str], image: Optional[str]):
|
||||||
result = {}
|
message = Message()
|
||||||
|
reply_markup = None
|
||||||
if image:
|
if image:
|
||||||
if image.startswith('link://'):
|
if image.startswith('link://'):
|
||||||
img_bytes = await download_image(image.replace('link://', ''))
|
img_bytes = await download_image(image.replace('link://', ''))
|
||||||
else:
|
else:
|
||||||
img_bytes = base64.b64decode(image.replace('base64://', ''))
|
img_bytes = base64.b64decode(image.replace('base64://', ''))
|
||||||
result['photo'] = img_bytes
|
message.append(File.photo(img_bytes))
|
||||||
if content:
|
if content:
|
||||||
result['text'] = content
|
message.append(Entity.text(content))
|
||||||
if file:
|
if file:
|
||||||
file_name, file_content = file.split('|')
|
file_name, file_content = file.split('|')
|
||||||
path = Path(__file__).resolve().parent / file_name
|
path = Path(__file__).resolve().parent / file_name
|
||||||
store_file(path, file_content)
|
store_file(path, file_content)
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
doc = f.read()
|
doc = f.read()
|
||||||
result['document'] = doc
|
message.append(File.document(doc))
|
||||||
del_file(path)
|
del_file(path)
|
||||||
|
if buttons:
|
||||||
|
bt = []
|
||||||
|
kb = []
|
||||||
|
for button in buttons:
|
||||||
|
if isinstance(button, Dict):
|
||||||
|
bt.append(_tg_kb(button))
|
||||||
|
if len(bt) >= 2:
|
||||||
|
kb.append(bt)
|
||||||
|
bt = []
|
||||||
|
if isinstance(button, List):
|
||||||
|
_t = []
|
||||||
|
for i in button:
|
||||||
|
_t.append(_tg_kb(i))
|
||||||
|
else:
|
||||||
|
kb.append(_t)
|
||||||
|
_t = []
|
||||||
|
reply_markup = InlineKeyboardMarkup(inline_keyboard=kb)
|
||||||
|
|
||||||
if content:
|
await bot.send_to(target_id, message, reply_markup=reply_markup)
|
||||||
await bot.call_api('send_message', chat_id=target_id, **result)
|
|
||||||
if image:
|
|
||||||
await bot.call_api('send_photo', chat_id=target_id, **result)
|
|
||||||
if file:
|
|
||||||
await bot.call_api('send_document', chat_id=target_id, **result)
|
|
||||||
|
|
||||||
if node:
|
if node:
|
||||||
for _msg in node:
|
for _msg in node:
|
||||||
@ -1399,3 +1435,4 @@ async def onebot_v12_send(
|
|||||||
await _send(_msg['data'], None)
|
await _send(_msg['data'], None)
|
||||||
else:
|
else:
|
||||||
await _send(content, image)
|
await _send(content, image)
|
||||||
|
await _send(content, image)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user