You are currently viewing CSC Tracker Bot Using PHP.  Have you ever tried to track CSC… |  by zeroxlive |  Coinmonks |  Oct, 2022

CSC Tracker Bot Using PHP. Have you ever tried to track CSC… | by zeroxlive | Coinmonks | Oct, 2022

Have you ever tried to track CSC Network when you are using social media.in this tutorial we will code a CSC tracker bot using php and coinex.net api service

Telegram

Telegram is a popular instant messaging service that prides itself on security. It has all of the features you would expect from a modern chat platform, including chatbots: software-based agents that you can program to read and respond to other users’ messages.

Setup Your Telegram Bot

The first step in creating a Telegram bot is setting up the profile that the bot will ultimately stand behind. This is also how you get a token for the Telegram API.

To set up a new bot profile, log in to your Telegram account and start a conversation with the BotFather (@BotFather), an official account that allows you to create and manage bots. In that conversation, enter the /newbot command.

The BotFather will ask you to choose a display name and username for your bot. The username has to end in “bot” and must be unique. In our example, we’ve settled on the display name CSCTrcker_bot and username CSCTrackerBot

Once you’ve landed on a valid username, the BotFather will automatically register your bot and reply with a token for the Telegram API, unique to that bot. Make sure not to share your token with anyone.

The next step in creating a Telegram bot is setting up the webhook that will communicate with your bot. Webhooks are how APIs tell you that something has happened, which stops you from having to query the API every few minutes (or seconds) to find out whether, for example, a new message has been sent.

Telegram uses just one type of webhook, which sends you an update object whenever anything happens.

Setting up the webhook is incredibly easy. There are just two things you need to know: your API token (you should have this from step one) and the URL where you’ll be hosting your bot. The URL will be something like https://Example.com/Bot.php. Make sure that you include the https at the start of the URL; otherwise, Telegram won’t send the webhook.

Now, in a regular web browser, navigate to https://api.telegram.org/bot<yourtoken>/setwebhook?url=https://Example.com/Bot.php. Voila, your webhook is now up and running!

it’s time to write the logic for ourTelegram bot. Here’s how I’ll go on…

The first thing to do is initialize a variable that will make it easy for us to call the Telegram API. That’s as simple as $path = "https://api.telegram.org/bot<yourtoken>.

Paste Your token api ( from bot father ) to .

Since we’ll be receiving updates by means of the webhook, let’s create and populate an array with that update data: $update = json_decode(file_get_contents("php://input"), TRUE)

Now, for the sake of convenience later on, let’s extract two crucial pieces of data from that update — the chat ID and message (if the update is not caused by a new message, this field might be empty, and we’ll code for that later):

$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];

In case you haven’t yet guessed what this bot is supposed to do, I want it to tell me the current weather for my location of choice. For that, I’ll create a /asset [Address] command.

To do that, let’s create an if statement to see if the message starts with /asset. We can accomplish that with the strpos() function, which tells us the position of a substring within a string:

if (strpos($message, "/asset") === 0) {
}

Nested within that if statement, let’s write some code to extract the location by chopping off the first nine characters of the message (which is how many characters are used up by the /asset command, as well as the space that will follow it):

if (strpos($message, "/asset") === 0) {
$asset = substr($message, 9);
}

If this bot were to be used in production, we’d have to add some input cleansing to make sure the location takes the right format. But it’s not, so we won’t worry about that.

Now we’ll get data from coinex.net :

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://www.coinex.net/api/v1/transactions/".$message,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"apikey: YOUR API KEY",
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response, true);

Here we ought to implement some kind of error handling, but I’m not going to bother. Instead, let’s hope for the best and shoot our bot’s response off using the Telegram API:

file_get_contents($path."/sendmessage?chat_id=".$chatId."&text="The list of assets:". $.data);

All in all, here’s what the code looks like:

<?php
$path = "https://api.telegram.org/bot<PasteYourTokenHere>";
$update = json_decode(file_get_contents("php://input"), TRUE);$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://www.coinex.net/api/v1/transactions/".$message,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"apikey: YOUR API KEY",
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response, true);

if (strpos($message, "/asset") === 0) {
$location = substr($message,6,42);
$asset=$data;
file_get_contents($path."/sendmessage?chat_id=".$chatId."&text="The list of assets:". $.data);
}
?>

Deploy the bot

With the logic done and dusted, save your code as a PHP file. Then, upload the file to you can upload your bot to a shared hosting and setup webhook.

New to trading? Try crypto trading bots or copy trading

Leave a Reply