SendCodeRequest不会无误地传递代码,并且这种情况仅在Heroku的服务器端发生

SendCodeRequest不会无误地传递代码,并且这种情况仅在Heroku的服务器端发生,heroku,telegram,telethon,Heroku,Telegram,Telethon,这是我的情况:在我的本地机器和服务器上使用相同的Telethon代码。从本地机器请求授权代码可以正常工作。从服务器请求代码不会产生任何错误,并且不会发送代码。有时它甚至可以在服务器上运行,而不需要对代码进行任何更改 我想可能有一些ip块或与ip相关的东西,因为这是服务器端唯一可能不同的东西:Heroku动态分配ip地址,因此,可能有一些子网由于某种原因被电报API阻塞。但是没有错误,这真的很奇怪。ip地址太多,无法反驳这一假设。我需要捕获至少一个ip地址,这会给我相反的结果:一次它收到了代码,另

这是我的情况:在我的本地机器和服务器上使用相同的Telethon代码。从本地机器请求授权代码可以正常工作。从服务器请求代码不会产生任何错误,并且不会发送代码。有时它甚至可以在服务器上运行,而不需要对代码进行任何更改

我想可能有一些ip块或与ip相关的东西,因为这是服务器端唯一可能不同的东西:Heroku动态分配ip地址,因此,可能有一些子网由于某种原因被电报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)
发送\u代码\u请求是来自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分钟的间隔,所以这绝对不是超时问题。而且:即使在半分钟后从失败的服务器端尝试请求本地代码时:代码几乎马上就来了。

最奇怪的是,您能够看到服务器上的
SentCode
响应,这让我感到困惑。老实说,我只会使用文档中描述的
StringSession
,然后在本地登录。这个问题似乎与AWS有关。最奇怪的是,您能够在服务器上看到
SentCode
响应,这让我感到困惑。老实说,我会使用文档中描述的
StringSession
,然后在本地登录。这个问题似乎与AWS有关