gpt4 book ai didi

node.js - 如何将 coinmarketcap API 中的值添加到电报消息中?

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:58 24 4
gpt4 key购买 nike

我正在尝试完成电报机器人,它会在几个命令后以消息响应......失去了尝试我可以单独解决这个问题的任何希望。那些带有预定义消息的命令已经完成,并且工作起来就像一个魅力。但现在我陷入了/price 命令,该命令应该在来自 coinmarket API 响应的消息中显示硬币值(value)。

我尝试了很多变体,但以下结果总是调用 API 调用错误:或类似 [object Object] 的消息..

     ALQO: $0.0443407142 | 9.73% 🙂
ETH: 0.000313592 | 10.14% 🙂
BTC: 0.0000107949 | 9.5% 🙂
Cap: $2,545,718

上面的文字是来自机器人的正确响应...不幸的是,使用 CMC 的免费 API,我只能用美元定价,所以正确的答案应该是

       Coinname: Price | Change%
Cap: Marketcap

我的/price 命令代码

    //This is /price command code
'use strict';

const Telegram = require('telegram-node-bot');

const rp = require('request-promise');
const requestOptions = {
method: 'GET',
uri: 'https://pro-
api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?
id=3501&convert=USD',
headers: {
'X-CMC_PRO_API_KEY': 'MYFREEAPIKEYFROMCMC'
},
json: true,
gzip: true
};

rp(requestOptions).then(response => {
console.log('API call response:', response['data'][3501]);
}).catch((err) => {
console.log('API call error:', err.message);
});

class PriceController extends Telegram.TelegramBaseController {
PriceHandler($) {
rp(requestOptions).then(response => {
console.log('API call response:', response['data'][3501]);
$.sendMessage('Cryptosoul: price', response['data']['USD']['price']
[3501]);
}).catch((err) => {
$.sendMessage('API call error:', err.message);
});
}

get routes() {
return {
'priceCommand': 'PriceHandler'
};
};
}

module.exports = PriceController;

在 Node index.js之后从API响应(打开机器人,(来自Visual Studio终端的消息)

     API call response: { id: 3501,
name: 'CryptoSoul',
symbol: 'SOUL',
slug: 'cryptosoul',
circulating_supply: 143362580.31,
total_supply: 499280500,
max_supply: null,
date_added: '2018-10-25T00:00:00.000Z',
num_market_pairs: 3,
tags: [],
platform:
{ id: 1027,
name: 'Ethereum',
symbol: 'ETH',
slug: 'ethereum',
token_address: '0xbb1f24c0c1554b9990222f036b0aad6ee4caec29' },
cmc_rank: 1194,
last_updated: '2019-04-01T23:03:07.000Z',
quote:
{ USD:
{ price: 0.000188038816143,
volume_24h: 11691.5261174775,
percent_change_1h: 0.29247,
percent_change_24h: 0.0222015,
percent_change_7d: 4.69888,
market_cap: 26957.72988069816,
last_updated: '2019-04-01T23:03:07.000Z' } } }

触发/price 命令后出现的消息“API调用错误:”“[对象对象]”“运行 Node index.js时出错(错误代码)”Chat with Bot

最佳答案

正如我所见,您在这里错误地访问了生成的 json 响应对象:

$.sendMessage('Cryptosoul: price', response['data']['USD']['price'] 
[3501])

只需漂亮地打印响应对象即可提供访问某些属性的正确方法。

{
"status": {
"timestamp": "2019-04-02T08:38:09.230Z",
"error_code": 0,
"error_message": null,
"elapsed": 14,
"credit_count": 1
},
"data": {
"3501": {
"id": 3501,
"name": "CryptoSoul",
"symbol": "SOUL",
"slug": "cryptosoul",
"circulating_supply": 143362580.31,
"total_supply": 499280500,
"max_supply": null,
"date_added": "2018-10-25T00:00:00.000Z",
"num_market_pairs": 3,
"tags": [],
"platform": {
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"slug": "ethereum",
"token_address": "0xbb1f24c0c1554b9990222f036b0aad6ee4caec29"
},
"cmc_rank": 1232,
"last_updated": "2019-04-02T08:37:08.000Z",
"quote": {
"USD": {
"price": 0.000201447607597,
"volume_24h": 12118.3983544441,
"percent_change_1h": 1.48854,
"percent_change_24h": 6.88076,
"percent_change_7d": 12.4484,
"market_cap": 28880.04882238228,
"last_updated": "2019-04-02T08:37:08.000Z"
}
}
}
}
}

因此我们可以看到 price 字段位于 USD 对象下,而该对象本身位于 quote 对象下,而您的代码中缺少该对象。

获取它的正确方法是:

const price = response["data"][3501]["quote"]["USD"]["price"];

PriceHandler 代码:

PriceHandler($) {
rp(requestOptions)
.then((response) => {
const price = response["data"][3501]["quote"]["USD"]["price"];
$.sendMessage("Cryptosoul: price", price);
})
.catch((err) => {
console.error("API call error:", err.message);
});
}

关于node.js - 如何将 coinmarketcap API 中的值添加到电报消息中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55465087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com