as per the Pyrogram library documentation, I'm trying to set the "phone_code" parameter with a callable function but I always get this error, can anyone help me?
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!")
the error:
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'
Sorry, I don't think I fully understand the requirement here, but from the looks of it you need to make it work. So in that case, you need to understand what are you passing as the argument to the function.
Since the codePhone
function takes an argument (phone_number) but in your Client class you're just sending it as callback reference which seems to be incorrect.
Also as per the documentation for the module (Pyrogram) here says that the phone_code
argument needs to be string (most likely string of numbers -> '2154'). So try using that instead.
Let me know if that works for you.
TL;DR: Use
app = Client("my_account",phone_number='39**********',phone_code='2154')
You are using version 1.0.7, but support for callback functions for parameters for Client was removed in ver. 0.16.0
Removed support for callback functions for Client arguments like phone_number, phone_code, password, … in favour of a simpler and sequential authorization flow.
At the moment it expects that you supply str
for phone_code
.
phone_number
parameter in Client
class takes a string, not a function. If you want to automate the process of phone number logins:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.