![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
在这份综合指南中,我们开始使用 Python 创建 Telegram 机器人,为用户提供实时加密更新。这个精心设计的机器人不仅提供任何所选加密货币的最新价格,还提供大量附加数据。深入研究本指南,探索它如何检索和显示关键的市场指标,例如 24 小时价格变化、市值、交易量和历史加密货币价格数据。有了这些重要信息,您就可以自信地探索加密货币领域并做出明智的决策。
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小时高/低价格:用户可以访问最高和最低价格过去 24 小时内特定加密货币的价格,这对于活跃交易者和投资者来说是至关重要的信息。 供应信息:机器人检索并显示所选加密货币的流通供应量和总供应量,从而深入了解其稀缺性。 热门加密货币:为了快速概述领先的加密货币,机器人会根据市值显示排名前 10 的代币。 链上 DEX 池数据:此高级功能使用户能够探索链上去中心化交易所 (DEX) 加密货币流动性池,提供有价值的信息对 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 编程有基本的了解。虽然本指南将提供深入的说明,但对 Python 语法和概念的基本了解将增强您的体验。 文本编辑器:选择您选择的文本编辑器或集成开发环境 (IDE),例如 Visual Studio Code、Sublime Text、或 Atom,编写您的代码。CoinGecko API:我们将利用 CoinGecko API 来获取必要的加密市场数据。 CoinGecko提供免费的演示计划,每分钟提供30次调用,每月上限为10,000次调用。注册 CoinGecko 帐户并申请演示计划以获取您的 API 密钥。第 1 步:Telegram Bot 设置
- Create a new bot on Telegram by searching for 'BotFather' and following the prompts. Provide a name and username for your bot.
- 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 上创建一个新机器人。为您的机器人提供名称和用户名。成功创建后,您将收到一个令牌,该令牌用作您的机器人用于 Telegram API 请求的身份验证密钥。第 2 步:安装 Python 库
We will utilize two essential Python libraries:
我们将利用两个重要的 Python 库:
python-telegram-bot
: This library facilitates interaction with the Telegram API.requests
: This library empowers HTTP requests to the CoinGecko API.
Install these libraries using pip:
python-telegram-bot:该库促进与 Telegram API 的交互。requests:该库支持对 CoinGecko API 的 HTTP 请求。使用 pip 安装这些库:
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 小时内的加密货币。\n/supply [加密货币名称]:获取特定加密货币的流通量和总供应量。\n/ranks:获取排名前 10 的加密货币。\n/search_pools [查询] [网络] [属性】:获取链上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
所提供的信息并非交易建议。根据本文提供的信息进行的任何投资,kdj.com不承担任何责任。加密货币具有高波动性,强烈建议您深入研究后,谨慎投资!
如您认为本网站上使用的内容侵犯了您的版权,请立即联系我们(info@kdj.com),我们将及时删除。
-
- 连锁链接(链接)价格显示TD顺序购买信号
- 2025-02-28 15:45:33
- 分析师阿里·马丁内斯(Ali Martinez)在TD顺序指标上确定了一个购买机会,该指标通常使用该指标来找到潜在的市场逆转点。
-
-
-
-
-
-
-
-
- REXAS Finance(RXS):以太坊投资者的最佳代价旋转新机遇
- 2025-02-28 15:05:33
- 作为以太坊的最佳替代品,Rexas Finance(RXS)吸引了机构和零售投资者的兴趣。