heroku - SendCodeRequest 没有错误地传递代码,这仅在使用 Heroku 的服务器端发生
问题描述
这是我的情况:在我的本地机器和服务器上使用相同的 Telethon 代码。从本地机器请求授权码工作正常。从服务器请求代码不会产生任何错误,并且不会发送代码。有时它甚至可以在服务器上运行,而无需对代码进行任何更改。
我想可能有一些 ip 块或与 ip 相关的东西,因为这是服务器端唯一可能不同的东西:Heroku 动态分配 ip 地址,因此,某些子网可能会被 Telegram API 阻止原因。但是没有错误,这真的很奇怪。有太多的 IP 地址来反驳这个假设。我需要至少捕获一个 ip 地址,它会给我相反的结果:一次它收到了代码,而另一次没有。所以我被这种情况困住了,不知道如何解决或澄清它。
global t
t = None
async def ssssendCode(phone):
global t
try:
if os.path.isfile(phone+'.session'):
logger.debug('client file exists')
else:
logger.debug('client file does not exist')
if t is None:
t = TelegramClient(phone, settings['telegramClientAPIId'], settings['telegramClientAPIHash'])
t.phone = phone
#t.phone_code_hash = None
await t.connect()
#response = await t.send_code_request(phone=phone,force_sms=True)
s3_session.resource('s3').Bucket('telethon').upload_file(str(phone)+".session", str(phone)+".session")
logger.debug(str(requests.get('https://httpbin.org/ip').text))
response = await t.send_code_request(phone=phone)
logger.debug(str(t.is_connected()))
except Exception as e:
response = str(e)
return str(response)
响应本地机器请求的示例
SentCode(type=SentCodeTypeSms(length=5), phone_code_hash='b5b069a2a4122040f1', next_type=CodeTypeCall(), timeout=120)
响应服务器端请求的示例
SentCode(type=SentCodeTypeSms(length=5), phone_code_hash='0e89db0324c1af0149', next_type=CodeTypeCall(), timeout=120)
send_code_request 来自 Telethon,未经修改
async def send_code_request(
self: 'TelegramClient',
phone: str,
*,
force_sms: bool = False) -> 'types.auth.SentCode':
"""
Sends the Telegram code needed to login to the given phone number.
Arguments
phone (`str` | `int`):
The phone to which the code will be sent.
force_sms (`bool`, optional):
Whether to force sending as SMS.
Returns
An instance of :tl:`SentCode`.
Example
.. code-block:: python
phone = '+34 123 123 123'
sent = await client.send_code_request(phone)
print(sent)
"""
result = None
phone = utils.parse_phone(phone) or self._phone
phone_hash = self._phone_code_hash.get(phone)
if not phone_hash:
try:
result = await self(functions.auth.SendCodeRequest(
phone, self.api_id, self.api_hash, types.CodeSettings()))
except errors.AuthRestartError:
return await self.send_code_request(phone, force_sms=force_sms)
# If we already sent a SMS, do not resend the code (hash may be empty)
if isinstance(result.type, types.auth.SentCodeTypeSms):
force_sms = False
# phone_code_hash may be empty, if it is, do not save it (#1283)
if result.phone_code_hash:
self._phone_code_hash[phone] = phone_hash = result.phone_code_hash
else:
force_sms = True
self._phone = phone
if force_sms:
result = await self(
functions.auth.ResendCodeRequest(phone, phone_hash))
self._phone_code_hash[phone] = result.phone_code_hash
return result
以防万一:我在尝试从本地机器和服务器获取代码之间有超过 2 分钟的时间,所以这绝对不是超时问题。而且:即使从失败的服务器端尝试半分钟后立即从本地请求代码:代码几乎立即出现。
解决方案
推荐阅读
- html - 我们可以在引导程序 4 中使用部分折叠概念吗?
- clojure - Clojure - 让第一个 + 过滤器变得懒惰
- sql - COALESCE 中的 CASE
- r - gganimate中许多(> 50)状态的问题
- python - 循环列表-列表的长度但不需要创建的项目,pythonic?
- c# - 如何在 UWP 中添加 mediaplayerelements 控件
- c# - ASP.Net 子应用程序抛出实体框架错误。
- javascript - 堆叠 UIkit.modal 内置对话框
- javascript - AngularJs 文本框验证,使用正则表达式负前瞻限制字符
- node.js - Nodejs应用程序在本地主机上工作但在heroku上崩溃