Some python code to make twitter bots ;)

Overview

How to set up a twitter bot using python's tweepy library

Create a twitter developer account and project

Make sure you are logged into your twitter account that you want to link to your bot

Steps:

  1. Go to Twitter developer platform
  2. Click on the "Sign up" button
  3. Fill out the form and then proceed to agree to sign away your rights to twitter lmao
  4. Verify Email
  5. Create App Name
  6. Get your keys and store them into a csv or however you would like (for this guide we use a csv file)

Here is how I have set up my csv file for keys:

Type Secret Key
key ***********
secret ***********
token ***********
access_token ***********
access_token_secret ***********

For clarification:

API Key = key

API Secret = secret

Bearer Token = token

Access Token = access_token

Access Token Secret = access_token_secret

Install needed python libraries

pip install tweepy pandas

Create a new folder for the bot, any media and your keys

mkdir tweepy-bots
cd tweepy-bots

Create a new file called keys.csv and put your keys in it as I have done above

touch keys.csv

Create a new file called twitterbot.py

touch twitterbot.py
  • Now we can start writing our code using whatever editor we want I use VS Code XD but for simplicity let's use nano in this guide.
nano twitterbot.py

First thing we need to do is import the libraries then set up our authentication

import tweepy
import pandas as pd
import os

here = os.path.dirname(os.path.abspath(__file__))

filename = os.path.join(here, 'twitter_api_keys.csv')

# Twitter API credentials
key_df = pd.read_csv(filename)
consumer_api_key = key_df[key_df.type == 'key']['Password'].values[0]
consumer_secret_key = key_df[key_df.type == 'secret']['Password'].values[0]
consumer_token = key_df[key_df.type == 'token']['Password'].values[0]
access_token_secret = key_df[key_df.type == 'access_token_secret']['Password'].values[0]
access_token = key_df[key_df.type == 'access_token']['Password'].values[0]

auth = tweepy.OAuthHandler(consumer_api_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

try:
    api.verify_credentials()
    print("Authentication OK")
except:
    print("Error during authentication")

Next we create the code to update status (aka tweeting)

  • Lets make a variable to hold our tweet's text
text = """How efficient is #Cardano ? 

Over 200 blocks on these two solar panels wired in series. 

14 watts to power the pool. 

Absolutely no reason to stake inside a datacenter. Thats called centralization. 

What do we want?  #TrueDecentralization and network resilience."""
  • Now we need to upload the media to twitter using the chunked uploader feature
media_filename = os.path.join(here, 'centered-star-forge.gif')

chunked_media = api.chunked_upload(filename=media_filename, media_category='tweet_gif')
  • Now we can update the status with the media and our text
update_status = api.update_status(status=text, media_ids=[chunked_media.media_id])

The last thing we need to do now is set up our cron job to run our code on our schedule

  • We will run the script once a day at 9am everyday

Open up a terminal and run the following command to check if you have cron jobs running:

crontab -l

If you don't have any cron jobs running, then you can add a new cron job by this command:

crontab -e

This will open a VIM based text editor for you to add your cron job to the cron job file.

We need to know our path for python package on our machine so we can run our code and you can find that by typing following command and then copy the path:

which python

you simply need to add the following line to the cron job file (use absolute paths):

0 9 * * * /User/wael/opt/anaconda3/bin/python /home/username/tweepy-bots/twitterbot.py

Now you can open terminal and run the following command to check if your cron job is running properly by checking your systems mail:

cd /var/mail

nano wael
Owner
Wael
Electronics and Software are what I do. Cardano Open-Source Developer and Stake Pool Operator.
Wael
Date Time Userbot With Python

DATE_TIME_USERBOT An Telegram Bot By @Pythone_3 Config Vars API_ID : Telegram API_ID, get it from my.telegram.org/apps API_HASH : Telegram API_ID, get

Sinzz-sinan-m 2 Oct 20, 2021
Find songs by lyrics.

LyricSearch Hi, welcome to LyricSearch - a simple (Yes), fast (Maybe), and powerful (Approach) lyric search engine. We support Three search methods to

Dicer_ 1 Dec 13, 2021
Discord heximals: More colors for your bot

DISCORD-HEXIMALS More colors for your bot ! Support : okimii#0434 COLORS ( 742 )

4 Feb 04, 2022
A smart tool to backup members πŸ“ˆ So you even after a raid/ ban you can easily restore them in seconds 🎲

πŸ€‘ Discord-backer πŸ€‘ A open-source Discord member backup and restore tool for your server. This can help you get all your members in 5 Seconds back af

John 29 Dec 21, 2022
Telegram Group Management Bot based on Pyrogram

Komi-San Telegram Group Management Bot based on Pyrogram More updates coming soon Support Group Open a Pull request if you wana contribute Example for

33 Nov 07, 2022
Support for Competitive Coding badges to add in Github readme or portfolio websites.

Support for Competitive Coding badges to add in Github readme or portfolio websites.

Akshat Aggarwal 2 Feb 14, 2022
Telegram bot implementing Lex Arcana using python-telegram-bot library.

Lex Arcana Telegram Bot πŸ€– Telegram bot implementing Lex Arcana using python-telegram-bot library. This bot was evaluated for the course "Computer Eng

NicolΓ² Sonnino 6 Jun 22, 2022
Simple Telegram Bot for generating BalckPearl BBCode Templates

blackpearl-bbcode-bot Simple Telegram Bot for generating BlackPearl BBCode Templates Written in Pyrogram Features - πŸŽ‰ IMDB Info fetching from files -

D. Luffy 5 Oct 19, 2022
A Simple Telegram Bot By @AsmSafone to Download Files From Mega.nz and Upload It to Telegram

MegaDL-Bot A Simple Telegram Bot By @AsmSafone to Download Files From Mega.nz and Upload It to Telegram Features No Login Required All Mega.nz File Li

SAF ONE 92 Dec 02, 2022
Enables you to execute scripts and perform API requests in MikroTik router

HomeAssistant component: MikroTik API The mikrotik_api platform enables you to execute scripts and perform API requests in MikroTik router To enable M

Pavel S 6 Aug 12, 2022
A Python AWS Lambda Webhook listener that generates a permanent URL when an asset is created in Contentstack.

Webhook Listener A Python Lambda Webhook Listener - Generates a permanent URL on created assets. See doc on Generating a Permanent URL: https://www.co

Contentstack Solutions 1 Nov 04, 2021
One of the best Telegram renamer bot with many new features

Renamer-Bot I think this repo gonna become one of the best renamer open source πŸ₯° . Please Give a ⭐ if you like this repo and also try following me fo

Ns Bots 97 Jan 06, 2023
Useful tools for building interactions in Python

discord-interactions-python Types and helper functions for Discord Interactions webhooks. Installation Available via pypi: pip install discord-interac

Discord 77 Dec 07, 2022
An unofficial API for lyricsfreak.com using django and django rest framework.

An unofficial API for lyricsfreak.com using django and django rest framework.

Hesam Norin 1 Feb 09, 2022
This asynchronous telegram bot sells books.

Selling_Books_Bot Description Say, you have a bunch of items you need no more and you want to sell it all out. That's where you're going to have to us

Roman 1 Oct 24, 2021
A repository of publicly verifiable token Sale contracts

Token-Sale-Plutus-Contract A repository of publicly verifiable token sale and royalty contracts. This will be the storage solution since it is easily

Logical Mechanism 29 Aug 18, 2022
A lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

24 July 2020 Actively soliciting contributers! Ping @ronncc if you would like to help out! pytube pytube is a very serious, lightweight, dependency-fr

pytube 7.9k Jan 02, 2023
A Python library to access Instagram's private API.

Instagram Private API A Python wrapper for the Instagram private API with no 3rd party dependencies. Supports both the app and web APIs. Overview I wr

2.6k Jan 05, 2023
An API wrapper library for opensea api.

Opensea API An API wrapper library for opensea api. Installation pip3 install opensea Usage Retrieving assets: from opensea import get_assets # This

Ankush Singh 38 Jul 17, 2022
THE BEST INSTAGRAM AUTO LIKER GET MORE FOLLOWERS WITH THIS AUTOMATION

Hi πŸ‘‹ , I'm Anandhu Ashok Developer making awesome things for awesome people πŸš€ Connect with me: THE BEST INSTAGRAM AUTO LIKER GET MORE FOLLOWERS WITH

Anandhu Ashok 3 Jul 26, 2022