📜 ⬆️ ⬇️

Telegram: sending messages to a mobile number to chat with a bot

Like many, I use an asterisk to organize a telephone connection. In addition to their internal number, subscribers also have a mobile number, where redirections are made, and SMS are sent in case of a missed call.


Traditionally, missed call notifications were sent either to email or to mobile in the form of SMS. With the advent of Telegram, it became possible to send messages to the instant messenger. One bad luck: we can not send a message to the mobile number.


And it is more convenient for us, because we already have mobile numbers (we have hundreds of subscribers who want to receive notifications :), and we still need to find the chat id, and each has its own.


Solution: a small chat bot.


The chat bot accepts the user and asks for his phone number. Then the received number and the corresponding chat id are saved to the server. Now we can send a notification to a number to our service, and a bot attached to the service will forward this notification to the corresponding user in telegrams. It turns out and cheap (we now do not pay for SMS), and angry.


Two points


  1. until the user connects to the bot, we cannot send him messages.
  2. The user ID of the sender of the number and the identifier of the user with the sent number are compared; in case of discrepancy, the user will not be associated with the specified number.

The application is small, but extremely useful. You can send any message (which was previously sent via SMS or email) - notifications about calls, errors, server crashes, etc.


Few details


Sending occurs by sending an http request to a locally running web server, which in combination implements the chat bot. The server searches for a match in the mongo database and then sends the data to the Telegram. It's simple.


The web server is written in nodejs, and the telegraf library is used to interact with Telegrams.


How to create a bot and get a token already have instructions .


How to send a message?


POST request with json message


For example:


curl -X POST \ http://localhost:3030/send/<number> \ -H 'content-type: application/json' \ -d '{"text": "Ahtung!!"}' 

How does this work for me? After the call is completed, the asterisk calls a small script that looks at the call status and sends a message if necessary. That's all.


Github sources: telegram-alert
Installation is described in the repository.
For those who want a quick start there is a docker-compose.yml


Use video (50 seconds)



Suggestions? Questions?


')

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


All Articles