Adapter Telegram

Fork me on GitHub

Description

The telegram service is a platform for message exchange with a focus on speed and safety. Service is very simple and free. Applications for operation with the service are available for a variety of platforms – mobile phones, tablets, computers – you can use the telegram on all devices simultaneously. Also by means of this service by simple API it is possible to create bots (the virtual companions).
Driver telegram (Telegram Adapter) allows to send and receive messages, that is to exchange telegrams with the ioBroker system, having created a bot in the telegram service. It is possible to give a bot different commands (to turn on the light, to open garage gate and so forth), it is possible to request any information (temperature in the house, a status of system of heating and so forth), the bot can publish snapshots from security video cameras when actuating the motion sensor, generally the functionality of this decision is very rich. The driver can be used together with the text2command adapter.

Information

 

Current version
Necessary conditions /
Developer Bluefox
Keywords chat bot, control, commands
Github icon_link link
Platform Javascript/Node.js
License MIT

Installation

Installation is on the Adapters tab page administration system. In the driver group communication find a line called Telegram Adapter, and press the button with the plus icon in the right side of the line.

You will see a pop-up window driver installation, after installation, it will automatically close.

If all goes well, on the Settings driver tab will be a line telegram.0 with installed driver instance.

Setting

Registration and creation of a bot

To configure the driver, you must first create a bot for ioBroker management.
If you do not have an account in the telegram service, it’s time to create it, to do this, go to the link (web client, it is possible also through an installed application) and register. Then create a bot, for that purpose follow the link, if you use web client or contact search bar looking for a user named @BotFather and begin a dialogue with him.

So, we create a new bot, give the command /newbot. Next in the dialog you will be asked to write the name of your bot – invent name, input it and press enter. Then you need to enter user name and it must end with the word bot. Username must be unique, so if the name you entered is already taken, you will be warned about it and will ask to invent another. The ultimate goal of the dialogue is the passkey to use API HTTP (token access HTTP API).

Now, for security, you should set a password to communicate with the bot. To do this, open a dialogue with the newly created interlocutor and type the command /password security, where the word security – is the password (create your own).

Setting the telegram driver

Now you can open the telegram driver settings in the ioBroker system (driver settings tab) and to input there a key, the password, and if you plan to use text2command, to select an instance of this driver from the list.

 

Run the configured telegram driver instance on the Settings driver tab, click Inactive button. Click to start (the red button with the play icon).

Go to the Log tab and make sure that the driver is connected to the service telegram and that there are no errors.

Well, the driver is configured.

Examples of use

Working together with the text2command driver

To work in conjunction with the text2command driver, you need to select the installed text2command driver instance from settings of the telegram adapter, as it is described above.

 

Let’s add several simple rules for an example – the rule, How many time, What is your name and Temperature on the street.

 

That’s all! Now you can open a dialogue with the bot, log in and ask questions:

 

However, it should be noted that the full type of commands and questions to a bot it is not so convenient, especially on mobile devices. For a bot, you can specify a list of quick commands for faster input – is enough to write the character “/” and appears a list of pre-defined commands – you just select the required. To do this, we turn again to the user @BotFather. Enter the command /setcommands, then type the name of the created bot starting with “@” symbol.

Enter the command in the format:
Command – description

  • – command – short name of the command is written as one word with no spaces (can be through a sign underscore)
  • – вescription – a short description of the command

Each command from a new line, in the application we use the Ctrl+Enter keyboard shortcut.

 

Now you have to edit the rules a bit that keywords of commands matched:

 

Now it is possible in a chat with a bot to write the choice command character “/” and on the screen to be displayed the list.

 

Remote server reboot

Let’s review an example more difficult.

Suppose you want to build a command that would be perceived by the bot, and rebooted the server. To do this, you need installed and configured telegram driver, text2command and javascript.

In the javascript.0 driver instance will create a script called t2c_restartSystem and the following contents:

createState('t2c_restartSystem.restart_command', '');
on({id: 'javascript.0.t2c_restartSystem.restart_command', change: 'any'}, function (obj) {
  if (obj.newState.val === true || obj.newState.val === 'true'){
      log('Command reboot');
      setState('javascript.0.t2c_restartSystem.restart_command', 'false');
      exec ('reboot');
  }
});

In a script create the javascript.0.t2c_restartSystem.restart_command variable and subscribe to it. On a condition of new true value, give the message in a log, reset the value of the variable to false and perform a reboot command.
Let’s add the new rule to the text2command driver:

 

Now, in the telegram dialogue with the user @BotFather add command /restart_iobroker (really do not need to add a command, but rather to prescribe all possible, that is, re-enter the previously created).

 

That’s all, you can check how the newly created command is working:

Publication snapshots of IP cameras for motion sensor

In addition to commands, the bot can send messages on any event or a picture/photo. So, the next task:

  • there is a D-Link DSC-900 IP camera, the address in networks 192.168.69.81, the stream address mjpeg http://192.168.69.81/mjpeg.cgi, the address for image capture – http://192.168.69.81/image.jpg
  • in a security zone of the camera watches the motion sensor connected to the controller based on arduino MEGA + Ethernet shield, data transmission protocol MQTT, the mqtt.0.arduino56.PIR2 variable is set to true (there is a movement), false (there is no movement)
  • when actuating the motion sensor, it is necessary to send in telegrams images to timepoints 0, 10, 20 seconds after actuating of the sensor.

Write a script of the following contents:

var request = require('request'); //for an image request from the camera
var fs = require('fs'); //for temporal saving the image in the form of the file

//Function which requests the image from the camera, saves it in the temporal file and sends it to the telegram driver
function sendImage() {
  request.get({url: 'http://192.168.69.81/image.jpg', encoding: 'binary'}, function (err, response, body) {
    fs.writeFile("/tmp/snapshot.jpg", body, 'binary', function(err) {
      if (err) {
        console.error(err);
      } else {
        sendTo('telegram.0', '/tmp/snapshot.jpg');
      }
    });
  });
}
//Subscribe for the mqtt.0.arduino56.PIR2 variable as soon as true value, send the current image and two more with the period 10 sec
on({id: 'mqtt.0.arduino56.PIR2', change: 'any'}, function (obj) {
  if (obj.newState.val === true || obj.newState.val === 'true'){
    console.log('Оbserved the movement! Snapshot sent to telegram');
    sendTo('telegram.0', 'Attention! Motion! Publishing the image');
    sendImage();
    setTimeout(sendImage, 10000);
    setTimeout(sendImage, 20000);
  }
});

The result should be something like this: