Telegram Bot (Talk2Fhem) mit dem SmartHome einrichten

Telegram Chat mit SmartHome

Click here for English version

Wie richte ich einen Telegram Bot mit FHEM ein?

  1. Einrichtung eines neuen Bots mit /newbot
  2. Definition des Telebots in FHEM
  3. Einrichten Eures Kontaktes in FHEM
  4. Testen & Absichern

Setup des Telegram Bots

In diesem Blog möchte ich Euch zeigen, wie Ihr einen Telegram Bot (Chatbot) mit dem Raspberry SmartHome (FHEM) einrichtet. In einem zweiten Schritt kann man sogar über Google Home Mini mit FHEM sprechen. Dazu später mehr, zunächst zu Telegram.

Dazu startet ihr Telegram und einen Chat mit dem User BotFather. Dann gebt ihr /newbot ein. Es ergibt sich ein Dialog:

/newbot
—————-
BotFather:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
—————-
Client:
MeinFHEMBot
—————-
BotFather:
Good. Now let’s choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
—————-
Client:
my_fhem_bot <—– ersetzen durch Euren erdachten Namen
—————-
BotFather:
Done! Congratulations on your new bot.
You will find it at telegram.me/my_fhem_bot.
You can now add a description, about section and profile picture for your bot, see /help for a list of commands.
—————-
Use this token to access the HTTP API:
1234567890:abcdefghijklmnopqrst-uvwxyzEine Beschreibung des Bot APIs könnt ihr hier einsehen https://core.telegram.org/bots/api

Wir kopieren uns das persönliche Token

1234567890:abcdefghijklmnopqrst-uvwxyz

in die Zwischenablage. Wir starten FHEM und geben in das Kommandofeld von FHEM folgendes ein:

define teleBot TelegramBot
1234567890:abcdefghijklmnopqrst-uvwxyz
define_telebot

wobei wir 1234567890:abcdefghijklmnopqrst-uvwxyz durch den Token aus unserer Zwischenablage ersetzen. Jetzt sorgen wir noch dafür, daß der FHEM den Bot auch abfragt:

attr teleBot pollingTimeout 120

attr teleBot pollingTimeout 120
Wenn wir jetzt aus dem Smartphone an @my_fhem_bot (also Euren erdachten Namen des Bots) eine Nachricht senden, werdet ihr als Chat-User bei FHEM aufgenommen. Wenn ihr alle User die Nachrichten senden dürfen aufgenommen habt, solltet Ihr verhindern, daß weitere die Erlaubnis erhalten:

attr teleBot allowUnknownContacts 0
attr teleBot pollingTimeout 120

Damit ist FHEM technisch mit Telegram verbunden.

Testen von Telegram Bot

Die Kommunikation kann folgendermaßen getestet werden: wenn ihr an den Chatbot aus Eurem Smartphone schreibt, sollte die Nachricht im Logfile von FHEM auftauchen. Die andere Richtung könnt ihr im Kommandofeld von FHEM austesten mit

set teleBot message Hello World
set teleBot message Hello World

und auf Eurem Smartphone erscheint „Hello World“.

hello world

Reaktion auf Text und Sprache

Zur Reaktion auf Text und Sprache (Spracheingabe Google) verwende ich Talk2Fhem oder Babble. Im weiteren Verlauf gehe ich auf Nutzung von Talk2Fhem ein. Zum Setup geben wir in das Kommandofeld von FHEM nacheinander

update 39_Talk2Fhem
shutdown restart

ein. Dann geben wir noch die drei Befehle nacheinander

define talk Talk2Fhem
define n_telbot notify teleBot:msgText.* set talk $EVENT
attr global language DE

ein. Zum Überprüfen der exekutierten Befehle sollte man anfangs noch in das Kommandofeld

define n_talk notify talk:.* {}

eingeben womit ein neues Fenster erscheint und wir auf DEF klicken und für die Definition von n_talk

n_talk
talk:.* {
 # Sende die Antwort per Telegram und gebe es über das GoogleHome aus
 	if ($EVENT =~ s/^answers: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 		#fhem("set d_googlespeak $EVENT");
 	}

 # Schicke den Fehler per Telegram und sag am GoogleHome das es nicht geklappt hat.
 	if ($EVENT =~ s/^err: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 		my @a = ("Das hat leider nicht geklappt", "Es gab leider einen Fehler", "Es tut mir leid. Das hat nicht funktioniert.", "Es ist leider zu einem Fehler gekommen","Könntest du das vielleicht nochmal anders sagen", "Mhhh, das kann ich so nicht verstehen");
 		#fhem("set d_googlespeak $a[int(rand($#a))]");
 	} 

 # Schick mir alle ausgeführten Befehle als Telegram

 	if ($EVENT =~ s/^cmds: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 	} 

 }

eingeben. So hat man eine Übersicht was eigentlich passiert.

Wir klicken auf Talk und dann auf die DEF und definieren ein Kommando für Licht durch die Definition

talk talk2fhem
Licht\S* an = (cmd=&amp;amp;amp;amp;amp;gt; 'set Funk_Schalter1 on' , answer=&amp;amp;amp;amp;amp;gt;'"Licht ist an!"')

und kann man jetzt im Chatbot die Nachricht

Bitte mach das Licht an

senden und als Rückmeldung kommt

Licht ist an!

sowie das ausgeführte Kommando

set Funk_Schalter1 on

als Kontrolle.
Dank Talk2Fhem sind nun auch Befehle möglich wie: 

Bitte mach das Licht in einer Stunde an
 

oder 

Bitte mach das Licht um 10 Uhr an
 

sowie Datumsangaben wie 

Bitte mach das Licht morgen abend um 8 Uhr an
 

oder 

Bitte mach das Licht Dienstag abend um 8 Uhr an

Telegram Chat mit SmartHome

How do I setup a telegram bot with FHEM?

  1. Setup of a new chatbot with /newbot
  2. Definition of the chatbot in FHEM
  3. Setup of allowed contacts in FHEM
  4. Testing & Protection

Setup Telegram Bot

In this blog I’d like to show you how to set up a Telegram Bot (Chatbot) with the Raspberry SmartHome (FHEM). In a second step you can even talk about Google Home Mini with FHEM. More about this later, first about Telegram.

To do this you start Telegram and chat with „BotFather“. Then enter /newbot. A dialogue starts as following:

/newbot
—————-
BotFather:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
—————-
Client:
MeinFHEMBot
—————-
BotFather:
Good. Now let’s choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
—————-
Client:
my_fhem_bot <—– replace by your unique name
—————-
BotFather:
Done! Congratulations on your new bot.
You will find it at telegram.me/my_fhem_bot.
You can now add a description, about section and profile picture for your bot, see /help for a list of commands.
—————-
Use this token to access the HTTP API:
1234567890:abcdefghijklmnopqrst-uvwxyzFor a description of the Bot API, see this page: https://core.telegram.org/bots/api

We copy the personal token 1234567890:abcdefghijklmnopqrst-uvwxyz into the clipboard. Thereafter we start FHEM and enter into the command field

define teleBot TelegramBot
1234567890:abcdefghijklmnopqrst-uvwxyz
define_telebot

and replace 1234567890:abcdefghijklmnopqrst-uvwxyz by our token from the clipboard. Now we make FHEM polling chats from the Telegram Bot

attr teleBot pollingTimeout 120

attr teleBot pollingTimeout 120
If we now send a message from the smartphone to @my_fhem_bot (your given name of the bot), you will be accepted as a chat user at FHEM. If you have recorded all users who are allowed to send messages, you should prevent others from receiving permission

attr teleBot allowUnknownContacts 0
attr teleBot pollingTimeout 120

Now FHEM is technically connected with Telegram.

Testing Telegram Bot

The communication can be tested as follows: if you write to the chatbot from your smartphone, the message should appear in FHEM’s logfile. You can test the other direction in the command field of FHEM with

set teleBot message Hello World
set teleBot message Hello World

and on your smartphone „Hello World“ pops up,

hello world

Voice and text control

For voice and text control (voice control by keyboard google) I use Talk2Fhem  orBabble. In this context I’ll describe how to use Talk2Fhem.

To set it up we enter into the command field of FHEM the commands one by one

update 39_Talk2Fhem
shutdown restart

Thereafter we enter the following three commands.

define talk Talk2Fhem
define n_telbot notify teleBot:msgText.* set talk $EVENT
attr global language EN

Thereafter we define the talk module by entering

define n_talk notify talk:.* {}

and a window pops up where we click on DEF and enter for the definitions of n_talk

n_talk
talk:.* {
 # Sende die Antwort per Telegram und gebe es über das GoogleHome aus
 	if ($EVENT =~ s/^answers: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 		#fhem("set d_googlespeak $EVENT");
 	}

 # Schicke den Fehler per Telegram und sag am GoogleHome das es nicht geklappt hat.
 	if ($EVENT =~ s/^err: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 		my @a = ("Das hat leider nicht geklappt", "Es gab leider einen Fehler", "Es tut mir leid. Das hat nicht funktioniert.", "Es ist leider zu einem Fehler gekommen","Könntest du das vielleicht nochmal anders sagen", "Mhhh, das kann ich so nicht verstehen");
 		#fhem("set d_googlespeak $a[int(rand($#a))]");
 	} 

 # Schick mir alle ausgeführten Befehle als Telegram

 	if ($EVENT =~ s/^cmds: //) {
 		fhem("set teleBot _msg \@USER $EVENT");
 	} 

 }

Using that we have an overview over executed commands.

We click on talk and therafer on DEF to define a command for switching the light by

talk talk2fhem
Licht\S* an = (cmd=&amp;amp;amp;amp;amp;gt; 'set Funk_Schalter1 on' , answer=&amp;amp;amp;amp;amp;gt;'"Licht ist an!"')

and now you can enter in the chatbot

Bitte mach das Licht an

and get

Licht ist an!

as well as the command

set Funk_Schalter1 on

to control it.
Thanks to Talk2Fhem you can send the command 

Bitte mach das Licht in einer Stunde an
 

or 

Bitte mach das Licht um 10 Uhr an
 

and do relative time dependencies as 

Bitte mach das Licht morgen abend um 8 Uhr an
 

or

Bitte mach das Licht Dienstag abend um 8 Uhr an

Telegram Chat mit SmartHome

Eine Antwort zu „Telegram Bot (Talk2Fhem) mit dem SmartHome einrichten”.

  1. Avatar von Selbstbau Signalduino – SmartHome & Internet of Things | Anleitung, Tests, Empfehlungen

    […] kann man nun Funkrauchmelder koppeln, Funksteckdosen schalten oder einen Chatbot mit Telegram […]