×

使用Bolt IOT的带有蜂鸣器的电报比特币通知器

消耗积分:0 | 格式:zip | 大小:0.90 MB | 2022-12-19

王毅山

分享资料个

描述

一旦比特币价格大幅下跌并且您首先收到通知,拥有一个个人机器人来提醒您不是很好吗?此外,在达到一定水平后,警报会上升,表明现在是投资的黄金机会。

先决条件:

1.螺栓物联网课程

2.使用botfather创建一个电报机器人

3.比特币价格API

让我们开始吧:

1.硬件部分:

进行如图所示的基本连接:

 

poYBAGOYke-AIrAZAADP2NQK4Ls163.png
确保启动 Bolt 并连接到 WIFI。绿灯应亮。
 

 

2.软件部分:

登录 linux 或使用 PuTTY 登录 Digital Ocean 的 VPS。

conf.py使用命令创建文件sudo nano conf.py编写以下代码:

"""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

alert.py使用命令创建另一个文件sudo nano alert.py编写以下代码:

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)

该程序的作用是在价格低于阈值时触发警报并发送电报通知。

保存程序并使用sudo python3 alert.py

3.输出

 

pYYBAGOYkfOAJNJ6AAArQUI5R-o078.jpg
 

 

poYBAGOYkgaAOahfAAL0qpyrYJw346.jpg
 

 


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !