在 Telethon 电报机器人中发送投票

Leo*_*ues 4 python telegram telegram-bot telethon

我如何发送投票?我正在尝试以下代码,但它没有返回错误并且不发送轮询:

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)
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来通过 Telethon 提交民意调查?

pai*_*nor 5

送你需要构建投票媒体与对象民调原始API类型中发现的https://tl.telethon.dev/

在您的情况下,您需要一个发送示例,InputMediaPoll如示例所示:

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)
    )
))
Run Code Online (Sandbox Code Playgroud)

使用实际值:

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')]
    )
))
Run Code Online (Sandbox Code Playgroud)