How can i send a poll? I am trying the following code but it returns no error and the poll is not sent:
from typing import Optional
from telethon.sync import TelegramClient
from telethon.tl.types import *
from telethon.tl.functions.messages import *
def _build_poll(question: str, *answers: str, closed: Optional[bool] = None,
id: int = 0) -> InputMediaPoll:
"""Build a poll object."""
return InputMediaPoll(Poll(
id=id, question=question, answers=[
PollAnswer(text=i, option=bytes([idx]))
for idx, i in enumerate(answers)
],
closed=closed
))
poll = _build_poll(f"Question", "Answer 1", "Answer 2", "Answer 3")
message = client.send_message(-325188743, file=poll)
Is there any better way to submit a poll with telethon?
To send polls you need to construct the poll media object with the raw API types found in https://tl.telethon.dev/.
In your case, you need an example of sending would be to send a InputMediaPoll
as the example shows:
await client.send_message('@username',file=types.InputMediaPoll(
poll=types.Poll(
id=..., # type: long (random id)
question=..., # type: string (the question)
answers=... # type: list of PollAnswer (up to 10 answers)
)
))
With actual values:
from telethon.tl.types import InputMediaPoll, Poll, PollAnswer
await client.send_message("telethonofftopic",file=InputMediaPoll(
poll=Poll(
id=53453159,
question="Is it 2020?",
answers=[PollAnswer('Yes', b'1'), PollAnswer('No', b'2')]
)
))