SIP <-> Telegram: sip.tg

    I’m sure many asked themselves: there is Telegram, a year ago it got calls, it has an open API, but for some reason no one has made a connection to the SIP protocol for some reason. After all, it gives unlimited possibilities: from replacing SIP dialers to organizing another channel of communication with users of your business. Today I will tell the story of the appearance of the first such gateway, which is now available to absolutely everyone!

    Once upon a time I got to Talk37 resource (now it is ruVoIP.net ) and I really liked it. Despite the fact that recently it has gained notoriety thanks to the efforts of darknet lovers, the active community of the project is still people who root for it. I thank the creator Dmitry Serov for his responsiveness and hard work.

    ruVoIP.net is a free virtual PBX, and almost any commercial PBX would envy its functionality. Each user registered in it receives an internal number during registration, which can be reached in many ways . And so, on September 24, 2017, the first prototype of the gateway saw the light: now it is possible to reach ruVoIP.net users via Telegram! Rather ... what does it mean maybe? We created a separate Telegram user who learned to receive calls and greet callers with a request to enter an internal number. But where to enter? How to enter? In general, something worked and caused all active participants great joy akin to launching a rocket into space. It doesn’t matter that it often crashed, there was no DTMF, and the sound was reminiscent of an old HF receiver.

    Time passed, the gateway improved, and ruVoIP.net entered the TOP of search engines at the request of “sip telegrams”. I began to receive requests from different people who want to make the same gateway; there were commercial offers. Unfortunately, the prototype architecture did not allow its extension and was too complicated for end users to use, so I was forced to refuse to promise everyone to do something. I really wanted to bring something to the new year, but I could not do it.

    By February 12, 2018, the new version of the gateway finally reached the level when it was already possible to feel it - closed beta testing began. I’m doing a project of this scale for the first time, so not everything worked out in an organized manner, but it brought its benefit: in the end, I managed to fix about a couple more dozen bugs on those user cases that I didn’t even think about.

    What happened


    I can not say that I managed to fix absolutely all the bugs. In the process, I actively communicated with those who wanted to make the same gateway and I had a picture of what users needed to build up. On the other hand, each function very much depends on the capabilities of the Telegram itself. Not everything was done conveniently (but something could not be done at all) due to platform limitations.

    The result is two main modes: Operators and Gateways . Both modes are configured through the @siptg_bot bot .

    Operators


    This is the mode of replacing SIP Softphone with the Telegram messenger. If you already have an account with some SIP provider, but you do not want to install (on the phone) another Softphone, which takes up memory, consumes battery, requires fine-tuning of codecs and does not adjust well to the quality of the data transfer channel, then this mode is for You. It is allowed to create up to 20 independent connections to SIP providers from one Telegram account (artificial restriction, it may be adjusted later). Calls for each provider work in both directions: both incoming and outgoing calls are possible. Editing an account looks something like this:


    The buttons contain either the current value of the option, or its name, if the value is not set (the exception is Password ). On each settings page there is a help that is called up by pressing the Help button .

    How to receive calls?


    Since the bots in Telegram cannot call, the service has a separate @siptg account that performs this function. With an incoming call, the number and name of the caller are taken from the SIP headers and come in a separate message; along with the message also comes a DTMF keyboard: Note: I understand that the current implementation looks strange and not convenient, but this is a necessary


    measure: the Telegram API does not allow to determine the name of the subscriber using the local address book; for the same reason, it is not possible to place information about the number of the caller on the call screen.

    How to call?


    Here we are again faced with the limitations of Telegram: you can’t dial an arbitrary phone number, you can only call another user. Therefore, there are two ways to call an arbitrary number:
    • send a phone number as a message;
    • attach a contact containing a phone number to the message.

    For convenience, both methods can be applied both to the bot (@siptg_bot) and to the account for calls (@siptg). If several operator accounts are configured, you will be asked to select an operator for the call:

    Then an incoming call to Telegram arrives and, after picking up the handset, the connection with the dialed number begins.

    Note: for each operator, you can specify the transformation rules for the dialed number and restrict its use for numbers matching the regular expression. If the dialed number does not match the regular expression of the restriction, the operator is excluded from the request to select an operator for the call. In the case of a non-alternative choice, the query does not appear. More information is available at the time of editing the Dialing Rules .

    Gateways


    The most interesting mode of operation is the Gateways mode . This mode allows you to use the Telegram account via SIP: receive calls and make calls from your Telegram account to other users. For example, you can create a special Telegram account for your business, inform it to your customers as a contact and receive calls, integrating them into the general queue of the virtual PBX. And since calls are possible in both directions, your operators will also have the opportunity to call the customer back if necessary; at the same time, the client will see the call on behalf of your company.

    Why do I need a separate Telegram session?


    The Telegram messenger allows the use of one account from different devices, and a separate session is created on each device. Since when using sip.tg in gateway mode, integration with SIP is performed on behalf of your account, you need to create a new Telegram session. Creating a session is available through the bot settings: in the Gateways section , click the Login button and follow the instructions.

    Note. The Telegram API does not allow limited access to your account, and therefore, by creating a new session, you consent to the transfer of full rights over your Telegram account to sip.tg service. The service administration makes every effort to limit the use of your Telegram account to third parties, does not transfer information about your Telegram session to third parties and does not use your Telegram account for purposes other than stated, but is not responsible for any damage that may result from theft of this information by third parties by hacking, or physical access to the equipment of the service.

    How to connect Telegram to a virtual PBX?


    After creating the session, you can configure the connection of your Telegram account to the virtual PBX. To do this, create a new SIP connection in the Gateway settings section of the bot:

    There are two ways to connect: incoming ( New Entry button : you configure a virtual PBX to connect to sip.tg servers) and outgoing ( New Out button : sip.tg servers connect to a virtual PBX). You can create several inbound and outbound connections and use them independently on different devices (in this case, the total number of connections should not exceed 20). Flags DTMF Rin. and DTMF ref. allow you to control the display of the extension dialing keypad and the display of the number of the caller with an incoming call.

    How to make and receive calls?


    Regardless of the selected method of connecting to a virtual PBX, incoming and outgoing calls are made the same way: with an incoming call of a virtual PBX , the telegram number and name of the caller are transmitted , and for an outgoing call, just dial the Telegram user number . You can use one of the following values as the Telegram user number :
    • user's real phone number tied to his account;
    • user username ;
    • * User ID .

    I also note the following features:
    1. With an incoming call, if only part of the data is known about the user, the first of the available values ​​is indicated as a number: phone number, username or ID. That is, if the user's phone number is not known, but the user specified username, then username will be returned as the number. If the user does not know either the phone number or the username is set, then * ID will be indicated as the number.
    2. You can call a user by their ID only if there was a direct or indirect interaction with him earlier: messaging, previous calls, etc.


    Something else


    The account for @siptg calls is registered to a real phone number. Why not use it for anything else? The first thing that comes to mind is to organize a gateway to Telegram from the PSTN. Yes, I’ve already thought about it, but so far I haven’t done it. And he did the following.

    Sometimes, leaving for roaming, you need to stay in touch. Since it is very expensive to receive calls to a Russian SIM card, I thought it would be nice to receive incoming calls to Telegram! All that is needed is to set up call forwarding on a Russian number and buy a local SIM card for Internet access.

    To forward incoming calls to Telegram, you need to complete a USSD request before traveling. After the trip, run the command to turn off call forwarding.**21*+79581006789###21#

    Notes:
    1. The cost of the call forwarding service is paid according to your tariff plan as a call to a mobile number in Moscow.
    2. The phone number to which the Telegram account is registered must match the phone number on which the call forwarding is established.
    3. The correct operation of this function depends on your service provider. In particular, work with non-Russian telecom operators is not guaranteed.


    What will happen next?


    Frankly, the uncertainty regarding Telegrams in Russia has been and remains. In addition, the fate of the project depends entirely on the Telegram team. I don’t know what the team’s reaction will be, because having the bad experience of contacting technical support earlier, I did not start contacting again. If approved, it will be possible to significantly improve the current implementation, and I see absolute benefits for everyone: the project opens up new opportunities for Telegram as a platform, which, ultimately, should attract a new audience.

    I also have a clear understanding of what needs to be done next, I well know the bottlenecks that will need to be addressed. I also understand that as a result of the Habra effect, everything can break, because I did not allocate large powers at the start, but I hope that the inherent ability to scale will reduce the recovery time. Nevertheless, if everything will be really bad - come in 2-3 days later :).

    I have no concrete plans for the commercialization of the project, at first all the functionality will be completely free. Nevertheless, if you expect a big load on your carrier or gateway, inform us in advance by contacting technical support: you can already transfer your accounts to a separate server. If there is a request for specific functionality - also contact: together we will think about how this can be solved most optimally.

    In conclusion, I will leave links to all project resources:

    Also popular now: