# Telegram Bot-crypto currencies notinfication AST.
## What is API:

api,Application Programming Interface,應用程式服務接口,可把他想成一個網址,透過Headers或Http params傳遞api token,即可取得希望得到的資料
==**注意**:API權限小者僅讀取資料,大者可修改帳戶資訊,若使用版本控制軟體務必注意不可將token發布至網上,可採取使用os.environment方法將toke藏起來==
## CMC API Documents
由於此project需要取得crypto currencies的價格資料,故選擇由CoinMartketCap的API,
[文件在此](https://coinmarketcap.com/api/documentation/v1/)
## STEP 1.設想程式架構
<iframe style="border:none" width="800" height="450" src="https://whimsical.com/embed/66v582LQmhngBQoYMdL7Hf"></iframe>
## STEP 2.API
由於BASIC PLAN有API request limit,一開始先使用sandbox-api,知道如何使用params後,再使用正式版本
發起api時可以使用*postman*來發起request,圖形化介面可使測試效率增高不少、且收到的json亦為beautufied
由Docs得知:API可取得8種不同資料(~~槓掉者為需付錢升級者,本文不說明~~)
|Category |Description |
|:---|:---|
|/cryptocurrency/|Endpoints that return data around cryptocurrencies such as ordered cryptocurrency lists or price and volume data.|
|/exchange/|Endpoints that return data around cryptocurrency exchanges such as ordered exchange lists and market pair data.|
|/global-metrics/|Endpoints that return aggregate market data such as global market cap and BTC dominance.|
|/tools/|Useful utilities such as cryptocurrency and fiat price conversions.|
|/blockchain/|Endpoints that return block explorer related data for blockchains.|
|/fiat/|Endpoints that return data around fiats currencies including mapping to CMC IDs.|
|/partners/|Endpoints for convenient access to 3rd party crypto data.|
|/key/|API key administration endpoints to review and manage your usage.|
我們要取得的為即時、指定幣種的報價,可擇定endpoint為Quotes Latest(v1/cryptocurrency/quotes/latest)
> Returns the latest market quote for 1 or more cryptocurrencies. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
若要取得全部交易幣種,則選擇endpoint為Listings Latest(v1/cryptocurrency/listings/latest)
> Returns a paginated list of all active cryptocurrencies with latest market data. The default "market_cap" sort returns cryptocurrency in order of CoinMarketCap's market cap rank (as outlined in our methodology) but you may configure this call to order by another market ranking field. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
由技術文件可得知,其params可填入:
- id(CMC設定的幣種id,如Bitcoin為1,逗號分格)
- slug(交易對,如"bitcoin,ethereum",逗號分格)
- symbol(幣種代號,如ETH,逗號分格)
- convert(Optionally calculate market quotes in up to 120 currencies at once by passing a comma-separated list of cryptocurrency or fiat currency symbols. Each additional convert option beyond the first requires an additional call credit. A list of supported fiat options can be found here. Each conversion is returned in its own "quote" object.)
- conver_id(Optionally calculate market quotes by CoinMarketCap ID instead of symbol. This option is identical to convert outside of ID format. Ex: convert_id=1,2781 would replace convert=BTC,USD in your query. This parameter cannot be used when convert is used.)
- aux選擇回傳哪些幣別資料:(預設為"num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply,is_active,is_fiat"
- num_market_pairs交易對數量
- cmc_rank:CMC排名
- date_added:幣別上線日期
- tags:標籤
- platform:平台
- max_supply:最大供給量
- circulating_supply:流通量
- total_supply:目前供給量
- market_cap_by_total_supply:由目前供給量之排名
- volume_24h_reported:24小時volume
- volume_7d:7天volume
- volume_7d_reported
- volume_30d:30天volume
- volume_30d_reported
- is_active
- is_fiat:是否為法幣掛勾幣
- skip_unvalid()
Postman新增request,網址填入
```
https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
```
params照下圖填入:(symbols是我自己選擇的幣種)

Headers一樣填入:(請輸入自己的API token)

輸完按send等待回傳資料:
```json=
{
"status": {
"timestamp": "2021-08-01T08:08:15.490Z",
"error_code": 0,
"error_message": null,
"elapsed": 12,
"credit_count": 1,
"notice": null
},
"data": {
"BTC": {
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"last_updated": "2021-08-01T08:07:03.000Z",
"quote": {
"USD": {
"price": 41765.00787217328,
"volume_24h": 24198423541.28948,
"volume_7d_reported": 338154737492.6884,
"percent_change_1h": -0.04557814,
"percent_change_24h": 0.19207076,
"percent_change_7d": 20.092804,
"percent_change_30d": 25.2925806,
"percent_change_60d": 12.26239284,
"percent_change_90d": -28.97017903,
"market_cap": 784051861588.813,
"last_updated": "2021-08-01T08:07:03.000Z"
}
}
},
"CRO": {
"id": 3635,
"name": "Crypto.com Coin",
"symbol": "CRO",
"slug": "crypto-com-coin",
"last_updated": "2021-08-01T08:06:34.000Z",
"quote": {
"USD": {
"price": 0.12945488581611,
"volume_24h": 25180729.88229944,
"volume_7d_reported": 507959177.4749,
"percent_change_1h": -0.72152929,
"percent_change_24h": 1.27307844,
"percent_change_7d": 11.43783414,
"percent_change_30d": 16.34470228,
"percent_change_60d": 2.70054635,
"percent_change_90d": -33.01656271,
"market_cap": 3270420552.868684,
"last_updated": "2021-08-01T08:06:34.000Z"
}
}
},
"ETH": {
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"slug": "ethereum",
"last_updated": "2021-08-01T08:07:03.000Z",
"quote": {
"USD": {
"price": 2594.8412223085593,
"volume_24h": 19448559646.836643,
"volume_7d_reported": 155922804455.1647,
"percent_change_1h": -0.4341627,
"percent_change_24h": 5.77602564,
"percent_change_7d": 19.03801353,
"percent_change_30d": 26.11965164,
"percent_change_60d": -3.20610367,
"percent_change_90d": -17.38091707,
"market_cap": 303399558566.71796,
"last_updated": "2021-08-01T08:07:03.000Z"
}
}
},
"MATIC": {
"id": 3890,
"name": "Polygon",
"symbol": "MATIC",
"slug": "polygon",
"last_updated": "2021-08-01T08:06:44.000Z",
"quote": {
"USD": {
"price": 1.10806770966065,
"volume_24h": 742415215.4549811,
"volume_7d_reported": 10711734537.6373,
"percent_change_1h": 0.02167591,
"percent_change_24h": 6.25295874,
"percent_change_7d": 16.54687818,
"percent_change_30d": 4.36661486,
"percent_change_60d": -38.888439,
"percent_change_90d": 30.70351966,
"market_cap": 7119042287.513054,
"last_updated": "2021-08-01T08:06:44.000Z"
}
}
},
"MATICBULL": {
"id": 6085,
"name": "3X Long Matic Token",
"symbol": "MATICBULL",
"slug": "3x-long-matic-token",
"last_updated": "2021-08-01T08:06:44.000Z",
"quote": {
"USD": {
"price": 0.8555,
"volume_24h": 799462.26835,
"volume_7d_reported": 5634501.3749,
"percent_change_1h": 0,
"percent_change_24h": 19.44153578,
"percent_change_7d": 53.72866128,
"percent_change_30d": -18.05555556,
"percent_change_60d": -92.22272727,
"percent_change_90d": -97.43555156,
"market_cap": 0,
"last_updated": "2021-08-01T08:06:44.000Z"
}
}
},
"VET": {
"id": 3077,
"name": "VeChain",
"symbol": "VET",
"slug": "vechain",
"last_updated": "2021-08-01T08:07:07.000Z",
"quote": {
"USD": {
"price": 0.09154435758614,
"volume_24h": 619420990.9924796,
"volume_7d_reported": 3140462954.3572,
"percent_change_1h": 0.19664004,
"percent_change_24h": 4.69621909,
"percent_change_7d": 26.07932512,
"percent_change_30d": 8.49540929,
"percent_change_60d": -26.25718306,
"percent_change_90d": -58.21160733,
"market_cap": 5887728178.239934,
"last_updated": "2021-08-01T08:07:07.000Z"
}
}
},
"XRP": {
"id": 52,
"name": "XRP",
"symbol": "XRP",
"slug": "xrp",
"last_updated": "2021-08-01T08:07:05.000Z",
"quote": {
"USD": {
"price": 0.76948930210237,
"volume_24h": 2662631333.7575765,
"volume_7d_reported": 22495739357.796,
"percent_change_1h": -0.83602159,
"percent_change_24h": 3.3747166,
"percent_change_7d": 26.24778474,
"percent_change_30d": 18.68734307,
"percent_change_60d": -24.70828115,
"percent_change_90d": -52.22900639,
"market_cap": 35636929719.741936,
"last_updated": "2021-08-01T08:07:05.000Z"
}
}
}
}
}
```
以上,完成資料的取得,接著要來進行JSON字串的處理
## STEP 3. 把request改由python送出
:star2: 隱藏API方法,新增一個config.py,藉import方式載入
```python=
# config.py
CMC_API='xxxxx-xxxx....'
```
main.py:
```python=
# main.py
import config
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
parameters = {
'symbol':'BTC,ETH,XRP,MATIC,MATICBULL,CRO,VET',
'aux':'volume_7d_reported'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': config.CMC_api,
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
```