0
relayer.py
async def send_gift(username, gift_id=config.teddy_id):
 client = TelegramClient(session_name, api_id, api_hash)
 client.start(phone=config.phone, password=config.two_factor)
 try:
 receiver_peer = await client.get_input_entity(username)
 print(receiver_peer)
 invoice = InputInvoiceStarGift(
 peer=receiver_peer,
 gift_id=gift_id
 )
 print(f"Generated Invoice: {invoice}")
 payment_form = client(functions.payments.GetPaymentFormRequest(invoice=invoice))
 if payment_form:
 print("Payment form retrieved successfully!")
 payment_result = client(functions.payments.SendStarsFormRequest(
 form_id=payment_form.form_id,
 invoice=invoice
 ))
 print(f"Payment successful: {payment_result.stringify()}")
 except Exception as e:
 print(f"An error occurred: {e}")

This is a function that sends teddy bear to the username given. It works perfectly fine without defining function send_gift(), but I need to make this function callable from different script (The relayer.py is never run as main). There is an issue "Cannot send requests while disconnected". I think I might be using client.start() wrong here. Would be grateful for any advice!

0

1 Answer 1

1

Asyncio should be showing you a warning already, client.start() in async context must be awaited, as the event loop is already running.

Also phone and password you're passing to it are useless in scripting context, you should be logging into the account and creating a session beforehand, which in that case they are ignored.

All the calls of client() must also be awaited.

Refer to the docs

answered Apr 21, 2025 at 0:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.