PHP Classes

Interacting with Your PHP Site Users with the Telegram CLI Wrapper - PHP Telegram CLI Wrapper package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Telegram CLI Wrapper PHP Telegram CLI Wrapper   Blog PHP Telegram CLI Wrapper package blog   RSS 1.0 feed RSS 2.0 feed   Blog Interacting with Your...  
  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Viewers: 863

Last month viewers: 11

Package: PHP Telegram CLI Wrapper

In the first part of this article we talked about several solutions to notify our Web site users using SMS, Facebook, WhatsApp and Telegram when you want them to return to your site to see some new content or take some action.

Read this article to learn how to notify Telegram in practice using the PHP Telegram CLI Wrapper class with some PHP code examples.




Loaded Article

Contents

Introduction

Storing User Information

The Normal Usage Flow

Security Concerns

The Main Services Section

Some Examples to Run from your Shell Console

Get the Contacts List

Conclusion


Introduction

As we have seen in the first part of this article, you can use different instant messaging systems to notify your Web site users about important events, like new content that was published or some action that the users need to perform as soon as possible.

I talked about alternatives like using SMS, Facebook, WhatsApp and Telegram. In this part of the article I will describe a practical case of use of the PHP Telegram CLI Wrapper package as a guide to show how you can use it in your projects to send instant messages to your users using Telegram.

Storing User Information

When you want to send messages from one user to other Telegram users, you have to store the information of your users somewhere.

This article presents a really simple proof of concept in order to show you the main concepts of how it works. Accessory things are solved in the easiest way possible so we can focus in the core concepts.

For instance, user information is stored in files. In your real application implementation you probably would to use a MySQL database.

The StorageSystem looks as simple as this:

namespace TelegramCliWrapper\Storage;
use TelegramCliWrapper\Models\BasicObject;
class LocalFilesStorage implements StorageInterface
{
    public function save(BasicObject $obj) { ... }
    public function getById($id) { ... }
    public function remove($id) { ... }
}
So to demonstrate how this class works, this is enough to have your user information stored in your system, in this case as a simple files.

By the way, as you may notice some of the services provided by the class are really simple. Please consider them more as pedagogic rather than useful to be implemented as is. The main intention is to show you how you can implement certain services to your users.

The Normal Usage Flow

Usually it works like this. A user comes to our Web site and enables an option to authorize being notified through Telegram app.

Our site presents him a small form, basically asking for the phone number and the code to access to our site, like a password. But initially the user does not have the code yet. So he checks the option "I am new" and then clicks on the "Register" button.

Telegram cli wrapper login

You can see an online demo here.

Security Concerns

In order to prevent the indiscriminate use of our system in the repository you can have several security measures like:

1. The system checks if the phone number already exists in our user storage

$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById( $phone );
if ($user) {
return Response::error('phone exists already in this system');
}

2. The system checks that each request for a number does not exceed 15 minutes. In other words, you can not try to register a number again in the same 15 minutes.

$phoneRequested = isset( $_SESSION[ 'phone_requested' ]) ? json_decode( $_SESSION['phone_requested' ], true) : array();
if (isset( $phoneRequested[ $phone ]) && (intval(date("U")) < $phoneRequested[ $phone ])) {
  return Response::error('phone already requested, you must to wait 15 minutes');
}
$phoneRequested[ $phone ] = intval(date("U")) + 15 * 60 * 60;
$_SESSION[ 'phone_requested' ] = json_encode( $phoneRequested );

You can enable these security measures or even implement other measures. It is important that we trust the phone number that user enters. This is meant to avoid that our Telegram account be consider as spam bot.

As an alternative you can do the following. The user can join our system and send to us a Telegram message with the code that we show to him in our web site.

Back to the main flow of the notifications to users. The user has to be received a code and a welcome message.

This code and the phone number have to be used to access our system like a password. This way the user has access to the services that our site provides.

The Main Services Section

Telegram cli wrapper area

The first service provided in the main section is to send a message to the user phone. The idea is to emulate the system notifications. Actually in our implementation, from this panel the user can only send messages to himself.

Another service, “check received”, serves as an emulation of the periodic script run eventually from cron that we have to create in order to make the system really useful.

The user can send a set of commands through telegram to our system. The system checks the last message received from the user and tries to interpret and execute it as a command. As I said, it is a simplified version of the real system that we can implement in a real project.

Now, the system is able to recognize the following commands:

* help => show current orders
* remove me => remove my phone number from the system
* send me a photo => ask for a photo
* tell me a joke => I send to you something funny
* say me the time => ask for the current time
* weather => ask for the weather
The code to implement this service is pretty simple. Maybe you can use it as an example for your server script that will check the received messages started regularly from cron.
$text = strtolower(trim($message->text));
$response = "";
switch ($text) {
 case "help":
  $response = "These are the things you can ask me:\n" .
  "help => this info\n" .
   "remove me => remove my phone number from the system\n" .
   "send me a photo => invite system to send a photo\n" .
   "tell me a joke => I send to you something funny\n" .
   "say me the time => I send to you the current time on my timezone\n" .
   "weather => I send to you the weather where I live\n";
  break;
 case "weather":
  $weather = new OpenWeatherApi();
  $response = $weather->getWeatherInfoAsString();
  break;
 case "say me the time":
  $response = sprintf("The current time here is %s", date("l, F jS Y h:ia"));
  break;
 case "tell me a joke":
  $response = IcndbApi::getAJoke();
  break;
 case "remove me":
  $t->msg($user->phone, "You have been deleted from my contact list");
  $t->del_contact($user->phone);
  $userStorage->remove($user->phone);
  unset($_SESSION['user']);
  header("location: index.php");
  break;
 case "send me a photo":
  $t->send_photo($user->phone, MediaSelector::getRandomPicture());
  break;
 default:
  $response = "I'm so sorry.\nI'm not ready yet to understand you";
  break;
}
$t->msg($user->phone, $response);

As I said, the basic idea is to demonstrate how we can use telegram to notify user and receive some feedback from him.

There are other utility services in the profile and messages tabs. These are meant to let you see how to work with some of the class methods. You can see a live demo here.

Some Examples to Run from your Shell Console

There are also some small example scripts that you can run on your system shell console. Remember that to do that you need to configure your telegram-cli program copy. In the README.md file it is explained basically how to do that.

Requirements to Install Class

You have to download telegram-cli and start at least once manually in order to configure your copy with your phone number and get the SMS that allows you to activate it.

Remember as well that in the config/config.ini you have to put some configuration values needed to run the examples. You have an example of the values that you can use in the file config/config.ini.dis.

Get the Contacts List

use TelegramCliWrapper\TelegramCliWrapper;
use TelegramCliWrapper\TelegramCliHelper;
$th = TelegramCliHelper::getInstance();
$t = new TelegramCliWrapper( $th->getSocket(), $th->isDebug() );
echo "These are my contacts" . PHP_EOL .
User::getTitles() . PHP_EOL;
$contacts = $t->getContactList();
foreach ($contacts as $contact) {
echo $contact . PHP_EOL;
}
$t->quit();

Conclusion

As you may have read, creating interactive services with the Telegram platform is really simple. You can take the examples provided with the package and easily adapt them to your real projects.

If you liked this article or you have questions regarding using the PHP Telegram CLI Wrapper package, post a comment here.




You need to be a registered user or login to post a comment

1,611,040 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

1. installation - Hosein Rad (2015-12-31 11:25)
installation... - 1 reply
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  
  All package blogs All package blogs   PHP Telegram CLI Wrapper PHP Telegram CLI Wrapper   Blog PHP Telegram CLI Wrapper package blog   RSS 1.0 feed RSS 2.0 feed   Blog Interacting with Your...