DEV Community

Cover image for Creating a twitter bot to automatically post Bitcoin and Ethereum prices.
Tom Boyle
Tom Boyle

Posted on

Creating a twitter bot to automatically post Bitcoin and Ethereum prices.

I am currently working on a crypto-themed website and was looking to market the website on twitter. A lot of guides just tell you that you need to be active on the platform however I don’t want to be sitting liking tweets and commenting all day. I decided there had to be a way to automate this and reminded me of a first-year project I completed university where we used the Twitter API to post tweets on our behalf.

The goal of this project is to automatically tweet price signals combining the knowledge gained from my Twitter API project and the CoinGecko API. Firstly, there are a couple requirements for this project:

· Python (latest version). Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems.

· Tweepy. An easy-to-use Python library for accessing the Twitter API.

· Twitter account. Twitter is a microblogging and social networking service.

· Twitter developer account. This account allows you to use the Twitter API to analyze, learn from, and interact with Tweets, Direct Messages, and users.

Once you have set up each of the above requirements you will be ready to commence the project. Please see the source code below:

import tweepy
import requests
import json
import time

API_KEY = ""
SECRET_KEY = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

auth_handler = tweepy.OAuthHandler(consumer_key=API_KEY, consumer_secret=SECRET_KEY)
auth_handler.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth_handler, wait_on_rate_limit=True)

print('logged in')

while True:
    bitcoinprice = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
    price = bitcoinprice.json()
    btcusd = price['bitcoin']['usd']
    print(btcusd)

    message = "$BTC currently trading at: $" + str(btcusd) + " USD. #BTC"
    api.update_status(message)

    print('Tweet posted.')

    ethereumprice = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd')
    price = ethereumprice.json()
    ethusd = price['ethereum']['usd']
    print(ethusd)

    message = "$ETH currently trading at: $" + str(ethusd) + " USD. #ETH"
    api.update_status(message)

    print('Tweet posted.')

    time.sleep(6000)
Enter fullscreen mode Exit fullscreen mode

Feel free to follow me on github or twitter.

Top comments (0)