DEV Community

Cover image for How to tweet via Python script?
Sonika Baniya
Sonika Baniya

Posted on

7 5

How to tweet via Python script?

What do you need to install?

  1. Python
  2. Tweepy : Tweepy is a python library for accessing Twitter API.
  3. Developer account in Twitter

Steps

  1. Create a test file name test.py in your working directory.

  2. Write the following here

import tweepy

# Consumer keys and access tokens from twitter developer console
CONSUMER_SECRET = '####'
CONSUMER_KEY = '###'
ACCESS_SECRET = '####'
ACCESS_KEY = '###'

# OAuth authentication connection from 
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

# Creation of the actual interface, using authentication
tweet = tweepy.API(auth)

# Write tweet here
tweet.update_status("Hi from API" )
Enter fullscreen mode Exit fullscreen mode

3.Run the following file by running python test.py command in your working directory and bam, you are done in 8 lines of code.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Great snippets. Thanks for sharing!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more