paint-brush
How You Can Control Your Smart Home Through a Telegram Botby@BlackLight
6,091 reads
6,091 reads

How You Can Control Your Smart Home Through a Telegram Bot

by Fabio ManganielloJanuary 6th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The two main tools used in this tutorial are Telegram and platypush. The bot API is well documented and supported, and it’s very easy for anyone who knows a bit of programming to build integrations. In this story, I’ll show you how to easily set up commands and routines on top of existing smart home installations. The tutorial includes a tutorial on how to create your first home automation bot on Telegram. It also includes a plugin and backend that enables you to use the bot to control Philips Hue lights, music and PiCamera.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How You Can Control Your Smart Home Through a Telegram Bot
Fabio Manganiello HackerNoon profile picture

You’ve got your smart home fully set up. You regularly like to show off with your friends how cool it is to turn on light bulbs, play videos and movies with a hint to your voice assistant, make coffee and adjust the thermostat with a tap on an app. Congratulations!

But if you’re an automation enthusiast who rarely settles, you’re probably grown frustrated with the number of apps you’ll have to download and the number of interfaces you’ll have to master in order to control your gadgets. You’ll probably have an app for the lights, one for your media center, one for your smart shades, one for your thermostat, and a Google Home app that zealously (and hopelessly) tries to put all of them in the same place.

Probably most of these apps won’t communicate with the others, and probably many of them won’t work if you aren’t on the same network as your gadget. Wouldn’t it be cool if we could control everything from the same interface, without cluttering our phones or computers with tons of apps, through an interface that is accessible both from mobile and desktop devices as well as through external scripts/integrations, whether you are on your home network or outdoor? An interface that was both lightweight and straightforward to use?

But wait, hasn’t such an interface been around for a while, under the name of messaging or chat? After all, wouldn’t it be cool to control our house, gadgets and cloud services through the same interface that we use to send cat pictures to our friends, and through a bot completely tailored to our needs, without all the boilerplate/frustration that usually comes with 3rd-party bots?

Enter the world of chatbots-powered smart homes. In this story, I’ll show you how to easily set up commands and routines on top of existing smart home installations. The two main tools used in this tutorial are:

Telegram: many messaging apps and platforms exist out there, but so far the efforts of many of them (Facebook Messenger, Whatsapp, Hangouts etc.) in providing a usable developers API have been disappointing, to say the least. Gone are the golden days where everyone used XMPP or IRC as their messaging backbone and it was easy to integrate with services that all spoke the same language: today’s messaging apps world is extremely fragmented.

Moreover, as it’s in the interest of many of the big players to create walled gardens that don’t communicate with the rest of the world, the most used solutions out there don’t come with officially supported APIs/developer interfaces. Not only: some of the big players out there actively discourage users from using anything either than the official app to interact with the platform (evil Whatsapp, I’m looking at you).

In this extremely fragmented world made of several unconnected islands, Telegram represents a welcome exception: their official bot API is well documented and supported, and it’s very easy for anyone who knows a bit of programming to build integrations.

platypush: those who have been following me for a while will probably have heard of my platform for automation geeks (for those who don’t, you can wrap up by reading my introductory article to platypush, the wiki or the integrations documentation). Among the others, platypush also comes with a Telegram plugin and backend.

So let’s get started and make your first home automation bot!

Creating the Telegram bot

It’s quite easy to create a new bot on Telegram:

  • Open a conversation with the BotFather.
  • Type /start followed by 
    /newbot
     to create a new bot. Give your bot a display name and a username.
  • You’ll be provided with a link to start a conversation with your bot and unique API key. Store it somewhere as we’ll use it soon to configure the platypush plugin.

Configuring your bot in platypush

1. Install platypush with the main extensions plus the Telegram integration:

pip install 'platypush[http,db,telegram]'
apt-get install redis-server
[sudo] systemctl start redis
[sudo] systemctl enable redis

2. Play a bit with it if you haven’t done so yet. Find a few things that you’d like to manage/automate — lights, music, media, sensors, database, robots, smart plugs… — and install/configure the associated extensions.

In this article, we’ll see how to configure our newly created bot to control Philips Hue lights, music playback and PiCamera streaming.

3. Add the Telegram configuration to your

~/.config/platypush/config.yaml 
file:

chat.telegram:
    api_token: <your bot token>

backend.chat.telegram:
    enabled: true

The backend enables you to receive events (like new messages, attachments, requests etc.) and create custom hooks on them. The plugin enables you to write you to chats, programmatically send messages and attachments, administer channels etc.

Let’s say that we want the bot to implement the following commands:

  • /start
    : Welcome the new user.
  • /help
    : Show the available commands.
  • /lights_on
    : Turn on the lights.
  • /lights_off
    : Turn off the lights.
  • /music_play
    : Play a music resource/URL.
  • /music_pause
    : Toggle the playback pause state.
  • /music_next
    : Play the next song.
  • /music_prev
    : Play the previous song.
  • /start_streaming
    : Start remote streaming on the PiCamera.
  • /stop_streaming
    : Stop remote streaming on the PiCamera.

All we have to do is to create event hooks in the platypush config.yaml. In this context you’ll need to:

Install and configure the Philips Huemopidy and PiCamera plugins:

pip install 'platypush[hue,mpd,picamera]'

# Hue lights configuration
light.hue:
    # Hue bridge IP address
    bridge: 192.168.1.10

    # Default groups to control
    groups:
        - Living Room

# MPD/Mopidy configuration
music.mpd:
    host: localhost
    port: 6600

# PiCamera configuration
camera.pi:
    vflip: False
    hflip: False

To keep your 

config.yaml
 cleaner, create a new file named
~/.config/platypush/include/bot.yaml
:

# /start command handler
event.hook.OnTelegramStartCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: start
    then:
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Welcome! Type /help to see the available commands"

# /help command handler
event.hook.OnTelegramHelpCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: help
    then:
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Available commands:\n

                  - /lights_on\n
                  - /lights_off\n
                  - /music_play [resource]\n
                  - /music_pause\n
                  - /music_prev\n
                  - /music_next\n
                  - /start_streaming\n
                  - /stop_streaming\n
                    "

# /lights_on command handler
event.hook.OnTelegramLightsOnCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: lights_on
    then:
        - action: light.hue.on
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Lights turned on"

# /lights_off command handler
event.hook.OnTelegramLightsOffCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: lights_off
    then:
        - action: light.hue.off
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Lights turned off"

# /music_play command handler
event.hook.OnTelegramMusicPlayCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: music_play
    then:
        - if ${cmdargs}:
            - action: music.mpd.play
              args:
                  resource: cmdargs[0]
        - else:
            - action: music.mpd.play

        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Music playing"

# /music_pause command handler
event.hook.OnTelegramMusicPauseCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: music_pause
    then:
        - action: music.mpd.pause
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Music paused"

# /music_prev command handler
event.hook.OnTelegramMusicPrevCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: music_prev
    then:
        - action: music.mpd.previous
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Playing previous track"

# /music_next command handler
event.hook.OnTelegramMusicNextCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: music_next
    then:
        - action: music.mpd.next
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "Playing next track"

# /start_streaming command handler
event.hook.OnTelegramCameraStartStreamingCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: start_streaming
    then:
        - action: camera.pi.start_streaming
          args:
              listen_port: 2222

        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "PiCamera streaming started. Check it out with vlc tcp/h264://hostname:2222"


# /stop_streaming command handler
event.hook.OnTelegramCameraStopStreamingCmd:
    if:
        type: platypush.message.event.chat.telegram.CommandMessageEvent
        command: stop_streaming
    then:
        - action: camera.pi.stop_streaming
        - action: chat.telegram.send_message
          args:
              chat_id: ${chat_id}
              text: "PiCamera streaming stopped"

Include your bot configuration in 

config.yaml
:

include:
    - include/bot.yaml

Start platypush:

# Manual start
platypush

# Service start
systemctl start platypush.service

Open a conversation with your bot on Telegram through the link provided by the BotFather and start playing with it:

Right now the bot is accessible by anyone — you probably don’t want that. You can configure the Telegram backend so it only accepts messages from a specific list of chat IDs (in Telegram the chat_id is used both for private users and groups).

Send a message to the bot and open the platypush logs or check its standard output. You should see some messages like this:

2020-01-03 19:09:32,701| INFO|platypush|Received event: {"type": "event", "target": "turing", "origin": "turing", "id": "***", "args": {"type": "platypush.message.event.chat.telegram.CommandMessageEvent", "chat_id": your_chat_id, "message": {"text": "/help", ...}, "user": {"user_id": your_user_id, "username": "****", "is_bot": false, "link": "https://t.me/you", "language_code": "en", "first_name": "***", "last_name": "***"}, "command": "help", "cmdargs": []}}

Copy the chat_id associated to your user in the backend configuration:

backend.chat.telegram:
    authorized_chat_ids:
        - your_user_id

The bot will now reply with an error if you try to send a message from an unauthorized user.

You can also invite your bot to group chats and let your friends flicker the lights in your house if you wish so!

What next?

We have only explored one specific feature of the Telegram integration in this article: the ability of the bot to react to CommandMessageEvent events, run actions and reply with text messages. As you can see from the list of supported Telegram events you can do more, such as:

  • Create hooks when someone shares contact info — ever thought of letting a bot automatically store the new contacts sent to you by your friends by chat?
  • Create hooks when you share a photo, video or image file — for example automatically download all media files sent to a chat to your hard drive or a remote Dropbox folder.
  • Run actions on text messages instead of commands — you can use the TextMessageEvent, for example, if you prefer to type “turn on the lights” instead of “/lights_on”.
  • Take a picture from the camera and send it to yourself through the send_photo action.You can also deploy multiple bots, e.g. for per device, so you can run actions on a specific device from the associated chat, or instead use a single bot as entry point and deliver messages to other devices over MQTTKafka or HTTP API.

As long as there’s a plugin for it, you can now do it through your chat bot.