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?
2
Answers
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:With actual values:
This function creates a poll on quiz mode meaning there is only one correct answer, the functions gets the following arguments:
group which is the group id or the channel id () where you want to send the quiz. In order to obtain the group id / channel id, open telegram web and turn to the old version of telegram web click on the group/channel, in the url of the web page copy for example in the url -> "https://web.telegram.org/?legacy=1#/im?p=@IntiriorChannel" the IntiriorChannel part,
if it is private group copy the number in the url for example https://web.telegram.org/?legacy=1#/im?p=s1504616337_1547258548617074964 the id is 1504616337.
the answers argument suppose to be a list of possible answers(you can have between 1 to 10 answers) for instance ["10", "20", "30", "40", "50"].
the question argument is the question you want to be asked in the quiz
the correct_answer argument is the index+1 of the correct answer in the list of answers, for instance the correct answer is "10", then the correct_answer argument is 1, or if the answer is "40" then the correct_answer argument is 4.