Overview
Polls allow you to create multiple-choice questions in tweets and collect votes from your followers. Twikit provides full support for creating polls, voting on them, and retrieving poll results.
Poll Class
Represents a poll attached to a tweet.
Attributes
The unique identifier of the poll.
The internal name of the poll card (e.g., poll2choice_text_only, poll4choice_text_only).
The Tweet object that contains this poll.
A list of poll choices. Each choice is a dictionary containing:
number (str): The choice number (“1”, “2”, etc.)
label (str): The text label for the choice
count (str): The number of votes for this choice
The duration of the poll in minutes.
The end date and time of the poll in UTC format.
last_updated_datetime_utc
The last updated date and time of the poll in UTC format.
Whether the vote counts are final (poll has ended).
The number of the choice you selected, if you’ve voted. None if you haven’t voted.
Methods
vote()
Vote on the poll with a specified choice.
updated_poll = await poll.vote( '1' )
The number of the choice to vote for (“1”, “2”, “3”, or “4”).
Returns: Poll - The updated Poll object with current vote counts.
Creating Polls
To create a poll, use the create_poll() method to generate a poll card URI, then attach it to a tweet.
Client.create_poll()
card_uri = await client.create_poll(choices, duration_minutes)
A list of choice labels for the poll. Must contain 2-4 choices.
The duration of the poll in minutes.
Returns: str - The poll card URI to attach to a tweet.
Once you have the poll URI, attach it when creating a tweet:
# Create the poll
choices = [ 'Option A' , 'Option B' , 'Option C' ]
duration_minutes = 1440 # 24 hours
card_uri = await client.create_poll(choices, duration_minutes)
# Create tweet with poll
tweet = await client.create_tweet(
text = 'What is your favorite option?' ,
poll_uri = card_uri
)
Voting on Polls
You can vote on polls in two ways:
Through the Poll Object
If you have a Tweet object with a poll:
tweet = await client.get_tweet_by_id( '1234567890' )
if tweet.poll:
# Vote for choice 1
updated_poll = await tweet.poll.vote( '1' )
# Check results
for choice in updated_poll.choices:
print ( f " { choice[ 'label' ] } : { choice[ 'count' ] } votes" )
Through the Client
You can also vote directly through the client:
updated_poll = await client.vote(
selected_choice = '2' ,
card_uri = poll.id,
tweet_id = tweet.id,
card_name = poll.name
)
The label or number of the choice to vote for.
The URI of the poll card (same as poll.id).
The ID of the tweet containing the poll.
The name of the poll card (from poll.name).
Usage Examples
Creating Polls with Different Durations
Short Poll (1 hour)
Standard Poll (24 hours)
Week-Long Poll
choices = [ 'Yes' , 'No' ]
duration_minutes = 60
card_uri = await client.create_poll(choices, duration_minutes)
tweet = await client.create_tweet(
text = 'Quick poll: Do you like this feature?' ,
poll_uri = card_uri
)
Checking Poll Results
tweet = await client.get_tweet_by_id( '1234567890' )
if tweet.poll:
poll = tweet.poll
print ( f "Poll: { tweet.text } " )
print ( f "Ends at: { poll.end_datetime_utc } " )
print ( f "Final: { poll.counts_are_final } " )
print ( " \n Results:" )
total_votes = sum ( int (choice[ 'count' ]) for choice in poll.choices)
for choice in poll.choices:
count = int (choice[ 'count' ])
percentage = (count / total_votes * 100 ) if total_votes > 0 else 0
print ( f " { choice[ 'label' ] } : { count } votes ( { percentage :.1f} %)" )
if poll.selected_choice:
print ( f " \n You voted for choice { poll.selected_choice } " )
Monitoring Your Own Poll
from twikit.streaming import Topic
# Create a poll
choices = [ 'Option A' , 'Option B' , 'Option C' , 'Option D' ]
card_uri = await client.create_poll(choices, 60 )
tweet = await client.create_tweet(
text = 'Vote now!' ,
poll_uri = card_uri
)
# Stream real-time engagement
topics = {Topic.tweet_engagement(tweet.id)}
session = await client.get_streaming_session(topics)
print ( "Monitoring poll votes..." )
async for topic, payload in session:
if payload.tweet_engagement:
# Fetch updated poll data
updated_tweet = await client.get_tweet_by_id(tweet.id)
if updated_tweet.poll:
for choice in updated_tweet.poll.choices:
print ( f " { choice[ 'label' ] } : { choice[ 'count' ] } votes" )
Automated Voting
# Search for polls on a topic
tweets = await client.search_tweet( 'python poll' , 'Latest' )
for tweet in tweets:
if tweet.poll and not tweet.poll.selected_choice:
# Haven't voted yet
print ( f "Found poll: { tweet.text } " )
# Vote for the first option
if len (tweet.poll.choices) > 0 :
updated_poll = await tweet.poll.vote( '1' )
print ( f "Voted for: { updated_poll.choices[ 0 ][ 'label' ] } " )
Poll Statistics
def analyze_poll ( poll ):
"""Analyze poll results and return statistics."""
total_votes = sum ( int (choice[ 'count' ]) for choice in poll.choices)
results = []
for choice in poll.choices:
count = int (choice[ 'count' ])
percentage = (count / total_votes * 100 ) if total_votes > 0 else 0
results.append({
'label' : choice[ 'label' ],
'votes' : count,
'percentage' : percentage
})
# Find winner
winner = max (results, key = lambda x : x[ 'votes' ])
return {
'total_votes' : total_votes,
'results' : results,
'winner' : winner,
'is_final' : poll.counts_are_final
}
tweet = await client.get_tweet_by_id( '1234567890' )
if tweet.poll:
stats = analyze_poll(tweet.poll)
print ( f "Total votes: { stats[ 'total_votes' ] } " )
print ( f "Winner: { stats[ 'winner' ][ 'label' ] } ( { stats[ 'winner' ][ 'percentage' ] :.1f} %)" )
Notes
Polls must have between 2 and 4 choices.
Poll duration can range from 5 minutes to 7 days (10,080 minutes).
Once you vote on a poll, you cannot change your vote or vote again.
The vote() method requires authentication. Make sure your client is logged in.