Discord Python Bot Tutorial to Check Bitcoin Price

This tutorial explains how to create a Discord bot by creating a webhook and posting messages to it using Python.

Create the Webhook

First you have to create a webhook. You create the webhook from within the Discord app. Open the server settings for a server that you manage. You must be an admin on a server to create webhooks.



In the server settings menu, go to the webhooks tab and create a new webhook.



On the "Edit Webhook" menu, configure the options as desired. Give it a name, upload an avatar, and choose the channel you want it to post to. Copy the webhook URL as you will need it later.

Create the Python Script

Now create a new Python file.

# discord_bitcoin_price_bot.py
import requests  # pip install requests

# Provide the webhook URL that Discord generated
discord_webhook_url = 'https://discordapp.com/api/webhooks/<webhookid>/<tokenid>'

# Get the BTC price from CoinDesk
bitcoin_price_url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
data = requests.get(bitcoin_price_url).json()
price_in_usd = data['bpi']['USD']['rate']

# Post the message to the Discord webhook
data = {
    "content": "Bitcoin price is currently at $" + price_in_usd + " USD"
}
requests.post(discord_webhook_url, data=data)
Execute the Script
python discord_bitcoin_price_bot.py

That is it! All you have to do is run the script and it will make an HTTP POST request to your webhook URL which will post a message to Discord. Whenever you run this Python script it will post the current Bitcoin price to the Discord channel. If you want to run this script automatically at certain time intervals, consider setting it up as a cron job.

Also check out the other options available at Discord "Execute Webhook" Reference. You have the option to override the username and avatar image as well as upload a file attachment.

Questions?Want to chat about bots? Come hang out on the DevDungeon Discord server. https://discord.gg/JWsSHJC.