Communicate with TELEGRAM Bot

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Communicate with TELEGRAM Bot

    Hallo zusammen,

    ich habe ein kleines aber fieses Problem. Mein Ziel ist es mit einem Telegram Bot zu kommunizieren.

    Aber vielleicht zunächst zum Hintergrund.
    Die Aufgabenstellung: Ich möchte, wenn ich zuhause in den dunklen Stunden des Tages ankomme, dass meine Außenbeleuchtung angeht. Gelöst habe ich dies wie folgt. Locative als APP ruft, wenn ich in den Geofence Bereich komme, eine Website auf meinem externen Server auf. Diese versendet mit PHPMailer eine Email in meinen IMAP account nach einigen Prüfungen. Der IMAP account gibt per PUSH widerum eine Meldung an meinen FHEM Server und dieser schaltet das Außenlicht an. Laufzeit: ca 5-10 Sekunden.

    Diese Lösung hatte für mich in der Vergangenheit ganz gut funktioniert. Da ich aber nun in erster Linie mit IOBroker arbeite würde ich es gerne optimieren und die Latenzzeit rreduzieren. Die gefundene Lösung liegt in einem Telegram-Adapter für IOBroker. In Telegram habe ich mir einen privaten BOT eingerichtet. Die Kommunikation funktioniert prompt. Wenn ich in dem Chat mit dem Bot eine Nachricht schreibe, wie "schalte licht ein", so passiert dies sofort.
    Nun will ich aber ja nicht selbst eine Nachricht senden an den Bot, sondern dies sollte von der Website erfolgen (wie bisher die Emailversand); also getriggert durch Locative APP.

    Soweit so gut. Mit folgender PHP sende ich eine Nachricht in den Chat:

    Code:
    <?php
    class eTelegram
    {
        function __construct(string $subject)
        {
            $this->subject = $subject;
            $this->Telegram_senden($this->subject);
        }
    
        private function Telegram_senden(string $subject)
        {
            $apiToken = "60593xxxxx:yyyyyyyyyy5t8HfF6jJuF1aYCuxxxxxxxxxxxx";
            $chatId = "528yyyyy";
    
            $data = [
                'chat_id' => $chatId,
                'text' => $subject
            ];
    
            $apiUrl = "https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data);
    
            $ch = curl_init($apiUrl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            $response = curl_exec($ch);
    
            if ($response === false) {
                // Fehler beim Senden der Nachricht.
                echo 'Fehler beim Senden der Nachricht: ' . curl_error($ch);
            } else {
                // Erfolgreich gesendete Nachricht.
                echo 'Nachricht erfolgreich gesendet!';
            }
    
            curl_close($ch);
        }
    }
    ?>
    
    ​
    Soweit so gut. Nun kommt das ABER. Mit dem Skript sende ich im Namen des Bot. Ich möchte aber ja in meinem Namen an den Bot eine Nachricht senden, so dass er mit dem Einschalten des Lichts reagieren kann.

    Ich habe nun Google schon seeit Tagen durchforstet. Manche behaupten dort, dass dies gar nicht ginge. Weiß hier jemand mehr oder hat dies vielleicht sogar schon einmal gemacht?

    $apiToken = "60593xxxxx:yyyyyyyyyy5t8HfF6jJuF1aYCuxxxxxxxxxxxx"; <-- dies habe ich als apiToken von GodFather für den Bot erhalten
    $chatId = "528yyyyy​ <--- dies ist meine eigene UserID


    Viele Grüße
    Holger










  • #2
    Hi together,

    I put this topic somehow on the table, but nevertheless am still interested in it to solve it.
    In the meantime there is an IMAP adapter available for IOBroker. As this is working quite well, the need is not too urgent anymore.

    Nevertheless, I'd love to solve this issue.

    When using my chat ID ("528yyyyy) as stated above, the message is visible in Telegram in the chat with my BOT. Unfortunately, it appears as if the bot would have send something to me. I need it vice versa. I have to send something to my bot. On behalf of me this should happen by my website.

    I wish you a pleasant Merry Christmas.

    Best regards
    Holger

    Kommentar


    • #3
      Entering a geofenced area triggers Locate as an app to access a webpage hosted on my external server. After some testing, this uses PHPMailer to deliver emails to my IMAP account. The external light is turned on when the IMAP account uses PUSH to communicate with my FHEM server. You can expect to see the message in your Telegram conversation with my BOT. Regrettably, it appears that the bot will communicate with me.
      Zuletzt geändert von ; 03.01.2024, 07:55.

      Kommentar


      • #4
        I meanwhile got a bit further. My understanding: There are two TELEGRAM APIs, one to communicate with the BOT and one general one.

        The general one seems to be a bit more difficult as you need to regester and so on. The API for the BOTs has the limitation, that you only can send the message in the name of the BOT and not in your own.

        However, I will stay with my current solution for this use case, as it works stable and still quite fast.

        Happy New Year 2024.

        Kommentar

        Lädt...
        X