Isn't it great to have a personal bot to alert you as soon as there is a huge drop in the price of Bitcoins and you get notified first. Moreover, after a certain level an alarm goes up depicting that its a golden opportunity to invest now.
Prerequisites:
2. Create a telegram bot using botfather
Let's get started:
1. Hardware Part:
Make basic Connections as shown in the figure:
2. Software part:
Login into linux or use PuTTY to login into Digital Ocean's VPS.
Create a file conf.py
using the command sudo nano
conf.py
. Write the following code:
"""Configurations for alert.py"""
bolt_api_key = "XXXX" # This is your Bolt Cloud API Key
device_id = "XXXX" # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = "@XXXX" # This is the channel ID of the created Telegram channel. Paste after @ symbol.
telegram_bot_id = "botXXXX" # This is the bot ID of the created Telegram Bot. Paste after bot text.
threshold = 700000 # Threshold beyond which the alert should be sent
Create another file alert.py
using command sudo nano
alert.py
. Write the following code:
import requests, time, json, conf
from boltiot import Bolt
mybolt = Bolt(conf.bolt_api_key, conf.device_id)
def get_bitcoin_price():
URL = "https://min-api.cryptocompare.com/" # REPLACE WITH CORRECT URL
response = requests.request("GET", URL)
response = json.loads(response.text)
current_price = response["INR"]
return current_price
def send_telegram_message(message):
"""Sends message via Telegram"""
url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
data = {
"chat_id": conf.telegram_chat_id,
"text": message
}
try:
response = requests.request(
"POST",
url,
params=data
)
print("This is the Telegram URL")
print(url)
print("This is the Telegram response")
print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
while True:
current_price = get_bitcoin_price()
if current_price <= conf.threshold:
print("Bitcoin has exceeded threshold")
message = "Alert! The current Bitcoin Price is " + str(current_price)
telegram_status = send_telegram_message(message)
response = mybolt.digitalWrite('0', 'HIGH')
print(response)
time.sleep(5)
response = mybolt.digitalWrite('0', 'LOW')
time.sleep(10)
What this program does is it triggers alarm and sends a telegram notification as soon as the price goes below the threshold.
Save the program and run it usingsudo python3
alert.py
3. Output
Comments