民意调查
Telegram 允许发送投票和测验,成千上万甚至数百万用户可以在聊天和频道中进行投票。
发送投票
pollAnswer#ff16e2ca text:TextWithEntities option:bytes = PollAnswer;poll#58747131 id:long flags:# closed:flags.0?true public_voters:flags.1?true multiple_choice:flags.2?true quiz:flags.3?true question:TextWithEntities answers:Vector<PollAnswer> close_period:flags.4?int close_date:flags.5?int = Poll;inputMediaPoll#f94e5f1 flags:# poll:Poll correct_answers:flags.0?Vector<bytes> solution:flags.1?string solution_entities:flags.1?Vector<MessageEntity> = InputMedia;---functions---messages.sendMedia#ac55d9c1 flags:# silent:flags.5?true background:flags.6?true clear_draft:flags.7?true noforwards:flags.14?true update_stickersets_order:flags.15?true invert_media:flags.16?true allow_paid_floodskip:flags.19?true peer:InputPeer reply_to:flags.0?InputReplyTo media:InputMedia message:string random_id:long reply_markup:flags.2?ReplyMarkup entities:flags.3?Vector<MessageEntity> schedule_date:flags.10?int send_as:flags.13?InputPeer quick_reply_shortcut:flags.17?InputQuickReplyShortcut effect:flags.18?long allow_paid_stars:flags.21?long suggested_post:flags.22?SuggestedPost = Updates;
要在聊天中发送投票,请调用messages.sendMedia,并提供inputMediaPoll:
-
poll是实际的投票构造函数,包含:
- question- 投票标题,也称为投票名称
- answers- 一个包含可能答案的向量(2-poll_answers_max),每个答案都有一个可见的标题text和一个唯一的option标识符(1-100 字节)
- closed- 投票是否已结束
- public_voters- 投票结果是否对所有用户公开可见(非匿名投票)
- multiple_choice- 是否可以选择多个选项作为答案
- quiz- 这是否是一个带有正确答案 ID 的测验inputMediaPoll.correct_answers
- close_period- 投票创建后持续有效时间(秒),5-600。不能与close_date.
-
close_date- 轮询自动关闭的时间点(Unix 时间戳)。必须至少为 5 秒,且不超过 600 秒;不能与 `.` 一起使用close_period。
最后两个参数完全相同,区别在于一个使用绝对 Unix 时间戳,另一个使用相对 Unix 时间戳。
-
correct_answers- 对于测验,唯一正确答案的选项 ID
-
solution- 当用户在问答式投票中选择错误答案或点击灯泡图标时显示的文本,长度为 0-200 个字符,最多可包含 2 个换行符。
-
solution_entities-用于解释的样式化文本消息实体solution
为了提前结束投票,防止进一步投票,请使用messages.editMessage,并将poll.closed标志设置为 true。
投票
pollAnswerVoters#3b6ddad2 flags:# chosen:flags.0?true correct:flags.1?true option:bytes voters:int = PollAnswerVoters;pollResults#7adf2420 flags:# min:flags.0?true results:flags.1?Vector<PollAnswerVoters> total_voters:flags.2?int recent_voters:flags.3?Vector<Peer> solution:flags.4?string solution_entities:flags.4?Vector<MessageEntity> = PollResults;poll#58747131 id:long flags:# closed:flags.0?true public_voters:flags.1?true multiple_choice:flags.2?true quiz:flags.3?true question:TextWithEntities answers:Vector<PollAnswer> close_period:flags.4?int close_date:flags.5?int = Poll;messageMediaPoll#4bd6e798 poll:Poll results:PollResults = MessageMedia;updateMessagePoll#aca1657b flags:# poll_id:long poll:flags.0?Poll results:PollResults = Update;---functions---messages.sendVote#10ea6184 peer:InputPeer msg_id:int options:Vector<bytes> = Updates;
当收到包含messageMediaPoll 的消息时,用户可以使用messages.sendVote并指定所选标识符进行投票。option
该方法将返回一个updateMessagePoll,其中包含一个更新后的pollResults构造函数,其中chosen设置了我们选择的选项的标志,并correct设置了正确答案的标志。
获得民意调查选票
pollAnswerVoters#3b6ddad2 flags:# chosen:flags.0?true correct:flags.1?true option:bytes voters:int = PollAnswerVoters;pollResults#7adf2420 flags:# min:flags.0?true results:flags.1?Vector<PollAnswerVoters> total_voters:flags.2?int recent_voters:flags.3?Vector<Peer> solution:flags.4?string solution_entities:flags.4?Vector<MessageEntity> = PollResults;updateMessagePoll#aca1657b flags:# poll_id:long poll:flags.0?Poll results:PollResults = Update;---functions---messages.getPollResults#73bb643b peer:InputPeer msg_id:int = Updates;
通常情况下,如果新用户在用户可参与的投票中投了票,他们将收到一条updateMessagePoll,其中包含更新后的pollResults。
也可以使用messages.getPollResults手动获取相同的构造函数。
在非匿名民意调查中获得投票者
messagePeerVote#b6cc2d5c peer:Peer option:bytes date:int = MessagePeerVote; messagePeerVoteInputOption#74cda504 peer:Peer date:int = MessagePeerVote; messagePeerVoteMultiple#4628f6e6 peer:Peer options:Vector<bytes> date:int = MessagePeerVote;messages.votesList#4899484e flags:# count:int votes:Vector<MessagePeerVote> chats:Vector<Chat> users:Vector<User> next_offset:flags.0?string = messages.VotesList;updateMessagePollVote#24f40e77 poll_id:long peer:Peer options:Vector<bytes> qts:int = Update;---functions---messages.getPollVotes#b86e380e flags:# peer:InputPeer id:int option:flags.0?bytes offset:flags.1?string limit:int = messages.VotesList;
messages.getPollVotes可用于获取非匿名投票的结果,查看每个用户对每个投票选项的投票情况。每当用户在非匿名投票中做出选择时,
机器人都会收到updateMessagePollVote 。机器人仅接收由其自身发起的投票的新投票。