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!
Asyncio should be showing you a warning already, client.start() in async context must be await
ed, 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 await
ed.