python - 在 telethon telegram bot 中发送民意调查

标签 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)

是否有更好的方式通过电视马拉松提交民意调查?

最佳答案

要发送民意调查,您需要使用 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/

相关文章:

python - Telegram 机器人无法在回复键盘后发送消息

Telegram bot api - 获取组中的所有消息

python - 在 Python 2.7 "The character encoding of the plain text document was not declared. "中获取错误

Python - 将浮点算术作为字符串

markdown - 在 Telegram 中,我想要项目符号、编号和缩进,但它不起作用

python - Telegram API 无法使用用户名 - 400。错误请求 : chat not found

python - 在数字周围裁剪可能位于矩形区域内任何地方的正方形区域

Python IMAP : =? utf-8?问?在主题字符串中

Telegram-bot 用户控制

python - 在 Bot 中获取用户的输入