Author: Joseluis Laso
Updated on: 2017-02-07
Posted on: 2015-11-18
Package: PHP Telegram CLI Wrapper
Read this article to learn how to implement instant message based user interactions in your PHP Web site using SMS, WhatsApp, Facebook or Telegram.
We often want to have more ways to interact with the users of our Web site, blog or portal. We want to be able to notify these users about new contents or tell them that we are expecting some action from them.
The traditional way to implement that task has been done using email. Although email is totally fine, this is a slow method that does not provide real and immediate interaction.
First I would like to take this opportunity to talk about how to properly implement an instant notification method that involves the user directly. Of course we have to respect always the local laws where our service will be available.
In any case we should respect always these three principles:
1) The user has accepted to receive our notifications
2) The user can reject our notifications in any moment.
3) User can change the frequency of notifications.
With these principles in mind, let's see which methods are available to do this task of sending immediate notifications to our users.
What Means Can We Use For Interacting with Our Site Users?
These are the main methods that come to my mind to notify users:
1) Using your own site native mobile app push notifications
2) Send SMS messages to the user phone
3) Send social application messages: WhatsApp, Telegram, Facebook, Line, etc..
I believe that the first method provides a much closer interaction level with the user and often is the the one that gets the fastest reaction from his part.
Obviously, having your own site mobile app can attract more users. Although it is also true that they only can use it after having downloaded and installed the app on their smartphones.
Good Purposes for Interacting with Our Site Users
Think about stuff like publication of new content, prompt the user for an action required in order to continue processing something, new interaction with the site or with other users. This is more obvious when we talk about intranets and company online applications.
Let's focus our attention in a real world example of mine. As some of you know I am the owner of tradukoj.com, a collaborative environment to help developers to translate their own developments, mainly made with PHP and Symfony.
Just imagine that you have a tradukoj account and you are translator of several projects. If the site had the possibility to notify you when there are new strings pending to translate, what do you prefer? Would you provide your email or you mobile phone number just to be notified? Let's depart from the assumption that yes, you provide your email or phone number.
Just imagine that you put your mobile phone number. The question is: What do you prefer to receive? An SMS saying that there are new strings pending to translate or a message in your social message app that allow us even respond with the translated string?
The rest of the article will give you some criteria to let you decide which messaging means are the best for your purposes and how can you use them in your own PHP projects. Keep in mind there is no single solution. You can create your own mobile app, as well you can send SMS messages in PHP with several solutions available in the PHP Classes site.
Social Application MessagingFor now, let's focus in how to use social messaging apps in our own benefit. In this case we will start with the most common social chat applications: WhatsApp, Facebook, Telegram.
The main problem is that the only library that exists to access WhatsApp API is not authorized by WhatsApp and so it is not supported by the WhatsApp team. You can try it at your own risk.
In my case I have to say that I installed successfully this PHP library for testing purposes and I used it to send and receive messages from and to users, although I am a bit scared after reading some comments around the Web about its use. It is said that you can lose the possibility to use your mobile number to access WhatsApp, as you can be banned for WhatsApp for the rest of your life.
Following the installation steps on its repository, you can successfully send and receive messages from other users. My advice if you do not want to get banned, use a brand new number instead of your current number.
Also try to follow the steps to use optional libraries, because more of current users use the encryption support, otherwise you can not talk to them.
In my humble opinion, I think WhatsApp should recognize that developers want to use their app and provide methods to do that in an authorized way.
Here you can find the unofficial WhatsApp API that I am talking about.
Facebook is obviously the most popular social network. There is no more to say about this fabulous application. You could use for instance this PHP class to send notifications to your Facebook users .
However this is not an officially supported way to communicate with Facebook users because it is faking a real user, and so it may stop working when Facebook changes the way their Web based messenger interface, if it still works at all. That is the problem on relying on unofficial APIs.
Until recently Facebook supported the XMPP protocol to chat with users. However, XMPP support was discontinued this year.
A supported alternative is to send application notifications to your users. These are one-way messages that applications can send to prompt the user for some action. It is often used by games but they also work in non-games applications.
There is an official API provided by Facebook for sending application notifications. It requires an application token (not a user token) which you can obtain using a generic OAuth client class like this.
The main restriction of this API is that it can only be used by Facebook Canvas applications. Those are applications that have a secure URL that is served inside a Facebook page in an iframe, as if it is part of Facebook.
Telegram is also a popular social chat application that I think it is a better option for developers to Interact with our users. It has an offical API and strong support directly by the Telegram staff team.
The only question is which one of the two available APIs we going to use?
We have the native or legacy API, that allows us to use telegram in the same way we use on the smartphone app or in desktop app. We can send and receive messages in our own phone number.
The other choice is to use the Bot API. It is much easier to use but for me it is not the right way to control notifications. Each user has to add our bot on his telegram app. So, we lose the control of which user is using our system or not. And there is no real and customized interaction.
The worst part is that the first option has only a cli app that runs on console.
Following the work of zyberspace I have created my own Telegram CLI wrapper class to easily use the methods that telegram-cli provides directly from PHP.
In the following part of this article we will see how we can send automated messages to our users from a crontab script, how we can take simple orders from our users to do simple things, and how to integrate other apps like webchat.
If you liked this article or have questions about the different instant messaging methods we can use in our sites, post a comment here.
You need to be a registered user or login to post a comment
1,528,482 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on: