如何将硬币市场帽API中的值添加到电报消息中



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

我尝试了许多变体,但以下结果总是要求 API 调用错误:或像 [对象对象] 这样的消息。

     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;

节点索引后从 API 响应.js(打开机器人,(来自 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 调用错误:"[对象对象]"运行节点索引时出错.js(错误代码("与机器人聊天

如我所见,您在此处错误地访问了生成的 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($) {
    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);
        });
}

最新更新