新增大量WhatsApp适配器相关代码,包括账号管理、会话处理、消息收发、验证授权、媒体处理、互动命令、审批流程、健康检测等完整功能模块,搭建基础的Baileys协议WhatsApp接入能力
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_PHONE_PATTERN = re.compile(r"^\+?[\d\s\-()]{7,20}$")
|
|
|
|
|
|
class TargetResolver:
|
|
def __init__(self):
|
|
self._suffix = "s.whatsapp.net"
|
|
|
|
def looks_like_phone(self, text: str) -> bool:
|
|
return bool(_PHONE_PATTERN.match(text.strip()))
|
|
|
|
def resolve_jid(self, target: str) -> str:
|
|
target = target.strip()
|
|
|
|
if "@" in target:
|
|
return target
|
|
|
|
if self.looks_like_phone(target):
|
|
phone = target.lstrip("+").replace(" ", "").replace("-", "").replace("(", "").replace(")", "")
|
|
return f"{phone}@{self._suffix}"
|
|
|
|
return f"{target}@{self._suffix}"
|
|
|
|
def resolve_group_jid(self, target: str) -> str:
|
|
if "@g.us" in target:
|
|
return target
|
|
target = target.strip().removesuffix("@g.us").removesuffix("@s.whatsapp.net")
|
|
return f"{target}@g.us"
|
|
|
|
def extract_phone(self, jid: str) -> str:
|
|
return jid.split("@")[0]
|
|
|
|
def is_group(self, jid: str) -> bool:
|
|
return "@g.us" in jid
|
|
|
|
def is_broadcast(self, jid: str) -> bool:
|
|
return "@broadcast" in jid
|