Build a Rasberry Pi Twitter Bot

by Apr 24, 2020Raspberry Pi projects

Getting started with the Twitter API

Twitter is a web service that provides an application programming interface (API), which means you can write software that communicates with the live Twitter service—perhaps to read tweets in real time or to automatically publish tweets.

The API is free to use, but you have to have a Twitter account and to register your application in order to get access to the API, but that’s easy enough.

Make Your Twitter Developer Account

Once you have a twitter account with phone number linked, it’s time to create a Twitter app. To make a Twitter app first go to the twitters developer site.

  1. click on “Create an app”. This will take you to an application form, where you have to fill in details. Make sure you fill in real information, and submit the application. It might take a few hours or a couple of days for your application to get approved.

2. After your account is reviewed, you can create an app. Give it a name, and describe your app. Make sure you enter a URL in the field. If you don’t have a website, just add a valid URL.

3.The app will not be created if you leave the URL block empty.  Also check the “Enable Sign in with Twitter” option. After the information is filled in, submit form, and you will be redirected to your application.

4.Here you will see three tabs, click on “Permissions” and select “Read, write and direct messages” and save it.

5. Next go to “Keys and tokens” and click on “Create Access Tokens” or “Regenerate Access Tokens”. You will notice some hash codes which we will use in our bot script. These are very important codes! DO NOT SHARE these with anyone, or they can gain access to your Twitter account!

Setting Up the Raspberry Pi

  1. With our API tokens handy we can now start setting up and writing our Raspberry Pi Twitter bot. The first thing we should do is make sure our Raspberry Pi is up to date by running the following commands on it.

2. With the Raspberry Pi now updated let’s install the tools we will need to set up our Raspberry Pi Twitter Bot. We can utilize the Python Library twython or tweepy to interact with Twitter’s API. Here we have made use of python library Tweepy. Visit Here to set up using twython.

To install Tweepy Python Library for twitter API we use pip command

Create a Twitter Bot with the Raspberry Pi

1.We start by importing the packages required for the code. The package, tweepy, is imported as tp which will be used to connect and control our Twitter account . OS and time are preinstalled packages.

2.Next, we have to add the credentials used to log in into our Twitter account. These credentials are nothing but tokens/keys we generate using the Twitter app.

In place of the hash you have to enter your keys. Replace the 'your-xxxx'with your account credentials. We saved the keys into variables so we can use them later to authenticate the Twitter login.
Now we have to authenticate our credentials so our bot can login to our account.

3.We use the authhandler function to validate the tokens. Next, we save the validation in a variable called, api. This is used for actions such tweeting/retweeting, liking, etc. Now we can start using the API to interact with our account.

4.We can use the me() function to fetch user data and print it to the terminal. The code, user.name and user.location, prints our username and location. Use this code to verify that the script is working, and the connection is established.

Example 1 : Write some code that will follow everyone that is following you

Example 2: Find Keywords & HashTags Using Twitter Bot

Let’s examine this code further in a quick code walk-through.

First we save some keywords. These are the words you want to look for in a tweet, here we used arduino, raspberrypi, esp8266 and learnrobotics. You can use any words you want. Tweepy will search for tweets with these words. In the next line we have a for loop where we search the tag in the function Cursor(). The result_type can either be “popular”, “recent” or “mixes”. The phrase lang=”en” will only search for tweets that are in English.

Furthermore, let’s look at the code, .items(). Here the item is empty but you can provide a parameter. (For example item(10)). The bot will only select 10 tweets with this code. If you leave it empty, it will make the search longer, and all the tweets with these keywords will be selected.

There is another try block inside the for loop. First, the tweet is “favorited” using the favorite() function. Then the retweet() function retweets the tweet on your account. Finally, sleep(60) pauses the loop for 60 seconds before selecting next tweet. You can increase or decrease the time in seconds. I recommend using a delay greater than 60 seconds because Twitter might disable your account for spamming.

Finally, there is an except blockThis block will throw an exception or warning if there is an error with the tweets. That’s all for the code, now you can save the file as tweetbot.py and run the code through terminal using the following command:

To stop the execution, just close the terminal window. Add a copy of code, above, that you can use as a starting point. Just enter your tokens/keys, and you are good to go.

Creating a multiplication Skill in Alexa using python

Written By Harshith

RELATED POSTS

Magic Wand using Raspberry Pi and OpenCV

Magic Wand using Raspberry Pi and OpenCV

What if I could do things with a swing of a wand? Practically impossible but with technology maybe not. In this tutorial, we will use a raspberry pi and OpenCV to try and recreate something similar to a wand. It will perform specific functions when it recognizes...

IBM Watson IoT Platform with Raspberry Pi

IBM Watson IoT Platform with Raspberry Pi

IBM Watson is one of the leading cloud computing platforms there is. It has almost all the features one could expect from a modern-day cloud platform, from machine learning support to IoT, even till edge computing. In this tutorial, we will use raspberry pi and an...

Home Automation With Telegram and Raspberry Pi

Home Automation With Telegram and Raspberry Pi

In this Article we will see how we can use Telegram for Home Automation. Telegram is an extremely popular multi-platform messaging service. Just like any other messaging platform, it is used to send messages and exchange photos, videos, stickers, audio, and files of...

Home Automation System with Raspberry Pi and Flask

Home Automation systems are becoming increasingly popular for the level of convenience they offer. Imagine sitting on your couch and turning on/off lights or fans in the room without having to get up. You could also control blinds or even door locks. This project is a...

Smart Alarm Clock with Raspberry Pi

Smart Alarm Clock with Raspberry Pi

Smart Alarms are very common today yet the good ones expensive. What if you wanted to make a smart alarm clock on your own using as little hardware and cost as possible? You may ask isn't the RPI costly, yes it is but, the only purpose of using an RPI is easy to net...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....