Translation of Redmine plug-ins from TelegramCLI to Webogram


    We have previously written about our redmine_chat_telegram and redmine_intouch plugins , designed
    to make your work with Redmine and Telegram more productive. And today we would like to talk about how we got rid of TelegramCLI. This is a big update, designed to simplify the work with our telegram-plugins.


    During the operation, we repeatedly had problems with setting up the TelegramCLI , so we decided to simplify the work of our plug-ins. To do this, we found it necessary to get rid of the TelegramCLI dependency, add a Webogram instead, and modify it to suit our needs. This allowed no longer running the TelegramCLI service on the server.


    Webogram


    Webogram is the official web client for Telegram, written in AngularJS. Therefore, to work with a Webogram through scripts, you have to use a headless browser, in our case it is PhantomJS.
    We modified the Webogram code to give it requests for the necessary actions without the need to work with the interface. In the form of a special request, the plugin sends to the URL of the modified Webogram an instruction to perform an operation in the Telegram.


    Principle of operation


    First, the plugin gives the command to perform some operation in the Telegram.


    telegram = TelegramCommon::Telegram.new
    result = telegram.execute('Test')

    Next, the launch string PhantomJS is formed.


    moduleTelegramCommonclassTelegram
        ...
        defmake_request
          @api_result = `#{cli_command}`
          debug(api_result)
          api_result
        enddefcli_command
          cmd = "#{phantomjs}#{config_path} \"#{api_url}\""
          debug(cmd)
          cmd
        endendend

    PhantomJS then performs the request and waits 10 seconds for the # api-status element to
    get the ready class, which would indicate that the Webogram has completed processing.


    // plugins/redmine_telegram_common/config/phantom-proxy.js
    ...
    page.open(url, function() {
      waitFor(
        function() {
          return page.evaluate(function() {
            return $('#api-status').hasClass('ready');
          });
        },
        function() {
          exit()
        }, 10000);
    });

    AngularJS-controller AppApiController is engaged in processing requests, and when it finishes, then on the page at the # api-status block the class changes to "ready", thereby notifying PhantomJS of the completion of the operation.


    // plugins/redmine_telegram_common/app/webogram/app/js/controllers.js
    $scope.promiseStatus = false;
    var args = {};
    if (typeof $routeParams.args !== 'undefined') {
      args = JSON.parse($routeParams.args)
    }
    $scope.handle = function(){
      var command = $routeParams.command;
      var handlerName = $scope['api' + command];
      if (typeof handlerName === 'function') {
        handlerName(args)
      } else {
        console.error('There is no ' + handlerName + ' api function.')
      }
    };
    $scope.apiTest = function(){
      $scope.successApi('api test')
    };
    ...
    $scope.successApi = function(msg){
      console.log('success: ' + msg);
      $scope.resolveApi()
    };
    $scope.failedApi = function(msg){
      console.log('failed: ' + msg);
      $scope.resolveApi()
    };
    $scope.resolveApi = function(){
      $scope.promiseStatus = true
    };

    Soon the request is executed, and the plugin receives a response with which to work.


    Conclusion


    As a result, in order to start working with our plugins, which previously depended on TelegramCLI, it is enough to install new dependencies, go to the redmine_telegram_common plugin settings page and go through a simple authorization procedure, during which PhantomJS will remember how to send Telegram requests.


    Telegram bots in our plugins redmine_chat_telegram and redmine_intouch learned how to work not only with getUpdates (as previously background processes through rake), but also through WebHooks (it is enough to initialize bots in the settings of the corresponding plug-ins, however HTTPS is required on Redmine). Thus, we got rid of additional background processes, which previously could have been a barrier to installing a plug-in by inexperienced users.


    If you have encountered any errors, you have suggestions for improving the plugin, or you wish to take part in the development of plug-ins, we will be happy for your feedback. Our repositories are listed below.


    Links



    Also popular now: