Skip to main content

BY USING PYTHON TWEEPY MAKE A TWITTER BOT....


In this episode we are getting to create a twitter bot with python using the selenium library.


if you're a beginner python developer otherwise you are trying to find some python projects then this tutorial is for you!


Things covered in this tutorial:
!) Create a twitter bot with python
2)Python basics
3)How to use python selenium

To start, here’s how you'll use Tweepy to make a tweet saying Hello Tweepy:

PYTHON_____________________________________________
import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

# Create API object
api = tweepy.API(auth)

# Create a tweet
api.update_status("Hello Tweepy")
This is a brief example, but it shows the four steps common to all or any Tweepy programs:

Import the tweepy package
Set the authentication credentials
Create a replacement tweepy.API object
Use the api object to call the Twitter API
Objects belonging to the tweepy.API class offer a huge set of methods that you simply can use to access most Twitter functionality. within the code snippet, we used update_status() to make a replacement Tweet.

We will see later during this article how the authentication works and the way you'll create the specified authentication key, token, and secrets.

This is just a touch example of what you'll do with Tweepy. Through this text , you’ll find out how to create programs that interact with Twitter in far more interesting and sophisticated ways.

Twitter API:

.
The Twitter API gives developers access to most of Twitter’s functionality. you'll use the API to read and write information associated with Twitter entities like tweets, users, and trends.

Technically, the API exposes dozens of HTTP endpoints related to:

-Tweets
-Retweets
-Likes
-Direct messages
-Favorites
-Trends
-Media

           Tweepy, as we’ll see later, provides how to invoke those HTTP endpoints without handling low-level details.

The Twitter API uses OAuth, a widely used open authorization protocol, to authenticate all the requests. Before making any call to the Twitter API, you would like to make and configure your authentication credentials. Later during this article, you’ll find detailed instructions for this.

You can leverage the Twitter API to create different sorts of automations, like bots, analytics, and other tools. confine mind that Twitter imposes certain restrictions and policies about what you'll and can't build using its API. this is often done to ensure users an honest experience. the event of tools to spam, mislead users, then on is forbidden.

The Twitter API also imposes rate limits about how frequently you’re allowed to invoke API methods. If you exceed these limits, you’ll need to wait between 5 and quarter-hour to be ready to use the API again. you want to consider this while designing and implementing bots to avoid unnecessary waits.

You can find more information about the Twitter API’s policies and limits in its official documentation:

-Twitter Automation
-Rate limits

Source code
 #!/usr/bin/env python

# tweepy-bots/bots/autoreply.py

import tweepy
import logging
from config import create_api
import time

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

def check_mentions(api, keywords, since_id):
    logger.info("Retrieving mentions")
    new_since_id = since_id
    for tweet in tweepy.Cursor(api.mentions_timeline,
        since_id=since_id).items():
        new_since_id = max(tweet.id, new_since_id)
        if tweet.in_reply_to_status_id is not None:
            continue
        if any(keyword in tweet.text.lower() for keyword in keywords):
            logger.info(f"Answering to {tweet.user.name}")

            if not tweet.user.following:
                tweet.user.follow()

            api.update_status(
                status="Please reach us via DM",
                in_reply_to_status_id=tweet.id,
            )
    return new_since_id

def main():
    api = create_api()
    since_id = 1
    while True:
        since_id = check_mentions(api, ["help", "support"], since_id)
        logger.info("Waiting...")
        time.sleep(60)

if __name__ == "__main__":
    main()

Comments

Popular posts from this blog

Computer Vision: Algorithms and Applications

As humans,we perceive the three-dimensional structure of the planet around us with apparent ease. Think of how vivid the three-dimensional percept is once you check out a vase of flowers sitting on the table next to you. You can tell the form and translucency of every petal through the subtle patterns of sunshine and shading that play across its surface and effortlessly segment each flower from the background of the scene The forward models that we use in computer vision are usually developed in physics (radiometry, optics, and sensor design) and in computer graphics . Both of these fields model how objects move and animate, how light reflects off their surfaces, is scattered by the atmosphere, refracted through camera lenses (or human eyes), and finally projected onto a flat (or curved) image plane. While computer graphics aren't yet perfect (no fully computer animated movie with human characters has yet succeeded at crossing the uncanny valley2 that separates real humans from...

Twitter bot @real_human_vc is coming for your thought leaders

Spend enough time on Twitter and you’ll start to discern patterns in the scrolling chaos that feels a little like the internet’s id: the grudges, the feuds, the wet-brained political chatter, and the general flatulences tooted out by otherwise smart people. In other words: the state of play on the board. (As the editor Willy Staley might point out: there is a type of tweet for every type of guy.)                   While much of Twitter’s utility is derived from its function as an echo chamber — and while a lot of the fun comes from seeing just how large your favorite prominent person’s blindspots are — the best stuff on the site comes out of people using the platform as it’s intended to be used, which is to say, as a broadcasting tool. There is a category difference between the kinds of tweets cranked off into the internet’s roil of human emotion and the ones that are intended as Teachable Moments or Pearls of Wisdom. It’s in this la...

Python for Data Analysis By Wes McKinney

This book cares with the nuts and bolts of manipulating, processing, cleaning, and crunching data in Python. This Book is to supply a guide to the parts of the Python programming language and its data-oriented library ecosystem and tools which will equip you to become an efficient data analyst. While “data analysis” is within the title of the book, the main target is specifically on Python programming, libraries, and tools as against data analysis methodology. this is often the Python programming you would like for data analysis.0 Why Python for Data Analysis? For many people, the Python programming language has strong appeal. Since its introduction in 1991, Python has become one among the foremost popular interpreted programming languages, along side Perl, Ruby, etc. . Python and Ruby became especially popular since 2005 approximately for building websites using their numerous web frameworks, like Rails (Ruby) and Django (Python). Such languages are often called scripting lang...