You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需API验证WhatsApp号码是否已注册的程序问题排查

问题分析与解决方案

原代码的核心错误

你写的代码逻辑完全不对——carrier.name_for_number()这个函数是用来获取手机号的运营商名称(比如“China Mobile”“AT&T”),和该号码是否注册WhatsApp没有任何关系。哪怕号码已经注册了WhatsApp,这个函数返回的还是运营商的名字,不会包含“WhatsApp”,所以不管输入什么有效号码,都会返回“NO”。

关键限制说明

WhatsApp不会公开用户的注册状态数据,所以完全不调用任何接口的话,没办法准确判断一个号码是否注册了WhatsApp。但可以通过模拟WhatsApp客户端的交互逻辑来间接验证,不过这种方法可能违反WhatsApp的服务条款,而且随着WhatsApp版本更新容易失效。

可行的替代方案

可以使用第三方库yowsup(基于WhatsApp的XMPP协议)来检查号码的注册状态,以下是简化示例:

首先安装依赖:

pip install yowsup2

然后编写代码:

from yowsup.layers.auth import AuthError
from yowsup.layers.network import YowNetworkLayer
from yowsup.stacks import YowStackBuilder
from yowsup.common import YowConstants
from yowsup.layers import YowLayerEvent
import threading

def check_whatsapp_number(phone_number):
    # 手机号需要带国家码,比如+8613xxxxxxxxx
    target_jid = f"{phone_number.replace('+', '')}@{YowConstants.WHATSAPP_SERVER}"
    
    stack_builder = YowStackBuilder()
    stack = stack_builder\
        .pushDefaultLayers(True)\
        .build()
    
    stack.setCredentials("your_phone", "your_password")  # 需要填入自己的WhatsApp账号凭证
    
    is_registered = False
    
    def on_auth_success():
        nonlocal is_registered
        # 发送存在性查询请求
        stack.sendPresenceSubscription(target_jid)
    
    def on_presence_available(presence):
        nonlocal is_registered
        if presence.getFrom() == target_jid:
            is_registered = True
            stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT))
    
    stack.getLayer(0).registerCallback("auth_success", on_auth_success)
    stack.getLayer(0).registerCallback("presence_available", on_presence_available)
    
    threading.Thread(target=stack.start).start()
    stack.loop(timeout=5)  # 等待5秒获取结果
    
    return "YES" if is_registered else "NO"

# 使用示例
phone_number = input("Enter the phone number (with country code, e.g., +8613xxxxxxxxx): ")
print(check_whatsapp_number(phone_number))

注意事项

  1. 你需要先获取自己WhatsApp账号的凭证(手机号和加密密码),可以通过yowsup-cli工具生成。
  2. 这种方法依赖WhatsApp的私有协议,随时可能失效,且可能违反WhatsApp的服务条款,谨慎使用。
  3. 如果可以接受调用API,WhatsApp官方提供了验证接口,但这不符合你“无需调用API”的要求。

内容的提问来源于stack exchange,提问作者pp aa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 02:37:22