bitcoin
bitcoin

$98917.207401 USD

5.80%

ethereum
ethereum

$3504.975738 USD

5.08%

tether
tether

$0.999394 USD

0.06%

xrp
xrp

$2.337858 USD

7.81%

bnb
bnb

$696.292934 USD

2.06%

solana
solana

$196.997512 USD

6.44%

dogecoin
dogecoin

$0.335701 USD

7.82%

usd-coin
usd-coin

$1.000089 USD

0.01%

cardano
cardano

$0.940245 USD

6.68%

tron
tron

$0.257484 USD

2.93%

avalanche
avalanche

$41.235479 USD

11.80%

chainlink
chainlink

$24.762324 USD

9.57%

toncoin
toncoin

$5.803528 USD

5.70%

shiba-inu
shiba-inu

$0.000023 USD

6.88%

sui
sui

$4.629948 USD

7.95%

加密貨幣新聞文章

使用 Python 建立具有即時價格更新的加密電報機器人

2024/04/17 12:01

在這份綜合指南中,我們開始使用 Python 創建 Telegram 機器人,為用戶提供即時加密更新。這個精心設計的機器人不僅提供任何所選加密貨幣的最新價格,還提供大量附加資料。深入研究本指南,探索它如何檢索和顯示關鍵的市場指標,例如 24 小時價格變化、市值、交易量和歷史加密貨幣價格數據。有了這些重要訊息,您就可以自信地探索加密貨幣領域並做出明智的決策。

使用 Python 建立具有即時價格更新的加密電報機器人

Building a Crypto Telegram Bot with Real-Time Price Updates Utilizing Python

使用 Python 建立具有即時價格更新的加密電報機器人

In the rapidly evolving world of cryptocurrencies, staying abreast of market fluctuations and asset performance is paramount. To address this need, we present a comprehensive guide on constructing a Telegram bot that empowers users with real-time cryptocurrency updates. This bot not only provides instant access to current prices but also offers a wealth of additional information, such as market capitalization, trading volume, and historical price data.

在快速發展的加密貨幣世界中,及時了解市場波動和資產表現至關重要。為了滿足這一需求,我們提供了有關構建 Telegram 機器人的綜合指南,該機器人為用戶提供即時加密貨幣更新。該機器人不僅可以即時存取當前價格,還可以提供大量附加信息,例如市值、交易量和歷史價格數據。

Functionality Overview

功能概述

Our Telegram Crypto Bot boasts an array of features designed to provide a comprehensive and real-time overview of the crypto market:

我們的 Telegram Crypto Bot 擁有一系列功能,旨在提供加密市場全面、即時的概覽:

  • Real-Time Crypto Market Data Retrieval: The bot seamlessly fetches and displays real-time market data for various cryptocurrencies, including current price, market cap, volume, and price change percentage.
  • 24h High/Low Prices: Users can access the highest and lowest prices of a specific cryptocurrency over the past 24 hours, a crucial piece of information for active traders and investors.
  • Supply Information: The bot retrieves and presents the circulating supply and total supply of a selected cryptocurrency, offering insights into its scarcity.
  • Top Cryptocurrencies: To provide a quick overview of the leading cryptocurrencies, the bot displays the top 10 coins based on market cap.
  • On-Chain DEX Pool Data: This advanced feature enables users to explore on-chain Decentralized Exchange (DEX) crypto liquidity pools, providing valuable insights into the DEX market.

Prerequisites

即時加密貨幣市場數據檢索:機器人無縫獲取並顯示各種加密貨幣的即時市場數據,包括當前價格、市值、交易量和價格變化百分比。 24 小時內特定加密貨幣的價格,這對於活躍交易者和投資者來說是至關重要的資訊。稀缺性。貨幣流動性池,提供有價值的資訊對 DEX 市場的洞察。

Before embarking on this project, ensure that the following prerequisites are met:

在開始此專案之前,請確保滿足以下先決條件:

  • Python 3.7 or higher installed on your system. Python is a versatile and beginner-friendly programming language that will serve as the foundation for our bot.
  • Telegram account. Telegram is a cloud-based instant messaging platform where our bot will reside.
  • Basic understanding of Python programming. While this guide will provide in-depth instructions, a foundational understanding of Python syntax and concepts will enhance your experience.
  • Text editor: Choose a text editor or Integrated Development Environment (IDE) of your choice, such as Visual Studio Code, Sublime Text, or Atom, to write your code.
  • CoinGecko API: We will leverage the CoinGecko API to fetch the necessary crypto market data. CoinGecko offers a free Demo plan, providing 30 calls per minute and a monthly cap of 10,000 calls. Register for a CoinGecko account and apply for the Demo plan to obtain your API key.

Step 1: Telegram Bot Setup

您的系統上安裝了 Python 3.7 或更高版本。 Python 是一種多功能且適合初學者的程式語言,將作為我們 bot.Telegram 帳戶的基礎。 Telegram 是一個基於雲端的即時通訊平台,我們的機器人將駐留在其中。雖然本指南將提供深入的說明,但對Python 語法和概念的基本了解將增強您的體驗。 、或 Atom,編寫您的程式碼。 CoinGecko提供免費的演示計劃,每分鐘提供30次調用,每月上限為10,000次調用。註冊 CoinGecko 帳戶並申請示範計畫以取得您的 API 金鑰。

  1. Create a new bot on Telegram by searching for 'BotFather' and following the prompts. Provide a name and username for your bot.
  2. Upon successful creation, you will receive a token, which serves as your bot's authentication key for Telegram API requests.

Step 2: Installing Python Libraries

透過搜尋「BotFather」並按照提示操作,在 Telegram 上建立一個新機器人。為您的機器人提供名稱和使用者名稱。

We will utilize two essential Python libraries:

我們將利用兩個重要的 Python 函式庫:

  1. python-telegram-bot: This library facilitates interaction with the Telegram API.
  2. requests: This library empowers HTTP requests to the CoinGecko API.

Install these libraries using pip:

python-telegram-bot:此程式庫促進與 Telegram API 的交互。

pip install python-telegram-bot requests

Step 3: Fetching Cryptocurrency Data

pip install python-telegram-bot requests第 3 步:取得加密貨幣數據

To access cryptocurrency data, we will employ the CoinGecko API, which provides comprehensive market insights beyond price information.

為了存取加密貨幣數據,我們將使用 CoinGecko API,它提供除價格資訊之外的全面市場洞察。

Step 4: Writing the Bot Code

第 4 步:編寫機器人程式碼

Now that our bot is set up and the libraries are installed, let's delve into the Python code:

現在我們的機器人已經設定完畢並安裝了函式庫,讓我們深入研究 Python 程式碼:

# Import necessary libraries

# 導入必要的庫

import telebot

進口遠端機器人

from telebot import types

從遠端機器人導入類型

import requests

導入請求

# Define your CoinGecko API key

# 定義您的 CoinGecko API 金鑰

cg_api_key = "YOUR_API_KEY_HERE"

cg_api_key = "您的 API_KEY_HERE"

# Define the main bot object

# 定義主要機器人對象

bot = telebot.TeleBot(cg_api_key)

機器人 = telebot.TeleBot(cg_api_key);

# Define command handlers

# 定義命令處理程序

@bot.message_handler(commands=['start'])

@bot.message_handler(命令=['開始'])

def start(message):

def開始(訊息):

# Welcome message

# 歡迎留言

bot.send_message(message.chat.id, "Welcome to the Crypto Telegram Bot! Type /help for a list of commands.")

bot.send_message(message.chat.id, "歡迎使用 Crypto Telegram 機器人!輸入 /help 取得指令清單。")

@bot.message_handler(commands=['help'])

@bot.message_handler(命令=['幫助'])

def help(message):

def 幫助(訊息):

# Help message

# 幫助訊息

bot.send_message(message.chat.id, "Available commands:\n/data [crypto-name]: Get current data for a specific cryptocurrency.\n/high_low [crypto-name]: Get highest and lowest prices of a specific cryptocurrency in the last 24 hours.\n/supply [crypto-name]: Get circulating and total supply of a specific cryptocurrency.\n/ranks: Get the top 10 cryptocurrencies.\n/search_pools [query] [network] [attributes]: Get on-chain DEX pool data.")

bot.send_message(message.chat.id, "可用指令:\n/data [crypto-name]: 取得特定加密貨幣的目前資料。\n/high_low [crypto-name]: 取得特定加密貨幣的最高和最低價格過去24 小時內的加密貨幣。 ] [網路] [屬性】:取得鏈上DEX池資料。

@bot.message_handler(commands=['data'])

@bot.message_handler(命令=['資料'])

def data(message):

定義數據(訊息):

# Get current data for a specific cryptocurrency

# 取得特定加密貨幣的當前數據

crypto_name = message.text.split()[1]

crypto_name = message.text.split()[1]

data = get_crypto_data(crypto_name)

資料 = get_crypto_data(crypto_name)

if data:

如果數據:

# Send the data to the user

# 傳送資料給用戶

bot.send_message(message.chat.id, f"Current data for {crypto_name}:\nPrice: {data['current_price']} USD\nPrice Change (24h): {data['price_change_percentage_24h']}\nMarket Cap: {data['market_cap']} USD\nVolume (24h): {data['total_volume']} USD")

bot.send_message(message.chat.id, f"{crypto_name} 的當前資料:\n價格: {data['current_price']} USD\n價格變化(24h): {data['price_change_percentage_24h']}\n市值: {data['market_cap']} 美元\n成交量(24 小時):{data['total_volume']} 美元")

else:

別的:

# Handle errors

# 處理錯誤

bot.send_message(message.chat.id, "Invalid cryptocurrency name. Please try again.")

bot.send_message(message.chat.id, "加密貨幣名稱無效。請重試。")

@bot.message_handler(commands=['high_low'])

@bot.message_handler(命令=['high_low'])

def high_low(message):

def high_low(訊息):

# Get highest and lowest prices of a specific cryptocurrency in the last 24 hours

# 取得特定加密貨幣在過去24小時內的最高和最低價格

crypto_name = message.text.split()[1]

crypto_name = message.text.split()[1]

data = get_high_low(crypto_name)

資料 = get_high_low(加密名稱)

if data:

如果數據:

# Send the data to the user

# 傳送資料給用戶

bot.send_message(message.chat.id, f"Highest and lowest prices of {crypto_name} in the last 24 hours:\nHighest Price: {data['high']} USD\nLowest Price: {data['low']} USD")

bot.send_message(message.chat.id, f"過去24 小時內{crypto_name} 的最高和最低價格:\n最高價格:{data['high']} USD\n最低價格:{data['low' ] } 美元”)

else:

別的:

# Handle errors

# 處理錯誤

bot.send_message(message.chat.id, "Invalid cryptocurrency name. Please try again.")

bot.send_message(message.chat.id, "加密貨幣名稱無效。請重試。")

@bot.message_handler(commands=['supply'])

@bot.message_handler(命令=['供應'])

def supply(message):

預設供應(訊息):

# Get circulating and total supply of a specific cryptocurrency

# 取得特定加密貨幣的流通量和總供應量

crypto_name = message.text.split()[1]

crypto_name = message.text.split()[1]

data = get_supply(crypto_name)

資料 = get_supply(加密名稱)

if data:

如果數據:

# Send the data to the user

# 傳送資料給用戶

bot.send_message(message.chat.id, f"Supply data for {crypto_name}:\nCirculating Supply: {data['circulating_supply']}\nTotal Supply: {data['total_supply']}")

bot.send_message(message.chat.id, f"為 {crypto_name} 提供資料:\n循環供應量:{data['circulate_supply']}\n總供應量:{data['total_supply']}")

else:

別的:

# Handle errors

# 處理錯誤

bot.send_message(message.chat.id, "Invalid cryptocurrency name. Please try again.")

bot.send_message(message.chat.id, "加密貨幣名稱無效。請重試。")

@bot.message_handler(commands=['ranks'])

@bot.message_handler(命令=['等級'])

def ranks(message):

def 排名(訊息):

# Get the top 10 cryptocurrencies

# 取得前 10 名的加密貨幣

data = get_top_cryptos()

資料 = get_top_cryptos()

if data:

如果數據:

# Send the data to the user

# 傳送資料給用戶

top_10 = "\n".join([f"{i + 1}. {crypto['name']}" for i, crypto in enumerate(data)])

top_10 = "\n".join([f"{i + 1}.{crypto['name']}" for i, crypto in enumerate(data)])

bot.send_message(message.chat.id, f"Top 10 Cryptocurrencies by Market Cap:\n{top_10}")

bot.send_message(message.chat.id, f"依市值排名前 10 位的加密貨幣:\n{top_10}")

else:

別的:

# Handle errors

# 處理錯誤

bot.send_message(message.chat.id, "Unable to fetch the top 10 cryptocurrencies. Please try again later.")

bot.send_message(message.chat.id, "無法取得前 10 名加密貨幣。請稍後重試。")

# Define functions to get data from CoinGecko API

# 定義函數以從 CoinGecko API 取得數據

def get_crypto_data(crypto_name):

def get_crypto_data(crypto_name):

# Get current data for a specific cryptocurrency

# 取得特定加密貨幣的當前數據

response = requests.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=" + crypto_name)

回應 = requests.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=" + crypto_name)

if response.status_code == 200:

如果回應.status_code == 200:

return response.json()[0]

返回response.json()[0]

else:

別的:

return None

返回無

def get_high_low(crypto_name):

def get_high_low(加密名稱):

# Get highest and lowest prices of a specific cryptocurrency in the last 24 hours

# 取得特定加密貨幣在過去24小時內的最高和最低價格

response = requests.get("https://api.coingecko.com/api/v3/coins/" + crypto_name + "/ohlc?vs_currency=usd&days=1")

回應 = requests.get("https://api.coingecko.com/api/v3/coins/" + crypto_name + "/ohlc?vs_currency=usd&days=1")

if response.status_code == 200:

如果回應.status_code == 200:

return response.json()[-1]

返回response.json()[-1]

else:

別的:

return None

返回無

def get_supply(crypto_name):

def get_supply(crypto_name):

# Get circulating and total supply of a specific cryptocurrency

# 取得特定加密貨幣的流通量和總供應量

response = requests.get("https://api.coingecko.com/api/v3/coins/" + crypto_name + "/market_chart?vs_currency=usd&days=1")

回應 = requests.get("https://api.coingecko.com/api/v3/coins/" + crypto_name + "/market_chart?vs_currency=usd&days=1")

if response.status_code == 200:

如果回應.status_code == 200:

return response.json()['total_supply_and_circulating_supply']

return response.json()['total_supply_and_circulate_supply']

else:

別的:

return None

返回無

def get_top_cryptos():

def get_top_cryptos():

# Get the top 10 cryptocurrencies

# 取得前 10 名的加密貨幣

response = requests.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=10")

回應 = requests.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=10")

免責聲明:info@kdj.com

The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!

If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.

2024年12月25日 其他文章發表於