我正在尝试完成电报机器人,它将在几个命令响应后响应消息...失去了尝试自己解决这一问题的希望。那些带有预定义消息的命令已经完成并且像一个咒语一样工作。。但是现在被卡在/ price命令上,该命令应该在来自coinmarket API的消息中显示硬币价值。
我尝试了许多变体,但以下结果总是要求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;
在节点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调用错误:” “ [对象对象]” “运行节点index.js(错误代码)时出错” Chat with Bot
答案 0 :(得分:0)
如我所见,您在这里错误地访问了生成的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);
});
}