Websocket with Telegram Bot

  • AlexR
        5 years ago #10413

        Hello,

        I want to use the auto update function via a webhook with the telegram bot api. The telegram bot will send a HTTPS POST if there is an update. I have to set an URL for the webhook to start. The simplifier instance I am using is the “https://simplifier.r2d.siemens.cloud”. Is there any way to get the POST request in my application if i set the webhook at the instance?

        Thanks!

        Kahn
            Has successfully completed the online course Introduction
            Has successfully completed the online course Intermediate (200)
            Has successfully completed the online course Advanced (300)
          5 years ago #10414
          Up
          0
          Down
          ::

          Hi Alex,

          unfortunately it may not be possible out of the box. However there is one open WebSocketChannel where you can register your user and receive messages: the channel is: wss://simplifier.yourinstance.com/pushnotification/. Here is the code to open the WebSocketChannel:

          var lfn_open = function(data) {
              console.log( "opened", data);
          };
          
          var lfn_close = function(data) {
              console.log( "closed", data);
          };
          
          var lfn_error = function(data) {
              console.log("error", data);
          };
          
          var lfn_message = function(data) {
              console.log("message", data);
          };
          
          var lo_ws = new WebSocket("wss://simplifier.yourinstance.com/pushnotification/");
          
          lo_ws.onclose = lfn_close;
          lo_ws.onopen = lfn_open;
          lo_ws.onmessage = lfn_message;
          lo_ws.onerror = lfn_error;
          
          this.getGlobals().setVar("ws", lo_ws);

          We can save the WebSocket as a global variable for further usages, the datatype must be ANY. Neat huh 😉

          The next frame must be a register frame. It must be a stringified JSON Message of the following form: {“userName”: “admin”}. Please use the WebSocket to use this:

          this.getGlobals().getVar("ws").send(JSON.stringify({"userName":"admin"}))

          So now the problem begins. As of the moment the WebSocket Channel is not open and can only be used for Push Notifications made by a PushNotification Connector. So you have to build a Proxy which intercepts the POST Response and relays it to the PushNotification Connector, which can be accssed thorugh a Client Route. The Route is https://simplifier.yourinstance.com/client/1.0/connectorRequest

          The payload must be:

          {
           "connectorName": "NAME OF THE PUSHNOTIFICATION CONNECTOR"
           "json": {
            "msg": "The String Frame"
           }
          }

          Or you can make a call and use the following Route: https://simplifier.yourinstance.com/client/1.0/connectorCallRequest

          The payload should be the following:

          {
           "connectorName": "NAME OF THE PUSHNOTIFICATION CONNECTOR"
           "connectorCallName": "NAME OF THE PUSHNOTIFICATION CONNECTOR CALL"
           "json": {
            "msg": "The parameter"
           }
          }

          or:

          {
           "connectorName": "NAME OF THE PUSHNOTIFICATION CONNECTOR"
           "connectorCallName": "NAME OF THE PUSHNOTIFICATION CONNECTOR CALL"
           "json": {
            "msgAlais": "The alias"
           }
          }

          If you manage to relay your postBody to the Message field as a Stringified JSON then the WebSocket will receive it. You could also use a PushNotification Connector and Subscribe to it, then use the same method to send the message to. The WebSocket Object however offers you more control for closing, opening, error handling and message relaying as well as managing of the registrations.

          ByteFrames are not accepted unfortunately. I hope this helps, there is no direct method though. I am sorry.

          Cheers

          Kamil

          AlexR
              5 years ago #10419
              Up
              0
              Down
              ::

              Thanks for the detailed answer Kamil! Unfortunately the telegram bot only parses https:// adresses. I guess their websocket is only outgoing and doesnt allow incoming webhooks. Any other way to read HTTPS POST requests in my application? 😀

              Kahn
                  Has successfully completed the online course Introduction
                  Has successfully completed the online course Intermediate (200)
                  Has successfully completed the online course Advanced (300)
                5 years ago #10420
                Up
                0
                Down
                ::

                Hi Alex, if the Telegram WebSocket is really outgoing then there is no problem just use my Code and replace the SimplifierAddress with the Telegramm WebSocket address. You should then receive their message if their WebSocket returns the messages.

              Viewing 4 posts - 1 through 4 (of 4 total)

              You must be logged in to reply to this topic.