📜 ⬆️ ⬇️

Chat assistant to the site using Telegram in 15 minutes



About chat assistants


Many people sell goods and services over the Internet. More people buy something online.
During the selection of purchases, there are often questions that can be solved by calling and talking to the manager.
Most likely I am not the only person in Habré who prefers to communicate with telephone managers.
And here come to the aid pop-up chat helpers, which seem to increase conversion, but many unnerving.
(For those who do not know: in the corner of the site a window pops up in which you can correspond online with a consultant).

There are a dozen of such services and they all work on the principle of " trial version for free, and then for the money ."
On the habr there are several articles, here is one of them ( http://habrahabr.ru/company/tuthost/blog/165365/ ), but, I am sure, the audience Habrahabra knows what it is about.
Most people will use the free version of any such service: you just need to register and insert a piece of JS code on the site. For those who have a lot of managers, they will have to pay: for example, a Redhelper for 10 operators will cost you:
115200 rub. for 1 year


')
Most likely - the price is adequate for those who pay salaries to ten managers.

But I decided to invent a free “bicycle” from scrap materials.
Running on your server takes 15 minutes. Anyone who is interested in the idea - please under the cat.


Telegram BOT


Gaining popularity abroad, Telegram Messenger recently introduced a new API for bot development . A thing is an interesting one, one can find many uses for it. Now you can create a bot (write a program) that will answer your questions, execute certain commands, and all this in the chat window of the Telegram messenger.

It is on the basis of the Telegram messenger and its API for bots that our free , unlimited chat assistant-to-site will work.

The general scheme of the idea is in the picture:



This is how it happens:
  1. Users on the site write to chat
  2. This message flies to your server
  3. From there Telegram-bot sends it to the right manager.
  4. Manager answers via Telegram
  5. The bot sends a message back to chat


Implementation


So, to run "at home" you will need:
  1. Server, VPS or hosting, which has up-to-date PHP, SQLite support and the ability to set_time_limit (0) (i.e. make the php script not time limited)
  2. Telegram application (on a smartphone or computer)
  3. Basic PHP knowledge

Attention! You run this code at your own risk! I spent a few hours on it and maybe it’s full of mistakes!


The action plan is:

0) Download source code from GitHub:
https://github.com/Surzhikov/TelegramSiteHelper

1) Register a new bot in Telegram.
All you need is a smartphone and 2 minutes.
Go to the Telegram, add the user @BotFather and send him the command "/ newbot"
Further, according to the instructions: we come up with the name, username, write about and load the photo.
The result of all should be the API Key with which we will work with BotAPI.
I did it like this:



(The message in which my API Key appears I have deleted)

2) The resulting key is inserted into TelegramBotConfig.php.
In the same place, we indicate any password you have invented for registering new managers.

3) Fill everything on your server

4) Run our bot - the background process getUpdates.php
It should work endlessly, so I recommend putting it in the server startup.
For reliability, you can put the launch of the script in the crontab every minute.
The script will run in a single instance, all repeated starts will be completed immediately.

5) Register a new manager
From the manager's smartphone, go to telegrams and add our new bot (the one you just created)
We write him a command: "/ newmanager password_for_managers." In response, we receive a greeting.



We repeat the operation as many times as necessary for each manager.
If at this stage you have not received a response from Bot, then something does not work!

6) Check
Go to the page index.html (that is, go to the address mysite.ru/index.php , and do not open locally).
There is a small, quick fix chat.
If you send a message to this chat, the bot will select a random manager and send it to him.



Here is an interesting point:
There can be a lot of clients (buyers), and one manager.
The manager communicates with customers through Telegram-bot.
If at the same time two (or more) clients have written a message, and the manager answers them in turn, the Telegram bot needs to somehow indicate to which client to send a response.
For this, I made the command "/ answer_HOME." After sending this command, all the letters of the manager go to the desired web chat.



Cons, pros that you can finish


Minuses
  1. Now the chat is made "in haste" in order to quickly launch it in your project. There are many loopholes with the help of which, for example, you can write in someone else's chat and peep someone else's correspondence.
  2. Now these problems do not bother me, because in the chat assistant on our site, it is not customary to transmit important and secret information.
  3. When a manager is alone and there are many clients, you can get confused about whom you answer.
  4. ... I will supplement from the comments


Pros:
  1. It works!
  2. Free forever and for any number of managers
  3. You do not need extra applications, only telegrams, which is for all popular platforms
  4. You can rewrite and style chat as you want.
  5. Telegram works very fast
  6. ... I will supplement from the comments


What can be done:
  1. Fool protection and security work
  2. A clever system of distributing messages between managers (the bot now sends a message to a random manager)
  3. Automatic answers from the bot when the manager is silent for a long time
  4. Add the name and photo of the manager who is in the chat
  5. ... I will supplement from the comments


Thanks for attention.

UPDATE 08/23/2015 — Fully rewrote the bot, now works more stable, now you can choose between sqlite and mysql

In the plans:
- admin panel with statistics
- work through webhook

https://github.com/Surzhikov/TelegramSiteHelper

Source: https://habr.com/ru/post/264035/


All Articles