Telegram Bot: Give Your Bot Some Tips

Intro

If you already know how to create a Telegram bot you probably don’t need this. Newbies only 🙂

Previously I wrote an entry on how to create a basic Telegram Bot here. I’ve made some “functions” that return mostly randomized texts and photos then I had an idea on how to save some string data on your webserver which the bot could retrieve. Be sure to check that one first before diving into this one (if you haven’t started creating any bots before).

The code is not the most beautiful one out there but it I think it’s a fun tip to share with fellow PHP newbies. If you’re lazy to read, the summary of this article is this: I made the “/tip” command so my friends tip the robot for whatever reasons (mostly after the robot gave them some sexy massages). So the goal here is to create a file that updates itself so that the tips collected can be summed up and reviewed when asked. Create a file with an array in it. The array contains one value (0). When someone pushes a command (giving a tip), use the Filesystem Functions and write array_push in it. Save and close that file after each update.

The Tipping

Sounds good? Let’s check it out. Create a new file (name it anything you want or integrate with current bot) and insert the following codes:

<?php

if($message == "/tip"){

    $getStats = fopen("robostats.php",a);
    $tipAmount = rand(0.10,50);
    fwrite($getStats, "<?php array_push(\$money, $tipAmount); ?>");
    fclose($getStats);
    file_get_contents($path."/sendMessage?parse_mode=HTML&chat_id=".$chatId."&text=Thanks for the tip!");
    file_get_contents($path."/sendMessage?parse_mode=HTML&chat_id=".$chatId."&text=Tip Given: R$ ".$tipAmount);
    file_get_contents($path."/sendMessage?parse_mode=HTML&chat_id=".$chatId."&text=Use /wallet to view tips collected");

}

?>

Breaking it Down

Pretty straightforward isn’t it? So the first thing you’ll notice is the /tip command invoked using the if(){} block. Then the codes inside it go like this:

  1. $getStats = Set a resource file handler using fopen(). Apply the “a” parameter to append new content at the end of the file’s content without overwriting everything. Read the full info on fopen() from the manual.
  2. $tipAmount = This variable generates number using rand(). I set it between 0.10 and 50 (10 cents to 50 dollars or whatever currency).
  3. Then I use fwrite() to write the new content everytime /tip is called. In the fwrite() parameters you have the resource file and content to be written declared.
  4. After writing always close your resource file handler using fclose();
  5. Next three lines I return some text messages showing that the action was successfully done. Message 1 reads: Thanks for the tip! Message 2 reads: Tip Given: R$ (random amount). Message 3 reads: Use /wallet to view tips collected. This command is used to check the total amount of tips collected.

That’s the working code but the other action is in another file. Create another file, based on this example I named it robostats.php. In this file the initial (before anyone sends the /tip command) content is:

<?php $money = array(0); ?>

Once you put that, save that file and close.

So whenever you call the /tip command, the if(){} block tells the server to write something like this:

<?php array_push($money, 24); ?>

I said ‘something like this’ because the second parameter in array_push is random. Note that I set it to be numbers between 0.10 and 50 in the first code samples shown. In this piece of sample, 24 was generated.

So once you array_push it the new content will look like this:

<?php $money = array(0); ?><?php array_push($money, 24); ?>

My current robostats.php look like this now:

<?php $money = array(0); ?><?php array_push($money, 24); ?><?php array_push($money, 6); ?><?php array_push($money, 49); ?><?php array_push($money, 38); ?><?php array_push($money, 22); ?><?php array_push($money, 30); ?><?php array_push($money, 38); ?><?php array_push($money, 13); ?><?php array_push($money, 40); ?><?php array_push($money, 25); ?><?php array_push($money, 17); ?><?php array_push($money, 18); ?><?php array_push($money, 29); ?><?php array_push($money, 48); ?><?php array_push($money, 49); ?>

Everytime you call /tip, the same action will occur at the end of the file. Why array_push? When you put as many array_push as you want as shown, whenever the file (robostats.php) is included, every array_push() commands send their values to the initial $money array making it from this:

$money = array(0)

to this…

$money = array(0,24)
// and so on...

The Bot’s Wealth

Now the next part is on how to retrieve this info and make it looks like the total amount of tips collected by the bot when queried. Create another command block like the /tip block above

if($message == "/wallet"){

    include("scripts/robostats.php");
    $current = number_format(array_sum($money), 2, '.',',');
    file_get_contents($path."/sendMessage?parse_mode=HTML&chat_id=".$chatId."&text=Tip collected = R$ ".$current);
    file_get_contents($path."/sendMessage?parse_mode=HTML&chat_id=".$chatId."&text=Thanks :)");

}

Again note the biggest block — the if(){} block which holds the statement $message == “/wallet”. Here are the next steps:

  1. Include the file we created earlier called robostats.php where we kept all arrays written.
  2. $current = the real work here is array_sum(), the purpose is simply to sum up all pushed arrays. In this example so far it’s 0 (initial) + 24 (the added array). So 24 in total. Then we wrap the array_sum() function with number_format() function so that when the numbers get bigger it will format them nicely by adding the comma separator for example: 1,000 or 12,456 and so on. Read about number_format() from the manual.
  3. That’s it, the working numbers are nicely calculated and formatted for display.
  4. Next two lines displays the output and they read; Message 1: Tip collected = R$ <value of $current>. Message 2: Thanks 🙂

How was that? Let me know in the comments 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

LINKS :)

© botdroid.me since 1998. Engine by Wordpress. Custom theme. Privacy Policy. Browse comfortably please. Borrowed media credits.