RingCentral 短信和比特币金库
一篇关于在比特币金库加密货币价格变动时如何使用 RingCentral 接收短信的简短文章。
引言
总结:这篇文章不是关于使用 RingCentral 短信应用挖比特币的,我打算使用他们的短信 API 在某种加密货币价格发生变化时获得通知。
我决定开始学习新的加密货币并投资于数字市场。有一种叫做比特币金库的新加密货币引起了我的注意。
它是一种全新的、完全去中心化的加密货币,是去中心化数字挖矿标准的一部分,具有新的防盗解决方案,已经在三个交易所上市。我开始在 miningcity.com 上挖矿。
我想持续关注它的当前价格,所以我想在价格发生变化时得到通知。我决定创建一个脚本,使用 Ring Central 的 API 向我发送一条短信,当当前价格与上次检查的价格相比变化超过 10 美元(上涨或下跌)时。
Using the Code
我们将需要以下工具
- Ring Central 开发帐户
- PHP
- CoinMarketCap API
- Cron Job (定时任务)
步骤
1. 创建一个 CoinMarketCap API
我使用 coinmarketcap 来检查加密货币的价格,所以请访问 https://coinmarketcap.com/api/ 并创建一个新的开发者免费帐户。
2. PHP 脚本
对于此测试,我创建了一个名为 RingCentral 的文件夹,并添加了一个名为 cryptoCurrency.php 的 PHP 文件。
打开你的空文件并复制这些代码行
<?php
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map';
$parameters = [
'symbol' => 'BTC,BTCV'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request
?>
在这个例子中,我们使用 /map
端点。
确保使用你自己的 API 密钥,否则它将无法工作,运行脚本,你将获取与负载参数中发送的符号相关的所有数据:'symbol' => 'BTC,BTCV'
。
在这个例子中,我们可以看到比特币和比特币金库货币的数据。这很有用,因为 API 建议使用内部 ID 而不是符号或名称。
现在我们有了比特币金库的 ID (5175),让我们使用将获取当前价格的端点: /v1/cryptocurrency/quotes/latest。
复制以下脚本
<?php
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
$parameters = [
'id' => '5175'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response,true); // json decoded response
print_r($result['data']['5175']['quote']['USD']);
curl_close($curl); // Close request</code>
这将打印我感兴趣的响应的唯一部分,即价格
为了比较价格,我们至少需要一个开始点进行比较,所以我们必须创建一个文件来保存当前价格。
创建一个名为 currentBitcoinVaultValue.txt 的文件并添加值 10
。
我们要做的就是将一个代码添加到我们的脚本中,该代码将读取文件中的当前值,然后使用从 API 读取的当前比特币金库价格更新它。
复制以下脚本,它将读取 BitcoinVault
的当前价格并将其保存到 .txt 文件
<?php
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
$parameters = [
'id' => '5175'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: b5f708a1-3c63-4152-9e37-3a4a883e3820'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response,true); // json decoded response
$currentBitcoinVaultPrice = $result['data']['5175']['quote']['USD']['price'];
curl_close($curl); // Close request*/
$fileName = 'currentBitcoinVaultValue.txt';
$processFile = fopen($fileName, "r") or die("Unable to open file!");
$oldPrice = fgets($processFile);
file_put_contents($fileName, $currentBitcoinVaultPrice);</code>
我的当前 .txt 文件现在已使用当前 BitcoinVault
价格进行了更新
3. 将 Ring Central 的短信 API 添加到我们的脚本
现在让我们继续有趣的部分,当价格发生变化时获取警报。对于本教程,我想知道当价格上涨 10 美元或下跌 10 美元时。所以让我们首先添加 Ring Central 的短信 API。
同样,我不会详细介绍如何设置 Ring Central 帐户。你可以在这篇 文章 中逐步操作,查看第 5 步(设置 RingCentral 短信 API)。获得一个开发人员帐户非常容易和快速。
让我们添加 RingCentral 的 API,你的脚本应该如下所示
<?php
require('vendor/autoload.php');
// CoinMarketCap API
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
$parameters = [
'id' => '5175'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: <code>b5f708a1-3c63-4152-9e37-3a4a883e3820</code>'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response,true); // json decoded response
$currentBitcoinVaultPrice = $result['data']['5175']['quote']['USD']['price'];
curl_close($curl); // Close request*/
$fileName = 'currentBitcoinVaultValue.txt';
$processFile = fopen($fileName, "r") or die("Unable to open file!");
$oldPrice = fgets($processFile);
file_put_contents($fileName, $currentBitcoinVaultPrice);
$calc = (float)$currentBitcoinVaultPrice - (float)$oldPrice;
if ($calc >= 10)
{
smsRingCentralAlert('Bitcoin Vault has gone up to '.$currentBitcoinVaultPrice);
}
if($calc <= -10)
{
smsRingCentralAlert('Bitcoin Vault has gone down to '.$currentBitcoinVaultPrice);
}
// Call Ring Central SMS API
function smsRingCentralAlert($message)
{
$RECIPIENT = 'YOUR-TEST-PHONE-NUMBER';
$RINGCENTRAL_CLIENTID = '<code>YOUR-</code>CLIENT-ID';
$RINGCENTRAL_CLIENTSECRET = '<code>YOUR-SECRE</code>T';
$RINGCENTRAL_SERVER = 'https://platform.devtest.ringcentral.com';
$RINGCENTRAL_USERNAME = 'YOUR-USERNAME';
$RINGCENTRAL_PASSWORD = '<code>YOUR-</code>PASSWORD';
$RINGCENTRAL_EXTENSION = 'YOUR-EXTENSION';</code>
$rcsdk = new RingCentral\SDK\SDK
($RINGCENTRAL_CLIENTID, $RINGCENTRAL_CLIENTSECRET, $RINGCENTRAL_SERVER);
$platform = $rcsdk->platform();
$platform->login($RINGCENTRAL_USERNAME, $RINGCENTRAL_EXTENSION, $RINGCENTRAL_PASSWORD);
$platform->post('/account/~/extension/~/sms',
[
'from' => ['phoneNumber' => $RINGCENTRAL_USERNAME],
'to' => [['phoneNumber' => $RECIPIENT]],
'text' => $message
]
);
}
请记住使用你自己的 API 密钥。
我使用较低和较高的价格做了一些例子来测试脚本。这是我的测试结果
4. 最后一步 Cron Job (定时任务)
在上一篇教程中,我解释了什么是 cron job,以及如何设置它。你可以在 这里 查看所有详细信息。
对于此示例,我将创建一个 cron job,它将每小时触发我们的脚本。
要创建 cron job,只需打开你的 CLI 并输入
crontab -e
确保检查你的 PHP EXE 的当前路径。此命令将有所帮助
whereis php
PHP 路径通常是
/usr/bin/php
运行 crontab
后,很可能会打开一个编辑器,这是我用于此示例的 cron 的一个示例,它将每小时运行
0 */1 * * *. /usr/bin/php /Users/juan/DEV/RingCentral/cryptoCurrency.php >
/tmp/stdout.log 2>/tmp/stderr.log
你可以更改 cron job 的时间安排,也可以更改要检查其价格的货币的 ID。
历史
- 2020 年 5 月 19 日:初始版本