เช็คราคา BTC/USDT เมื่อราคาถึงกำหนดให้แจ้งเตือนผ่าน Line Notify Python

ติดตั้ง

ติดตั้งไลบราลี่

  • pip install ccxt
  • pip install requests
สมัครใช้งาน Line Notify ฟรี
  • https://notify-bot.line.me/th/
  • เข้าสู่ระบบไปสร้าง Token จากนั้นนำมาใช้กับโปรแกรม

Code

import ccxt
import time
import requests

exchange = ccxt.binance()
symbol = 'BTC/USDT'
threshold = 25000 #ราคาที่ต้องการให้เตือน
line_notify_token = 'ใส่โทเคน_line_notify_token'

# ทดสอบส่ง notify เพื่อดูว่าส่งไปจริงได้
message = "ทดสอบแจ้งเตือนเมื่อราคาถึง " + str(threshold)
headers = {
    "Authorization": "Bearer " + line_notify_token
}
payload = {'message': message}
r = requests.post("https://notify-api.line.me/api/notify", headers=headers, params=payload)
# ทดสอบส่ง notify เพื่อดูว่าส่งไปจริงได้

while True:
    ticker = exchange.fetch_ticker(symbol)
    price = ticker['last']

    if price >= threshold:
        message = "BTC price reached $25,000: " + str(price)
        headers = {
            "Authorization": "Bearer " + line_notify_token
        }
        payload = {'message': message}
        r = requests.post("https://notify-api.line.me/api/notify", headers=headers, params=payload)
        print("Notification sent to Line Notify")
        break
   
    time.sleep(60) # wait for 60 seconds before checking the price again

ทดสอบใช้งาน

ใส่ราคาที่ต้องการให้แจ้งเตือนที่ตัวแปร threshole จากนั้นนำ token line notify มาใส่ โดยเมื่อรันครั้งแรกโปรแกรมจะแจ้งเตือนเพื่อทดสอบว่าสามารถส่งข้อความมาหาได้ จากนั้นก็รอให้ราคาถึงจุดหรือมากกว่าที่กำหนด จะมีการส่งแจ้งเตือนเข้าทาง Line โดยตัวโปรแกรมจะมีการเช็คราคา ทุกๆ 1นาที
เมื่อราคาถึงจะมีแจ้งเตือนประมาณนี้


แสดงความคิดเห็น

0 ความคิดเห็น