main
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import re
5
6from loguru import logger
7from pyrogram.client import Client
8from pyrogram.types import Message, ReplyParameters
9
10from config import PREFIX
11from messages.parser import parse_msg
12from messages.sender import send2tg
13from messages.utils import equal_prefix, startswith_prefix
14from utils import i_am_bot
15
16OCR_BOT = "GLBetabot"
17
18
19async def send_to_ocr_bridge(client: Client, message: Message, **kwargs):
20 """Send photo to miaomiao bot for OCR.
21
22 See docs in `bridge/README.md` for details.
23
24 kwargs:
25 target_chat (int | str, optional): Send result to this telegram target chat. If not set, send to the trigger message's chat.
26 reply_msg_id (int, optional): If set to integer > 0, the result is sent as a reply message to this message_id.
27 If set to 0, reply to the trigger message itself.
28 If set to -1, do not send as a reply message.
29 """
30 # send docs if message == "/ocr", without reply
31 if equal_prefix(message.text, prefix=[PREFIX.OCR]) and not message.reply_to_message:
32 await send2tg(client, message, texts=f"**图片转文字**: 以`{PREFIX.OCR}`回复图片消息即可提取文字", **kwargs)
33 return
34 info = parse_msg(message)
35 msg = info["text"]
36 if not startswith_prefix(msg, prefix=[PREFIX.OCR]):
37 return
38 if await i_am_bot(client): # bot can't send message to other bots
39 return
40
41 # reply a message with /ocr
42 if message.reply_to_message:
43 message = message.reply_to_message
44 info = parse_msg(message, silent=True) # parse again
45
46 if info["mtype"] != "photo":
47 return
48 # get the img file_id
49 file_id = info["file_id"]
50
51 cid = kwargs["target_chat"] if kwargs.get("target_chat") else message.chat.id # MSG-A's cid
52 mid = kwargs["reply_msg_id"] if kwargs.get("reply_msg_id") else message.id # MSG-A's mid
53 msg += f" \n#ID=({cid},{mid})".replace("None", "-1")
54 logger.warning(f"OCR via 妙妙小工具 (@{OCR_BOT}): {msg!r}")
55 await client.send_photo(chat_id=f"@{OCR_BOT}", photo=file_id, caption=msg)
56
57
58async def forward_ocr_results(client: Client, message: Message):
59 """See docs in `bridge/README.md` for details."""
60 if message.from_user.username != OCR_BOT or not message.reply_to_message:
61 return
62 info = parse_msg(message)
63 reply_msg = message.reply_to_message
64 reply_info = parse_msg(reply_msg)
65
66 # this message should be a photo with captions
67 if info["mtype"] != "photo" or not info["text"]:
68 return
69 # this message should reply to a photo message starting with "/ocr"
70 if reply_info["mtype"] != "photo" or not startswith_prefix(reply_info["text"], prefix=[PREFIX.OCR]):
71 return
72
73 if matched := re.search(r"#ID=\((-?\d+),(-?\d+)\)", reply_info["text"]):
74 target_cid = matched.group(1) # MSG-A's cid
75 target_mid = int(matched.group(2)) if int(matched.group(2)) != -1 else None # MSG-A's mid
76 cid = message.chat.id # result's cid
77 mid = message.id # result's mid
78 logger.info(f"Forwarding chat=@{OCR_BOT}, id={mid} -> chat={target_cid}, id={target_mid}")
79 await client.copy_message(chat_id=target_cid, from_chat_id=cid, message_id=mid, reply_parameters=ReplyParameters(message_id=target_mid)) # type: ignore