0

根据 Pyrogram 库文档,我正在尝试使用可调用函数设置“phone_code”参数,但我总是收到此错误,有人可以帮助我吗?

from pyrogram import Client
import time

def codePhone(phone_number):
    print('numero')
    sleep (10)
    return '2154'

app = Client("my_account",phone_number='39**********',phone_code=codePhone)
with app:
    app.send_message("me", "Hi!")

错误:

Pyrogram v1.0.7, Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)

The confirmation code has been sent via SMS
Traceback (most recent call last):
  File "/home/Topnews/Test/tdlib/prova3.py", line 10, in <module>
    with app:
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/client.py", line 248, in __enter__
    return self.start()
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/sync.py", line 56, in async_to_sync_wrap
    return loop.run_until_complete(coroutine)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/methods/utilities/start.py", line 57, in start
    await self.authorize()
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/client.py", line 327, in authorize
    signed_in = await self.sign_in(self.phone_number, sent_code.phone_code_hash, self.phone_code)
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/methods/auth/sign_in.py", line 61, in sign_in
    r = await self.send(
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/methods/advanced/send.py", line 77, in send
    r = await self.session.send(
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/session/session.py", line 441, in send
    return await self._send(data, timeout=timeout)
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/session/session.py", line 364, in _send
    message = self.msg_factory(data)
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/session/internals/msg_factory.py", line 37, in __call__
    len(body)
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/raw/core/tl_object.py", line 76, in __len__
    return len(self.write())
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/raw/functions/auth/sign_in.py", line 81, in write
    data.write(String(self.phone_code))
  File "/usr/local/lib/python3.8/dist-packages/pyrogram/raw/core/primitives/string.py", line 31, in __new__
    return super().__new__(cls, value.encode())
AttributeError: 'function' object has no attribute 'encode'
4

3 回答 3

1

抱歉,我不认为我完全理解这里的要求,但从它的外观来看,你需要让它工作。因此,在这种情况下,您需要了解您将什么作为参数传递给函数。

由于该codePhone函数需要一个参数(电话号码),但在您的客户端类中,您只是将其作为回调引用发送,这似乎是不正确的。

同样根据模块(Pyrogram)的文档herephone_code参数需要是字符串(最有可能是数字字符串->'2154')。所以尝试使用它。

让我知道这是否适合你。

TL;DR:使用

app = Client("my_account",phone_number='39**********',phone_code='2154')
于 2021-01-03T19:03:29.163 回答
0

您使用的是 1.0.7 版本,但在版本中删除了对客户端参数回调函数的支持。0.16.0

删除了对客户端参数(如电话号码、电话代码、密码等)的回调函数的支持,以支持更简单和顺序的授权流程。

目前它希望您str提供phone_code.

于 2021-01-03T19:04:27.913 回答
-2

phone_number类中的参数Client采用字符串,而不是函数。如果您想自动化电话号码登录的过程:

from pyrogram import Client
from pyrogram.errors import SessionPasswordNeeded
import os

api_id    = input('API ID: ')
api_hash  = input('API HASH: ')
phone     = input('Phone Number (with +): ')

def connect():
    try:
        client = Client(phone, api_id, api_hash)
        client.connect()
        code   = client.send_code(phone)
        try:
            signed_in = client.sign_in(phone, code.phone_code_hash, input('Verification Code: '))
            client.accept_terms_of_service(signed_in.id)
        except SessionPasswordNeeded:
            client.check_password(input('Two-Step Verification password: '))
        client.disconnect()
    except Exception as ex:
        os.remove(f'{phone}.session')
        print(f'Error !\n\t`{type(ex).__name__}`\n\t{ex}')
        return

    with client as app:
        app.send_message('me', "Hey it's YOU!")

if __name__ == '__main__':
    connect()
于 2021-05-29T02:25:13.320 回答