skip to Main Content

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


  1. 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')]
        )
    ))
    
    Login or Signup to reply.
  2. 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.

    from telethon.tl.types import Poll, PollAnswer, PollAnswerVoters,PollResults, MessageMediaPoll 
    from telethon import TelegramClient 
    
    
    client = TelegramClient("SessionBot", "api_id", "api_hash").start(bot_token="bot token")
    
    async def build_quiz_poll(group: str, answers: list, question:str, correct_answer: int) -> None:
        """ Create poll in quiz mode """
    
        await client.send_message(group, file=MessageMediaPoll(
            poll=Poll(
                id=random.randint(0, 100_000),
                question=question,
                answers=[PollAnswer(text=option, option=bytes([i])) for i, option in
                         enumerate(answers, start=1)],
                quiz=True
            )
            results=PollResults(
                results=[PollAnswerVoters(option=bytes([correct_answer]), 
    voters=200_000, correct=True)],
            )
        ))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search