Skip to content

send_gift() with text parameter causes TypeError: object of type 'NoneType' has no len() #169

@ExodusOfGraf

Description

@ExodusOfGraf

Checklist

  • I am sure the error is coming from Pyrofork's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U git+https://github.com/Mayuri-Chan/pyrofork and reproduced the issue using the latest development version

Description

Pyrofork version: 2.3.68, 2.3.69
Python version: 3.11
Operating System: Linux

When calling client.send_gift() with the text parameter, the method raises a TypeError because entities is None instead of an empty list []

The gift should be sent successfully with the text message attached, but the method raises the following error:

TypeError: object of type 'NoneType' has no len()

Traceback:
File "pyrogram/methods/payments/send_gift.py", line 90, in send_gift
form = await self.invoke(
File "pyrogram/raw/types/text_with_entities.py", line 79, in write
b.write(Vector(self.entities))
File "pyrogram/raw/core/primitives/vector.py", line 74, in new
[Int(cls.ID, False), Int(len(value))]
TypeError: object of type 'NoneType' has no len()

In pyrogram/methods/payments/send_gift.py line 87:
message=raw.types.TextWithEntities(text=text, entities=entities) if text else None

When text is provided but entities is None (default), the TextWithEntities object is created with entities=None. Later, when Pyrogram tries to serialize this object, it calls len(self.entities) which fails because None has no length.
I tried change line 87 to:
message=raw.types.TextWithEntities(text=text, entities=entities or []) if text else None

Steps to reproduce

from pyrogram import Client

app = Client("my_account")

async with app:
# This will fail with TypeError
await app.send_gift(
chat_id=123456789,
gift_id=5170233102089322756,
text="Hello! This is a gift message"
)

Code example

if message_text and message_text.strip():
                    await client.send_gift(
                        chat_id=user['user_id'],
                        gift_id=5170233102089322756,
                        text=message_text.strip()
                    )
                else:
                    await client.send_gift(
                        chat_id=user['user_id'],
                        gift_id=5170233102089322756
                    )

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions