我如何发送投票?我正在尝试以下代码,但它没有返回错误并且未发送民意调查:
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)
是否有更好的方式通过电视马拉松提交民意调查?
最佳答案
要发送民意调查,您需要使用 raw API types 构造民意调查媒体对象发现于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)
)
))
实际值:
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')]
)
))
关于python - 在 telethon telegram bot 中发送民意调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59492527/