Pyrogram调用send_code接口新账号无法接收验证码问题
问题:Pyrogram send_code 无法给新注册Telegram账号发送验证码
现象
- 调用Pyrogram的
send_code函数给新注册的Telegram账号发送验证码时,无法收到验证码;但给已存在的账号发送时可正常接收。 - 使用Telegram官方APP注册该新账号时,能正常接收验证码。
相关代码
phone_number = "+79111443505" client = Client(":popo", app_id, api_hash) await client.connect() sent_code_info = await client.send_code(phone_number=phone_number, force_sms=True) print(sent_code_info) phone_code = input("Please enter your phone code: ") # Sent phone code using last function
返回的sent_code_info信息
SentCodeInfo(phone_code_hash='[隐藏哈希值]', type=SentCodeTypeSms(length=5), next_type=None, timeout=60)
解决方案
1. 移除force_sms=True参数
新注册账号的Telegram风控机制会优先推荐语音验证码或APP内验证方式,强制指定SMS可能触发反垃圾拦截,导致短信无法送达。修改代码让Telegram自动选择验证渠道:
sent_code_info = await client.send_code(phone_number=phone_number)
2. 提升API账号信任等级
新创建的Telegram API账号(app_id + api_hash)初始信任度低,频繁调用send_code易被判定为可疑操作:
- 先用该API账号绑定一个已完成验证的Telegram主账号
- 确保API账号的注册手机号为真实已验证号码
3. 更换IP地址
如果当前使用的IP属于Telegram标记的垃圾IP段(比如部分数据中心IP),新账号的验证码发送会被限制:
- 切换到原生住宅IP尝试
- 先用官方APP在该IP环境下测试注册流程,确认IP无限制
4. 确认账号状态
确保目标手机号处于完全未注册Telegram的状态,避免处于“待验证”的中间状态:
- 先用官方APP完成注册后再用Pyrogram操作
- 若之前注册过,彻底注销该手机号的Telegram关联信息后重试
5. 捕获异常排查隐性限制
send_code返回成功信息不代表验证码实际发送,可添加异常捕获逻辑排查问题:
try: sent_code_info = await client.send_code(phone_number=phone_number) print(sent_code_info) except Exception as e: print(f"发送验证码失败: {str(e)}")
内容的提问来源于stack exchange,提问作者Stranik
相关产品推荐
相关产品推荐

