
Hello. My name is Andrey.
Last summer I published a draft and an article "
Chat assistant to the site using Telegram in 15 minutes ." The idea of the project was to implement a
pop-up Chat Assistant on the site, the backend for which (for a chat) would be the
Telegram messenger that is gaining popularity.
Surprisingly, the project quickly began to recruit stars on
GitHub , and I received letters of thanks and requests for help in setting it up from people from all over the world. People could not run the script on their hosting: either set_time_limit (0) is not supported, then error 503, then the paths are not specified correctly.
The second version of the script is much easier to install and works on the basis of
WebHooks .
>>>> Link to the GitHub repository .
')
In a nutshell, what is it

For those who have not seen the previous article, I repeat.
Telegram Site Helper is a set of scripts (PHP, JS) that implement a chat assistant for your site (so that your visitors / clients can promptly ask a question in the chat to your manager). The manager will receive a message in the Telegram Messenger. And there can answer. The system is suitable for sites where there is not a very large flow of customers. There is still a small inconvenience: all messages from all visitors come to the manager in a single chat. To send a response to the visitor, the manager must first select "podchat", sending the command
/ chat_1234 , like this:
What's new
Chat is now powered by WebHooks.
Telegram server itself will send new messages to your server.
The ever-running script server for Long Poll requests to the Telegram server is no longer needed. But now HTTPS is needed (this is Telegram policy). You can
buy a certificate, make it free at
StartSSL or make a self-signed one.
For those who do not want to search, commands for the terminal:
How to create a csr file for StartSSLsudo openssl req -nodes -newkey rsa:2048 -keyout apache.key -out apache.csr
How to make a self-signed certificatesudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout apache.key -out apache.crt
Only one file is used for installation.

There is only one file in the repository:
telegram-site-helper-install.php is an installer. It will create the necessary directories, unzip JS, CSS and two PHP files.
Here you can also generate a JS chat code, which you will need to paste into the site page.
Attention: the scripts in the installer are encoded in base64Since for many website owners on Wordpress / Joomla, base64 is associated with viral injections - I invite all
paranoid people in advance who are afraid to explore the contents of the scripts encoded in base64 (for example, using
this utility ).
Popup and embedded chats, styling
Chat can be styled without getting into CSS. The color of the frame, background, font can be configured directly from the installer.

Another chat supports two display modes. It can be
embedded into any other HTML element or "
glued " to one of the corners of the screen. Chat can be opened by clicking on the standard label, or by clicking on an arbitrary HTML element.

You can also change all the captions (“Start chat”, “Enter your name”, “Attach file” and others).
File transfer
Now the client and the manager can share files (photos and documents). Files are stored on the Telegram server and do not occupy space on your hosting.

JQuery is no longer required
The last time I was scolded, they say you need to pull jQuery for the work of a small script. I realized everything and this time I wrote everything in pure JavaScript.
Works via Server Side Events or LongPoll
Chat on the site can now receive updates from your server not only using Long Poll requests, but also using Server Side Event broadcast (it will only work on hosting with
set_time_limit (0) supported and there are no problems with the
flush () function)
How to install
- Log into your server / VPS / hosting via SSH or FTP
- Create a folder (more convenient in the root of the site) and name it for example telegram-site-helper
- Make it chmod (access rights) at least 0775 (the installer script will create folders and files there)
- Upload telegram-helper-install.php to this folder
- Run the script via https (for example:
https: //mysite.ru/telegram-site-helper/telegram-site-helper-install.php
https: //mysite.ru/telegram-site-helper/telegram-site-helper-install.php
).
You should see the following: 
- Select Language and click Start Installation.
- Create a password for authorization of managers and enter it in the appropriate field (the manager will enter this password once for authorization from Telegram bot)
- Get the token for Bot at @BotFather and insert it (token) in the appropriate field:

- Choose where to store data: in SQLite or in MySQL. If in MySQL - configure access.
- If you use a self-signed certificate - enter the full path to it (you need to send it to the Telegram server)

- Click " Check settings and install Telegram Site Helper "
- If the installation was successful you should see the following:

- Open Telegram and write to your bot " / login 1234 " (where 1234 is the manager's password). If the installation was successful, the bot will authorize you.

- Go to the tab " Generator widget "

- Customize the widget and copy the code to the site. Everything.
- If you like the project - you can do Donate .
Future plans
- Still implement LongPoll support to the Telegram server to dispense with HTTPs
- Invent and do: how to divide customers into "sub-chats"
- Complete english translation
- Voice chat
- Ability to change / disable message sound
- Clever thoughts that habra users will write in the comments
At the end of the article I express my gratitude to the Telegram team for the platform for Bots. Extremely useful thing.
Thank.
UPD! I forgot to add that after installation, it is worth removing the installer from the server and setting up permissions for the folder.
Many thanks for the prompt Pull Request and code criticism. I am looking for strength and time for improvement.